Skip to content

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

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

Merged
merged 1 commit into from
Apr 8, 2025

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 <[email protected]>
@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
@compiler-errors
Copy link
Member

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Apr 7, 2025

📌 Commit fdefffe has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors 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 Apr 7, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 7, 2025
…er-errors

compiler: report error when trait object type param reference self

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.

r? `@compiler-errors` `@fee1-dead`
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 7, 2025
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#139124 (compiler: report error when trait object type param reference self)
 - rust-lang#139346 (Don't construct preds w escaping bound vars in `diagnostic_hir_wf_check`)
 - rust-lang#139379 (Use delayed bug for normalization errors in drop elaboration)
 - rust-lang#139421 (Fix trait upcasting to dyn type with no principal when there are projections)
 - rust-lang#139468 (Don't call `Span::with_parent` on the good path in `has_stashed_diagnostic`)
 - rust-lang#139476 (rm `RegionInferenceContext::var_infos`)
 - rust-lang#139490 (Update some comment/docs related to "extern intrinsic" removal)

r? `@ghost`
`@rustbot` modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 7, 2025
…er-errors

compiler: report error when trait object type param reference self

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.

r? ``@compiler-errors`` ``@fee1-dead``
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 8, 2025
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#139124 (compiler: report error when trait object type param reference self)
 - rust-lang#139346 (Don't construct preds w escaping bound vars in `diagnostic_hir_wf_check`)
 - rust-lang#139421 (Fix trait upcasting to dyn type with no principal when there are projections)
 - rust-lang#139468 (Don't call `Span::with_parent` on the good path in `has_stashed_diagnostic`)
 - rust-lang#139476 (rm `RegionInferenceContext::var_infos`)
 - rust-lang#139490 (Update some comment/docs related to "extern intrinsic" removal)

r? `@ghost`
`@rustbot` modify labels: rollup
Zalathar added a commit to Zalathar/rust that referenced this pull request Apr 8, 2025
…er-errors

compiler: report error when trait object type param reference self

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.

r? ```@compiler-errors``` ```@fee1-dead```
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 8, 2025
…errors

Rollup of 19 pull requests

Successful merges:

 - rust-lang#138676 (Implement overflow for infinite implied lifetime bounds)
 - rust-lang#139024 (Make error message for missing fields with `..` and without `..` more consistent)
 - rust-lang#139098 (Tell LLVM about impossible niche tags)
 - rust-lang#139124 (compiler: report error when trait object type param reference self)
 - rust-lang#139321 (Update to new rinja version (askama))
 - rust-lang#139346 (Don't construct preds w escaping bound vars in `diagnostic_hir_wf_check`)
 - rust-lang#139386 (make it possible to use stage0 libtest on compiletest)
 - rust-lang#139421 (Fix trait upcasting to dyn type with no principal when there are projections)
 - rust-lang#139468 (Don't call `Span::with_parent` on the good path in `has_stashed_diagnostic`)
 - rust-lang#139476 (rm `RegionInferenceContext::var_infos`)
 - rust-lang#139481 (Add job summary links to post-merge report)
 - rust-lang#139485 (compiletest: Stricter parsing for diagnostic kinds)
 - rust-lang#139490 (Update some comment/docs related to "extern intrinsic" removal)
 - rust-lang#139491 (Update books)
 - rust-lang#139496 (Revert r-a changes of rust-lang#139455)
 - rust-lang#139500 (document panic behavior of Vec::resize and Vec::resize_with)
 - rust-lang#139501 (Fix stack overflow in exhaustiveness due to recursive HIR opaque hidden types)
 - rust-lang#139504 (add missing word in doc comment)
 - rust-lang#139507 (compiletest: Trim whitespace from environment variable names)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 8, 2025
Rollup of 10 pull requests

Successful merges:

 - rust-lang#138676 (Implement overflow for infinite implied lifetime bounds)
 - rust-lang#139024 (Make error message for missing fields with `..` and without `..` more consistent)
 - rust-lang#139098 (Tell LLVM about impossible niche tags)
 - rust-lang#139124 (compiler: report error when trait object type param reference self)
 - rust-lang#139321 (Update to new rinja version (askama))
 - rust-lang#139346 (Don't construct preds w escaping bound vars in `diagnostic_hir_wf_check`)
 - rust-lang#139386 (make it possible to use stage0 libtest on compiletest)
 - rust-lang#139421 (Fix trait upcasting to dyn type with no principal when there are projections)
 - rust-lang#139464 (Allow for reparsing failure when reparsing a pasted metavar.)
 - rust-lang#139490 (Update some comment/docs related to "extern intrinsic" removal)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 5913c52 into rust-lang:master Apr 8, 2025
6 checks passed
@rustbot rustbot added this to the 1.88.0 milestone Apr 8, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 8, 2025
Rollup merge of rust-lang#139124 - xtexx:rust-langgh-139082, r=compiler-errors

compiler: report error when trait object type param reference self

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.

r? ````@compiler-errors```` ````@fee1-dead````
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICE: not using infcx
4 participants