Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,28 @@ CompileExpr::visit (HIR::CallExpr &expr)
};

auto fn_address = CompileExpr::Compile (expr.get_fnexpr (), ctx);
if (ctx->const_context_p ())
{
if (!FUNCTION_POINTER_TYPE_P (TREE_TYPE (fn_address)))
{
rust_error_at (expr.get_locus (),
"calls in constants are limited to constant "
"functions, tuple structs and tuple variants");
return;
}

if (TREE_CODE (fn_address) == ADDR_EXPR)
{
tree fndecl = TREE_OPERAND (fn_address, 0);
if (!DECL_DECLARED_CONSTEXPR_P (fndecl))
{
rust_error_at (expr.get_locus (),
"calls in constants are limited to constant "
"functions, tuple structs and tuple variants");
return;
}
}
}

// is this a closure call?
bool possible_trait_call
Expand Down
60 changes: 60 additions & 0 deletions gcc/rust/backend/rust-compile-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -297,5 +297,65 @@ CompileItem::visit (HIR::Module &module)
CompileItem::compile (item.get (), ctx);
}

void
CompileItem::visit (HIR::TupleStruct &tuple_struct_decl)
{
TyTy::BaseType *lookup = nullptr;
if (!ctx->get_tyctx ()->lookup_type (
tuple_struct_decl.get_mappings ().get_hirid (), &lookup))
{
rust_error_at (tuple_struct_decl.get_locus (), "failed to resolve type");
return;
}

if (lookup->is_concrete ())
TyTyResolveCompile::compile (ctx, lookup);
}

void
CompileItem::visit (HIR::Enum &enum_decl)
{
TyTy::BaseType *lookup = nullptr;
if (!ctx->get_tyctx ()->lookup_type (enum_decl.get_mappings ().get_hirid (),
&lookup))
{
rust_error_at (enum_decl.get_locus (), "failed to resolve type");
return;
}

if (lookup->is_concrete ())
TyTyResolveCompile::compile (ctx, lookup);
}

void
CompileItem::visit (HIR::Union &union_decl)
{
TyTy::BaseType *lookup = nullptr;
if (!ctx->get_tyctx ()->lookup_type (union_decl.get_mappings ().get_hirid (),
&lookup))
{
rust_error_at (union_decl.get_locus (), "failed to resolve type");
return;
}

if (lookup->is_concrete ())
TyTyResolveCompile::compile (ctx, lookup);
}

void
CompileItem::visit (HIR::StructStruct &struct_decl)
{
TyTy::BaseType *lookup = nullptr;
if (!ctx->get_tyctx ()->lookup_type (struct_decl.get_mappings ().get_hirid (),
&lookup))
{
rust_error_at (struct_decl.get_locus (), "failed to resolve type");
return;
}

if (lookup->is_concrete ())
TyTyResolveCompile::compile (ctx, lookup);
}

} // namespace Compile
} // namespace Rust
8 changes: 4 additions & 4 deletions gcc/rust/backend/rust-compile-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ class CompileItem : private HIRCompileBase, protected HIR::HIRStmtVisitor
void visit (HIR::ImplBlock &impl_block) override;
void visit (HIR::ExternBlock &extern_block) override;
void visit (HIR::Module &module) override;
void visit (HIR::TupleStruct &tuple_struct) override;
void visit (HIR::Enum &enum_decl) override;
void visit (HIR::Union &union_decl) override;
void visit (HIR::StructStruct &struct_decl) override;

// Empty visit for unused Stmt HIR nodes.
void visit (HIR::TupleStruct &) override {}
void visit (HIR::EnumItem &) override {}
void visit (HIR::EnumItemTuple &) override {}
void visit (HIR::EnumItemStruct &) override {}
Expand All @@ -57,9 +60,6 @@ class CompileItem : private HIRCompileBase, protected HIR::HIRStmtVisitor
void visit (HIR::ExternCrate &) override {}
void visit (HIR::UseDeclaration &) override {}
void visit (HIR::TypeAlias &) override {}
void visit (HIR::StructStruct &) override {}
void visit (HIR::Enum &) override {}
void visit (HIR::Union &) override {}
void visit (HIR::Trait &) override {}
void visit (HIR::EmptyStmt &) override {}
void visit (HIR::LetStmt &) override {}
Expand Down
20 changes: 13 additions & 7 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1726,16 +1726,22 @@ TypeCheckExpr::visit (HIR::MatchExpr &expr)
void
TypeCheckExpr::visit (HIR::ClosureExpr &expr)
{
TypeCheckContextItem current_context = context->peek_context ();
TyTy::FnType *current_context_fndecl = current_context.get_context_type ();

std::vector<TyTy::SubstitutionParamMapping> subst_refs;
HirId ref = expr.get_mappings ().get_hirid ();
DefId id = expr.get_mappings ().get_defid ();
RustIdent ident{current_context_fndecl->get_ident ().path, expr.get_locus ()};
RustIdent ident{CanonicalPath::create_empty (), expr.get_locus ()};

if (context->have_function_context ())
{
TypeCheckContextItem current_context = context->peek_context ();
TyTy::FnType *current_context_fndecl
= current_context.get_context_type ();

ident = RustIdent{current_context_fndecl->get_ident ().path,
expr.get_locus ()};

// get from parent context
std::vector<TyTy::SubstitutionParamMapping> subst_refs
= current_context_fndecl->clone_substs ();
subst_refs = current_context_fndecl->clone_substs ();
}

std::vector<TyTy::TyVar> parameter_types;
for (auto &p : expr.get_params ())
Expand Down
15 changes: 15 additions & 0 deletions gcc/testsuite/rust/compile/issue-3551.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[lang = "sized"]
pub trait Sized {}

#[lang = "fn_once"]
pub trait FnOnce<Args> {
#[lang = "fn_once_output"]
type Output;

extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
}

struct Bug {
a: [(); (|| 0)()],
// { dg-error "calls in constants are limited to constant functions, tuple structs and tuple variants" "" { target *-*-* } .-1 }
}