Skip to content

Commit 83e13db

Browse files
bors[bot]philberty
andauthored
1190: Fix ICE in reachability class and refactor associated types code r=philberty a=philberty There are several fixes going on to solve these issues which overlap with one another so it seemed best to pack them within the same PR. The main issue for #1128 was that we did not resolve a trait when it was unused leading to us hitting the ICE in the privacy pass. Since the type check context was empty for the trait since it was not resolved. To fix this we needed to refactor the trait resolver to resolve the trait as part of iterating the crate. This resulted in some regressions in the testsuite so this is why we need the the other commits. Which required us to finally perform the refactor specified in #1105 to fix these. Fixes #1105 #1128 #1132 1192: Add an assertion to avoid peeking when the stack is empty r=philberty a=philberty This will ensure we get a proper ICE instead of memory corruption/segv. Addresses #1130 1193: Remove unused parameter caller from generating Call expressions r=philberty a=philberty Within const context the fncontext maybe empty which in turn results in a segv for generating const calls which will be evaluated by the const-expr code anyway. Addresses #1130 Co-authored-by: Philip Herron <[email protected]>
4 parents 9cf744c + bbf171c + 7fe6bc1 + ae085ac commit 83e13db

16 files changed

+323
-400
lines changed

gcc/rust/backend/rust-compile-context.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,11 @@ class Context
254254
bool in_fn () { return fn_stack.size () != 0; }
255255

256256
// Note: it is undefined behavior to call peek_fn () if fn_stack is empty.
257-
fncontext peek_fn () { return fn_stack.back (); }
257+
fncontext peek_fn ()
258+
{
259+
rust_assert (!fn_stack.empty ());
260+
return fn_stack.back ();
261+
}
258262

259263
void push_type (tree t) { type_decls.push_back (t); }
260264
void push_var (::Bvariable *v) { var_decls.push_back (v); }

gcc/rust/backend/rust-compile-expr.cc

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,8 @@ CompileExpr::visit (HIR::CallExpr &expr)
497497

498498
// must be a call to a function
499499
auto fn_address = CompileExpr::Compile (expr.get_fnexpr (), ctx);
500-
auto fncontext = ctx->peek_fn ();
501-
translated
502-
= ctx->get_backend ()->call_expression (fncontext.fndecl, fn_address, args,
503-
nullptr, expr.get_locus ());
500+
translated = ctx->get_backend ()->call_expression (fn_address, args, nullptr,
501+
expr.get_locus ());
504502
}
505503

506504
void
@@ -610,10 +608,8 @@ CompileExpr::visit (HIR::MethodCallExpr &expr)
610608
args.push_back (rvalue);
611609
}
612610

613-
auto fncontext = ctx->peek_fn ();
614-
translated
615-
= ctx->get_backend ()->call_expression (fncontext.fndecl, fn_expr, args,
616-
nullptr, expr.get_locus ());
611+
translated = ctx->get_backend ()->call_expression (fn_expr, args, nullptr,
612+
expr.get_locus ());
617613
}
618614

619615
tree
@@ -696,8 +692,8 @@ CompileExpr::compile_dyn_dispatch_call (const TyTy::DynamicObjectType *dyn,
696692
tree fn_expr
697693
= ctx->get_backend ()->var_expression (fn_convert_expr_tmp, expr_locus);
698694

699-
return ctx->get_backend ()->call_expression (fnctx.fndecl, fn_expr, args,
700-
nullptr, expr_locus);
695+
return ctx->get_backend ()->call_expression (fn_expr, args, nullptr,
696+
expr_locus);
701697
}
702698

703699
tree
@@ -866,9 +862,8 @@ CompileExpr::resolve_operator_overload (
866862
if (rhs != nullptr) // can be null for negation_expr (unary ones)
867863
args.push_back (rhs);
868864

869-
auto fncontext = ctx->peek_fn ();
870-
return ctx->get_backend ()->call_expression (fncontext.fndecl, fn_expr, args,
871-
nullptr, expr.get_locus ());
865+
return ctx->get_backend ()->call_expression (fn_expr, args, nullptr,
866+
expr.get_locus ());
872867
}
873868

874869
tree
@@ -1289,10 +1284,8 @@ HIRCompileBase::resolve_deref_adjustment (Resolver::Adjustment &adjustment,
12891284
}
12901285

12911286
// make the call
1292-
auto fncontext = ctx->peek_fn ();
1293-
return ctx->get_backend ()->call_expression (fncontext.fndecl, fn_address,
1294-
{adjusted_argument}, nullptr,
1295-
locus);
1287+
return ctx->get_backend ()->call_expression (fn_address, {adjusted_argument},
1288+
nullptr, locus);
12961289
}
12971290

12981291
tree

gcc/rust/backend/rust-compile-implitem.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ CompileTraitItem::visit (HIR::TraitItemFunc &func)
5252

5353
rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
5454
TyTy::FnType *fntype = static_cast<TyTy::FnType *> (concrete);
55+
fntype->monomorphize ();
5556

5657
// items can be forward compiled which means we may not need to invoke this
5758
// code. We might also have already compiled this generic function as well.

gcc/rust/backend/rust-compile-item.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ CompileItem::visit (HIR::Function &function)
127127
{
128128
rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
129129
fntype = static_cast<TyTy::FnType *> (concrete);
130+
fntype->monomorphize ();
130131
}
131132
}
132133

gcc/rust/backend/rust-compile-resolve-path.cc

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -251,21 +251,6 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
251251
rust_assert (ok); // found
252252
rust_assert (trait_item_ref->is_optional ()); // has definition
253253

254-
Analysis::NodeMapping trait_mappings
255-
= trait_item_ref->get_parent_trait_mappings ();
256-
257-
HirId associated_impl_id;
258-
ok = ctx->get_tyctx ()->lookup_associated_impl_mapping_for_self (
259-
trait_mappings.get_hirid (), receiver, &associated_impl_id);
260-
rust_assert (ok);
261-
262-
Resolver::AssociatedImplTrait *associated = nullptr;
263-
bool found_associated_trait_impl
264-
= ctx->get_tyctx ()->lookup_associated_trait_impl (
265-
associated_impl_id, &associated);
266-
rust_assert (found_associated_trait_impl);
267-
associated->setup_associated_types ();
268-
269254
return CompileTraitItem::Compile (
270255
trait_item_ref->get_hir_trait_item (), ctx, lookup, true,
271256
expr_locus);

gcc/rust/rust-backend.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,7 @@ class Backend
285285

286286
// Create an expression for a call to FN with ARGS, taking place within
287287
// caller CALLER.
288-
virtual tree call_expression (tree caller, tree fn,
289-
const std::vector<tree> &args,
288+
virtual tree call_expression (tree fn, const std::vector<tree> &args,
290289
tree static_chain, Location)
291290
= 0;
292291

gcc/rust/rust-gcc.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class Gcc_backend : public Backend
250250

251251
tree array_index_expression (tree array, tree index, Location);
252252

253-
tree call_expression (tree caller, tree fn, const std::vector<tree> &args,
253+
tree call_expression (tree fn, const std::vector<tree> &args,
254254
tree static_chain, Location);
255255

256256
// Statements.
@@ -1794,8 +1794,7 @@ Gcc_backend::array_index_expression (tree array_tree, tree index_tree,
17941794

17951795
// Create an expression for a call to FN_EXPR with FN_ARGS.
17961796
tree
1797-
Gcc_backend::call_expression (tree, // containing fcn for call
1798-
tree fn, const std::vector<tree> &fn_args,
1797+
Gcc_backend::call_expression (tree fn, const std::vector<tree> &fn_args,
17991798
tree chain_expr, Location location)
18001799
{
18011800
if (fn == error_mark_node || TREE_TYPE (fn) == error_mark_node)

gcc/rust/typecheck/rust-hir-path-probe.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -326,18 +326,6 @@ class PathProbeType : public TypeCheckBase
326326
}
327327

328328
TyTy::BaseType *trait_item_tyty = trait_item_ref->get_tyty ();
329-
if (impl != nullptr && !is_reciever_generic ())
330-
331-
{
332-
HirId impl_block_id = impl->get_mappings ().get_hirid ();
333-
AssociatedImplTrait *lookup_associated = nullptr;
334-
bool found_impl_trait
335-
= context->lookup_associated_trait_impl (impl_block_id,
336-
&lookup_associated);
337-
// see testsuite/rust/compile/torture/traits10.rs this can be false
338-
if (found_impl_trait)
339-
lookup_associated->setup_associated_types ();
340-
}
341329

342330
// we can substitute the Self with the receiver here
343331
if (trait_item_tyty->get_kind () == TyTy::TypeKind::FNDEF)

gcc/rust/typecheck/rust-hir-trait-ref.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -454,18 +454,11 @@ class AssociatedImplTrait
454454

455455
TyTy::BaseType *get_self () { return self; }
456456

457-
void setup_associated_types ();
458-
459-
void setup_associated_types2 (const TyTy::BaseType *self,
460-
const TyTy::TypeBoundPredicate &bound);
457+
void setup_associated_types (const TyTy::BaseType *self,
458+
const TyTy::TypeBoundPredicate &bound);
461459

462460
void reset_associated_types ();
463461

464-
TyTy::BaseType *get_projected_type (const TraitItemReference *trait_item_ref,
465-
TyTy::BaseType *reciever, HirId ref,
466-
HIR::GenericArgs &trait_generics,
467-
Location expr_locus);
468-
469462
private:
470463
TraitReference *trait;
471464
HIR::ImplBlock *impl;

0 commit comments

Comments
 (0)