Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiler: report error when trait object type param reference self #139124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

xtexx
Copy link
Contributor

@xtexx xtexx commented Mar 30, 2025

Fixes #139082.

Emits an error when Self is found in the projection bounds of a trait
object. In type aliases, Self has no meaning, so type A = &'static dyn B where trait B = Fn() -> Self will expands to type A = &'static Fn() -> Self which is illegal, causing the region solver to bail out
when hitting the uninferred Self.

r? @compiler-errors @fee1-dead

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 30, 2025
@rustbot
Copy link
Collaborator

rustbot commented Mar 30, 2025

HIR ty lowering was modified

cc @fmease

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

Please add a UI test.

Also, there's no explanation for why this fixes the issue? Specifically, I'm not convinced this is the right fix for the issue yet, since it doesn't really explain why this doesn't ICE for small permutations of the original reproducer like:

trait B<C> = Fn() -> Self;
type D<'a> = Vec<&'a dyn B<()>>;

And why this ends up fixing an ICE in the region solving code.

@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 Mar 30, 2025
Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

I thought more about this, and I think this may be the right fix, though currently it duplicates some code. Please add a test still, and it would be nice to add some sort of explanation about why this is necessary in the PR.

@xtexx
Copy link
Contributor Author

xtexx commented Mar 30, 2025

type D<'a> = Vec<&'a dyn B<()>>; is not a lazy type alias thus does not trigger the well-formed check (

if tcx.type_alias_is_lazy(item.owner_id) =>
, wfcheck::check_item -> check_where_clauses -> implicit infer, infer_predicates)

However, when the type D is actually used, it leads to a ICE, as the type inference has been called.

trait B<C> = Fn() -> Self;
type D = &'static dyn B<()>;

fn main() {
    let a = D::new();
    _ = a;
}

(note that, the ICE from the above code is not the one in the issue but another one, compiler/rustc_infer/src/infer/canonical/canonicalizer.rs:404:17: encountered a fresh type during canonicalization, but they are both due to not-inferred types passed down)

( I am not sure if Self here should be allowed or rejected. )

@xtexx xtexx force-pushed the gh-139082 branch 2 times, most recently from 53a3f72 to 0580faa Compare March 30, 2025 04:33
@xtexx
Copy link
Contributor Author

xtexx commented Mar 30, 2025

@rustbot review

@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 Mar 30, 2025
@compiler-errors
Copy link
Member

One last requested change, sorry for the back and forth

@rustbot author

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 1, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 1, 2025

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

@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 Apr 1, 2025
Fixes rust-lang#139082.

Emits an error when `Self` is found in the projection bounds of a trait
object. In type aliases, `Self` has no meaning, so `type A = &'static
dyn B` where `trait B = Fn() -> Self` will expands to `type A = &'static
Fn() -> Self` which is illegal, causing the region solver to bail out
when hitting the uninferred Self.

Bug: rust-lang#139082
Signed-off-by: xtex <xtexchooser@duck.com>
@xtexx
Copy link
Contributor Author

xtexx commented Apr 3, 2025

@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 Apr 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICE: not using infcx
3 participants