gccrs: fix ICE when we have unimplemented/invalid trait items

When the resulting trait item is in an error state this means the
underlying fields will be null.

Fixes #2478

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-item.cc: add is_error check

gcc/testsuite/ChangeLog:

	* rust/compile/non_member_const.rs: add missing error message
	* rust/compile/issue-2478.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
This commit is contained in:
Philip Herron 2023-07-29 17:33:47 +01:00 committed by Arthur Cohen
parent 4d63098451
commit 46a5df00ab
3 changed files with 20 additions and 5 deletions

View file

@ -615,7 +615,8 @@ TypeCheckItem::validate_trait_impl_block (
impl_item.get (), self,
specified_bound,
substitutions);
trait_item_refs.push_back (trait_item_ref.get_raw_item ());
if (!trait_item_ref.is_error ())
trait_item_refs.push_back (trait_item_ref.get_raw_item ());
}
}

View file

@ -0,0 +1,16 @@
#[lang = "sized"]
pub trait Sized {}
struct Bar;
trait Foo {
const N: u32;
fn M();
}
impl Foo for Bar {
// { dg-error "missing N, M in implementation of trait .Foo." "" { target *-*-* } .-1 }
fn N() {}
// { dg-error "method .N. is not a member of trait .Foo." "" { target *-*-* } .-1 }
}

View file

@ -7,9 +7,7 @@ trait Foo {
struct Bar;
impl Foo for Bar {
impl Foo for Bar {// { dg-error "missing N in implementation of trait .Foo." }
const N : u32 = 0; // { dg-error "item .N. is an associated const, which does not match its trait .Foo." }
// error: item `N` is an associated const, which doesn't match its
// trait `<Bar as Foo>`
}
}
}