Skip to content

Avoid inference constraints from opaque blanket impls - #160752

Merged
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
amirHdev:fix-opaque-blanket-guidance
Nov 24, 2026
Merged

rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
amirHdev:fix-opaque-blanket-guidance

Conversation

@amirHdev

@amirHdev amirHdev commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

partially addresses trait system refactor initiative issue 229

this avoids constraining unrelated trait arguments when matching blanket impls for opaque types that are not yet defined while still allowing inference from their bounds

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Aug 8, 2026
@amirHdev
amirHdev marked this pull request as ready for review August 9, 2026 11:07
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 9, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 9, 2026
@rustbot

rustbot commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 19 candidates

@nnethercote

Copy link
Copy Markdown
Contributor

r? @lcnr

@rustbot rustbot assigned lcnr and unassigned nnethercote Aug 9, 2026
@rust-bors

This comment has been minimized.

// Blanket impls here only guide inference while the opaque hidden type
// is unknown. Do not let constraints from matching the impl itself
// constrain other inference variables.
Ok(ecx.make_ambiguous_response_no_constraints(maybe))

@lcnr lcnr Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would also cause undesirable breakage, e.g I would like to use blanket impls for <impl Iterator<Item = u32> as IntoIterator>::Item. You should be able to construct a complete example from that by looking at the original PR/issue adding this hack for opaque types. I think that currently breaks with your PR

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now only reject the blanket impl if matching the impl itself constrains another trait argument and still allowing inference from the opaque bounds

@lcnr

lcnr commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 25, 2026
@rustbot

rustbot commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@amirHdev
amirHdev force-pushed the fix-opaque-blanket-guidance branch from 876b69c to 530b635 Compare August 28, 2026 12:49
@rustbot

This comment has been minimized.

@amirHdev

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 28, 2026
@amirHdev
amirHdev requested a review from lcnr August 28, 2026 12:55
.args
.iter()
.skip(1)
.all(|arg| ecx.resolve_vars_if_possible(arg) == arg)

@lcnr lcnr Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, also feeling unsure about that you could imagine

?opaque = impl Trait<Assoc = u32>

?opaque: OtherTrait<?u>

impl<T: Trait<Assoc = U>, U> OtherTrait<U> for T {}

sorry for not having much capacity to review this PR

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i was working on this pr meanwhile and yeah, that case works with the current check. I added it

{
// Blanket impls may guide inference for not-yet-defined opaques,
// but must not constrain the other trait arguments while matching
// the impl itself.

@lcnr lcnr Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, this does work, feels brittle however as it relies on us not proving any of the added goals in consider_impl_candidate before calling the then closure 🤔

you could change this to just be goal_trait_ref .args .iter() .all(|arg| ecx.resolve_vars_if_possible(arg) == arg)

without the self type special case. hmm

the scary thing is that this can easily break and is subtly wrong wrt normalizing the impl header as these goals also only get evaluated in evaluate_added_goals_and_make_canonical_response 🤔

I am unsure. Your PR feels less wrong than currently nightly behavior. Still feels kinda iffy to me though. Can you keep a comment here that this is brittle and probably not quite what we want still, probably just keep the FIXME(trait-system-refactor-initiative#229) as this setup is still just kinda jank and I dislike it 😅

I would like to maybe do a larger change in the future where we instead eagerly replace opaque types with Placeholders, but that's still far off and has issues itself

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the tricky part is allowing inference from the opaque's bounds without letting the blanket impl itself constrain other inference variables

the check covers matching the impl header but normalization and added goals can introduce constraints later

that's probably the distinction we need to get right for #229 🤔

@lcnr

lcnr commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 13, 2026
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 15, 2026
@amirHdev
amirHdev requested a review from lcnr October 15, 2026 17:04
@lcnr

lcnr commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@bors r+ delegate

can you manually rebase this PR and then approve it? unsure whether it'll cause merge conflicts given that it has been a while

r=me

@rust-bors

rust-bors Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 4498357 has been approved by lcnr

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 24, 2026
@lcnr

lcnr commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@bors delegate+

@rust-bors

rust-bors Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

✌️ @amirHdev, you can now approve this pull request!

If @lcnr told you to "r=me" after making some further change, then please make that change and post @bors r=lcnr.

View changes since this delegation.

@lcnr

lcnr commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@bors r-

@rustbot author

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Nov 24, 2026
@rust-bors

rust-bors Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved.

View changes since this unapproval

@rust-bors rust-bors Bot removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Nov 24, 2026
@amirHdev
amirHdev force-pushed the fix-opaque-blanket-guidance branch from 4498357 to e537e5c Compare November 24, 2026 12:41
@rustbot

rustbot commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@amirHdev

Copy link
Copy Markdown
Contributor Author

patch is unchanged, thanks!

@bors r=lcnr

@rust-bors

rust-bors Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

📌 Commit e537e5c has been approved by lcnr

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 24, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 24, 2026
…ce, r=lcnr

Avoid inference constraints from opaque blanket impls

partially addresses trait system refactor initiative issue 229

this avoids constraining unrelated trait arguments when matching blanket impls for opaque types that are not yet defined while still allowing inference from their bounds
rust-bors Bot pushed a commit that referenced this pull request Nov 24, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - #166340 (Revert "implement const Iterator for Range - #156216")
 - #165345 (Remove the 'eq', 'eq_trace', and 'goals_to_obligations' methods from 'At')
 - #165538 (Supertraits returns Unnormalized)
 - #165794 (`consider_impl_candidate` tweaks)
 - #160752 (Avoid inference constraints from opaque blanket impls)
 - #163187 (add `scalbn` and `ilogb` methods on floats)
 - #164541 (Add regression test for #152415)
 - #164543 (Add regression test for #151316)
 - #165920 (Introduce compute_abi helper and ComputeCallConv trait)
 - #166313 (Move `rustdoc-ui` tests in the right folder)
 - #166420 ([rustdoc] Fix search index generation: if crate content didn't change, it should generate the same search index)
 - #166485 (Allow passing `--exact-match` and `--purge` to `x perf`)
@rust-bors
rust-bors Bot merged commit 92bb0e6 into rust-lang:main Nov 24, 2026
14 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 24, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 24, 2026
Rollup merge of #160752 - amirHdev:fix-opaque-blanket-guidance, r=lcnr

Avoid inference constraints from opaque blanket impls

partially addresses trait system refactor initiative issue 229

this avoids constraining unrelated trait arguments when matching blanket impls for opaque types that are not yet defined while still allowing inference from their bounds
TDecking pushed a commit to TDecking/miri that referenced this pull request Nov 25, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - rust-lang/rust#166340 (Revert "implement const Iterator for Range - rust-lang/rust#156216")
 - rust-lang/rust#165345 (Remove the 'eq', 'eq_trace', and 'goals_to_obligations' methods from 'At')
 - rust-lang/rust#165538 (Supertraits returns Unnormalized)
 - rust-lang/rust#165794 (`consider_impl_candidate` tweaks)
 - rust-lang/rust#160752 (Avoid inference constraints from opaque blanket impls)
 - rust-lang/rust#163187 (add `scalbn` and `ilogb` methods on floats)
 - rust-lang/rust#164541 (Add regression test for rust-lang/rust#152415)
 - rust-lang/rust#164543 (Add regression test for rust-lang/rust#151316)
 - rust-lang/rust#165920 (Introduce compute_abi helper and ComputeCallConv trait)
 - rust-lang/rust#166313 (Move `rustdoc-ui` tests in the right folder)
 - rust-lang/rust#166420 ([rustdoc] Fix search index generation: if crate content didn't change, it should generate the same search index)
 - rust-lang/rust#166485 (Allow passing `--exact-match` and `--purge` to `x perf`)
mehmettoplu-rs pushed a commit to mehmettoplu-rs/rustc_codegen_gcc that referenced this pull request Nov 27, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - rust-lang/rust#166340 (Revert "implement const Iterator for Range - rust-lang/rust#156216")
 - rust-lang/rust#165345 (Remove the 'eq', 'eq_trace', and 'goals_to_obligations' methods from 'At')
 - rust-lang/rust#165538 (Supertraits returns Unnormalized)
 - rust-lang/rust#165794 (`consider_impl_candidate` tweaks)
 - rust-lang/rust#160752 (Avoid inference constraints from opaque blanket impls)
 - rust-lang/rust#163187 (add `scalbn` and `ilogb` methods on floats)
 - rust-lang/rust#164541 (Add regression test for rust-lang/rust#152415)
 - rust-lang/rust#164543 (Add regression test for rust-lang/rust#151316)
 - rust-lang/rust#165920 (Introduce compute_abi helper and ComputeCallConv trait)
 - rust-lang/rust#166313 (Move `rustdoc-ui` tests in the right folder)
 - rust-lang/rust#166420 ([rustdoc] Fix search index generation: if crate content didn't change, it should generate the same search index)
 - rust-lang/rust#166485 (Allow passing `--exact-match` and `--purge` to `x perf`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants