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

Use a post-monomorphization typing env when mangling components that come from impls #135149

Merged
merged 2 commits into from
Jan 8, 2025

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Jan 6, 2025

When mangling associated methods of impls, we were previously using the wrong param-env. Instead of using a fully monomorphized param-env like we usually do in codegen, we were taking the post-analysis param-env, and treating it as an early binder to re-substitute the impl args. I've pointed out the problematic old code in an inline comment.

This would give us param-envs with possibly trivial predicates that would prevent normalization via param-env shadowing.

In the example test linked below, tests/ui/symbol-names/normalize-in-param-env.rs, this happens when we mangle the impl impl<P: Point2> MyFrom<P::S> for P with the substitution P = Vec2. Because the where clause of the impl is P: Point2, which elaborates to [P: Point2, P: Point, <P as Point>::S projects-to <P as Point2>::S2] and the fact that impl Point2 for Vec2 normalizes Vec2::S2 to Vec2::S, this causes a cycle.

The proper fix here is to use a fully monomorphized param-env for the case where the impl is properly substituted.

Fixes #135143

While #134081 uncovered this bug for legacy symbol mangling, it was preexisting for v0 symbol mangling. This PR fixes both. The test requires a "hack" because we strip the args of the instance we're printing for legacy symbol mangling except for drop glue, so we box a closure to ensure we generate drop glue.

r? oli-obk

@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 Jan 6, 2025
impl_trait_ref.map(|impl_trait_ref| impl_trait_ref.instantiate(self.tcx, args)),
)
} else {
// We are probably printing a nested item inside of an impl.
Copy link
Member Author

Choose a reason for hiding this comment

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

This happens when we try to mangle a nested function inside of an impl, like:

impl<T> Foo for T {
  fn outer() {
    fn inner() {}
  }
}

When mangling inner, we try to print it like <T as Foo>::outer::inner, however since inner doesn't inherit the substitutions from outer, we give it fresh identity substs.

I'm kinda of the opinion that we should print outer item def paths as plain old crate::{impl#0}::outer::inner rather than printing with the trait ref. I think that would break things tho 🤷

let mut typing_env = ty::TypingEnv::post_analysis(self.tcx, impl_def_id);
if !args.is_empty() {
typing_env.param_env =
ty::EarlyBinder::bind(typing_env.param_env).instantiate(self.tcx, args);
Copy link
Member Author

Choose a reason for hiding this comment

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

This was the problematic substutiton.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 7, 2025

@bors r+

@bors
Copy link
Contributor

bors commented Jan 7, 2025

📌 Commit 86d8b79 has been approved by oli-obk

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 Jan 7, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 7, 2025
Use a post-monomorphization typing env when mangling components that come from impls

When mangling associated methods of impls, we were previously using the wrong param-env. Instead of using a fully monomorphized param-env like we usually do in codegen, we were taking the post-analysis param-env, and treating it as an early binder to *re-substitute* the impl args. I've pointed out the problematic old code in an inline comment.

This would give us param-envs with possibly trivial predicates that would prevent normalization via param-env shadowing.

In the example test linked below, `tests/ui/symbol-names/normalize-in-param-env.rs`, this happens when we mangle the impl `impl<P: Point2> MyFrom<P::S> for P` with the substitution `P = Vec2`. Because the where clause of the impl is `P: Point2`, which elaborates to `[P: Point2, P: Point, <P as Point>::S projects-to <P as Point2>::S2]` and the fact that `impl Point2 for Vec2` normalizes `Vec2::S2` to `Vec2::S`, this causes a cycle.

The proper fix here is to use a fully monomorphized param-env for the case where the impl is properly substituted.

Fixes rust-lang#135143

While rust-lang#134081 uncovered this bug for legacy symbol mangling, it was preexisting for v0 symbol mangling. This PR fixes both. The test requires a "hack" because we strip the args of the instance we're printing for legacy symbol mangling except for drop glue, so we box a closure to ensure we generate drop glue.

r? oli-obk
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 7, 2025
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#131146 (Stop clearing box's drop flags early)
 - rust-lang#133810 (remove unnecessary `eval_verify_bound`)
 - rust-lang#134745 (Normalize each signature input/output in `typeck_with_fallback` with its own span)
 - rust-lang#134989 (Lower Guard Patterns to HIR.)
 - rust-lang#135149 (Use a post-monomorphization typing env when mangling components that come from impls)
 - rust-lang#135171 (rustdoc: use stable paths as preferred canonical paths)
 - rust-lang#135200 (rustfmt: drop nightly-gating of the `--style-edition` flag registration)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 7, 2025
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#133810 (remove unnecessary `eval_verify_bound`)
 - rust-lang#134745 (Normalize each signature input/output in `typeck_with_fallback` with its own span)
 - rust-lang#134989 (Lower Guard Patterns to HIR.)
 - rust-lang#135149 (Use a post-monomorphization typing env when mangling components that come from impls)
 - rust-lang#135171 (rustdoc: use stable paths as preferred canonical paths)
 - rust-lang#135200 (rustfmt: drop nightly-gating of the `--style-edition` flag registration)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 3e12d4d into rust-lang:master Jan 8, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 8, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 8, 2025
Rollup merge of rust-lang#135149 - compiler-errors:mangle, r=oli-obk

Use a post-monomorphization typing env when mangling components that come from impls

When mangling associated methods of impls, we were previously using the wrong param-env. Instead of using a fully monomorphized param-env like we usually do in codegen, we were taking the post-analysis param-env, and treating it as an early binder to *re-substitute* the impl args. I've pointed out the problematic old code in an inline comment.

This would give us param-envs with possibly trivial predicates that would prevent normalization via param-env shadowing.

In the example test linked below, `tests/ui/symbol-names/normalize-in-param-env.rs`, this happens when we mangle the impl `impl<P: Point2> MyFrom<P::S> for P` with the substitution `P = Vec2`. Because the where clause of the impl is `P: Point2`, which elaborates to `[P: Point2, P: Point, <P as Point>::S projects-to <P as Point2>::S2]` and the fact that `impl Point2 for Vec2` normalizes `Vec2::S2` to `Vec2::S`, this causes a cycle.

The proper fix here is to use a fully monomorphized param-env for the case where the impl is properly substituted.

Fixes rust-lang#135143

While rust-lang#134081 uncovered this bug for legacy symbol mangling, it was preexisting for v0 symbol mangling. This PR fixes both. The test requires a "hack" because we strip the args of the instance we're printing for legacy symbol mangling except for drop glue, so we box a closure to ensure we generate drop glue.

r? oli-obk
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.

Overflow evaluating requirement in latest nightly (1.85)
4 participants