Skip to content

gccrs: Add missing bounds checks on the associated impl - #4787

Merged
philberty merged 1 commit into
masterfrom
phil/trait-bounds
Aug 16, 2026
Merged

gccrs: Add missing bounds checks on the associated impl#4787
philberty merged 1 commit into
masterfrom
phil/trait-bounds

Conversation

@philberty

Copy link
Copy Markdown
Member

Before this require_b<T: B> (T) only checked for S implement B and it early accepted the

impl<T: A> B for S

But we need to validate the impl's bound after we bind the arguments being used on the generic impl. So when we do a call-expr/method-call expr we are binding to that impl and we need to validate. When we do that we know that impl T = Bad and we walk the impl generic params T: A apply the subst for Bad: A where its is not implemented and we fail and return.

Fixes #4678

gcc/rust/ChangeLog:

* typecheck/rust-hir-trait-reference.h: add emit_errors option
* typecheck/rust-hir-trait-resolve.cc (AssociatedImplTrait::bind_impl_for_projection): new
(AssociatedImplTrait::bind_impl_for_bound): call new validate function
* typecheck/rust-type-util.cc (lookup_associated_impl_block): make this more generic
* typecheck/rust-tyty-call.cc (validate_call_argument_associated_impl_bounds): new
(TypeCheckCallExpr::visit): for each call arg validate and bind
(TypeCheckMethodCallExpr::check): likewise

gcc/testsuite/ChangeLog:

* rust/compile/issue-4678.rs: New test.

Before this require_b<T: B> (T) only checked for S<Bad> implement B and
it early accepted the

  impl<T: A> B for S<T>

But we need to validate the impl's bound after we bind the arguments being
used on the generic impl. So when we do a call-expr/method-call expr we are
binding to that impl and we need to validate. When we do that we know that
impl T = Bad and we walk the impl generic params T: A apply the subst for
Bad: A where its is not implemented and we fail and return.

Fixes #4678

gcc/rust/ChangeLog:

	* typecheck/rust-hir-trait-reference.h: add emit_errors option
	* typecheck/rust-hir-trait-resolve.cc (AssociatedImplTrait::bind_impl_for_projection): new
	(AssociatedImplTrait::bind_impl_for_bound): call new validate function
	* typecheck/rust-type-util.cc (lookup_associated_impl_block): make this more generic
	* typecheck/rust-tyty-call.cc (validate_call_argument_associated_impl_bounds): new
	(TypeCheckCallExpr::visit): for each call arg validate and bind
	(TypeCheckMethodCallExpr::check): likewise

gcc/testsuite/ChangeLog:

	* rust/compile/issue-4678.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
@philberty
philberty enabled auto-merge August 16, 2026 21:42
@philberty
philberty added this pull request to the merge queue Aug 16, 2026
Merged via the queue into master with commit fde042e Aug 16, 2026
13 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in libcore 1.49 Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Trait bounds on generic impl blocks are silently ignored (Missing E0277)

1 participant