-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ use rustc_middle::bug; | |
use rustc_middle::ty::print::{PrettyPrinter, Print, PrintError, Printer}; | ||
use rustc_middle::ty::{ | ||
self, GenericArg, GenericArgKind, Instance, ReifyReason, Ty, TyCtxt, TypeVisitableExt, | ||
TypingEnv, | ||
}; | ||
use tracing::debug; | ||
|
||
|
@@ -383,14 +384,26 @@ impl<'tcx> Printer<'tcx> for SymbolPrinter<'tcx> { | |
&mut self, | ||
impl_def_id: DefId, | ||
args: &'tcx [GenericArg<'tcx>], | ||
mut self_ty: Ty<'tcx>, | ||
mut impl_trait_ref: Option<ty::TraitRef<'tcx>>, | ||
) -> Result<(), PrintError> { | ||
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); | ||
} | ||
let self_ty = self.tcx.type_of(impl_def_id); | ||
let impl_trait_ref = self.tcx.impl_trait_ref(impl_def_id); | ||
let (typing_env, mut self_ty, mut impl_trait_ref) = | ||
if self.tcx.generics_of(impl_def_id).count() <= args.len() { | ||
( | ||
TypingEnv::fully_monomorphized(), | ||
self_ty.instantiate(self.tcx, args), | ||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
When mangling I'm kinda of the opinion that we should print outer item def paths as plain old |
||
// Use the identity substitutions for the impl. We also need | ||
// a well-formed param-env, so let's use post-analysis. | ||
( | ||
TypingEnv::post_analysis(self.tcx, impl_def_id), | ||
self_ty.instantiate_identity(), | ||
impl_trait_ref.map(|impl_trait_ref| impl_trait_ref.instantiate_identity()), | ||
) | ||
}; | ||
|
||
match &mut impl_trait_ref { | ||
Some(impl_trait_ref) => { | ||
|
@@ -403,7 +416,7 @@ impl<'tcx> Printer<'tcx> for SymbolPrinter<'tcx> { | |
} | ||
} | ||
|
||
self.default_print_impl_path(impl_def_id, args, self_ty, impl_trait_ref) | ||
self.default_print_impl_path(impl_def_id, self_ty, impl_trait_ref) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//@ revisions: legacy v0 | ||
//@[v0] compile-flags: -C symbol-mangling-version=v0 | ||
//@[legacy] compile-flags: -C symbol-mangling-version=legacy -Zunstable-options | ||
//@ build-pass | ||
|
||
pub struct Vec2; | ||
|
||
pub trait Point { | ||
type S; | ||
} | ||
impl Point for Vec2 { | ||
type S = f32; | ||
} | ||
|
||
pub trait Point2: Point<S = Self::S2> { | ||
type S2; | ||
} | ||
impl Point2 for Vec2 { | ||
type S2 = Self::S; | ||
} | ||
|
||
trait MyFrom<T> { | ||
fn my_from(); | ||
} | ||
impl<P: Point2> MyFrom<P::S> for P { | ||
fn my_from() { | ||
// This is just a really dumb way to force the legacy symbol mangling to | ||
// mangle the closure's parent impl def path *with* args. Otherwise, | ||
// legacy symbol mangling will strip the args from the instance, meaning | ||
// that we don't trigger the bug. | ||
let c = || {}; | ||
let x = Box::new(c) as Box<dyn Fn()>; | ||
} | ||
} | ||
|
||
fn main() { | ||
<Vec2 as MyFrom<_>>::my_from(); | ||
} |
There was a problem hiding this comment.
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.