Skip to content

Commit cfa155b

Browse files
philbertydkm
authored andcommitted
gccrs: Fix segv in errors in type checking an impl item
When we typecheck a trait impl block item, at the end we validate it against the trait definition by doing a final unify but if the type check fails on the the impl item it returns NULL here. The other issue was that if we fail to resolve the specified lifetime we return error early, this changes the typechecking to default to an anon lifetime so we can continue typechecking. Fixes #4188 gcc/rust/ChangeLog: * typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItemWithTrait::visit): null guard * typecheck/rust-hir-type-check.cc (TraitItemReference::get_type_from_fn): default to anon gcc/testsuite/ChangeLog: * rust/compile/issue-4188.rs: New test. Signed-off-by: Philip Herron <[email protected]>
1 parent f7c5eca commit cfa155b

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

gcc/rust/typecheck/rust-hir-type-check-implitem.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,8 @@ TypeCheckImplItemWithTrait::visit (HIR::Function &function)
582582
// normal resolution of the item
583583
TyTy::BaseType *lookup
584584
= TypeCheckImplItem::Resolve (parent, function, self, substitutions);
585+
if (lookup == nullptr)
586+
return;
585587

586588
// map the impl item to the associated trait item
587589
const auto tref = trait_reference.get ();

gcc/rust/typecheck/rust-hir-type-check.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,19 +237,20 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const
237237
: Mutability::Mut;
238238
rust_assert (self_param.has_lifetime ());
239239

240+
auto region = TyTy::Region::make_anonymous ();
240241
auto maybe_region = context->lookup_and_resolve_lifetime (
241242
self_param.get_lifetime ());
242-
243-
if (!maybe_region.has_value ())
243+
if (maybe_region.has_value ())
244+
region = maybe_region.value ();
245+
else
244246
{
245247
rust_error_at (self_param.get_locus (),
246248
"failed to resolve lifetime");
247-
return get_error ();
248249
}
250+
249251
self_type = new TyTy::ReferenceType (
250252
self_param.get_mappings ().get_hirid (),
251-
TyTy::TyVar (self->get_ref ()), mutability,
252-
maybe_region.value ());
253+
TyTy::TyVar (self->get_ref ()), mutability, region);
253254
}
254255
break;
255256

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
trait MemoryUnit {
2+
extern "C" fn read_dword(&'s self) -> u16 {}
3+
// { dg-error {failed to resolve lifetime} "" { target *-*-* } .-1 }
4+
// { dg-error {mismatched types} "" { target *-*-* } .-2 }
5+
}
6+
7+
impl MemoryUnit for MemoryUnit {
8+
extern "C" fn read_dword(&'s self) -> u16 {
9+
let b16 = self.read_word() as u16;
10+
11+
b16 << 8
12+
}
13+
}

0 commit comments

Comments
 (0)