Skip to content

Commit

Permalink
gccrs: fix ICE when we have unimplemented/invalid trait items
Browse files Browse the repository at this point in the history
When the resulting trait item is in an error state this means the
underlying fields will be null.

Fixes Rust-GCC#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>
  • Loading branch information
philberty authored and CohenArthur committed Jan 9, 2024
1 parent 65732d4 commit af3eded
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
3 changes: 2 additions & 1 deletion gcc/rust/typecheck/rust-hir-type-check-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ());
}
}

Expand Down
16 changes: 16 additions & 0 deletions gcc/testsuite/rust/compile/issue-2478.rs
Original file line number Diff line number Diff line change
@@ -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 }
}
6 changes: 2 additions & 4 deletions gcc/testsuite/rust/compile/non_member_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>`
}
}
}

0 comments on commit af3eded

Please sign in to comment.