diff options
| author | Eduard-Mihai Burtescu <edy.burt@gmail.com> | 2019-01-29 07:21:11 +0200 |
|---|---|---|
| committer | Eduard-Mihai Burtescu <edy.burt@gmail.com> | 2019-03-15 13:25:10 +0200 |
| commit | 8619edede1496a8d9c4131f9cb2079e71dccd5fb (patch) | |
| tree | 912e6fb69b376cb1132947a95ccf9ea0cf52580f | |
| parent | 9df7c3f48fd4bb20da04624d19c8c1465d1117dc (diff) | |
| download | rust-8619edede1496a8d9c4131f9cb2079e71dccd5fb.tar.gz rust-8619edede1496a8d9c4131f9cb2079e71dccd5fb.zip | |
rustc: slice substs in ty::print instead of passing the full ones.
| -rw-r--r-- | src/librustc/infer/error_reporting/mod.rs | 2 | ||||
| -rw-r--r-- | src/librustc/mir/mod.rs | 2 | ||||
| -rw-r--r-- | src/librustc/ty/instance.rs | 2 | ||||
| -rw-r--r-- | src/librustc/ty/print/mod.rs | 96 | ||||
| -rw-r--r-- | src/librustc/ty/print/pretty.rs | 49 | ||||
| -rw-r--r-- | src/librustc/ty/structural_impls.rs | 4 | ||||
| -rw-r--r-- | src/librustc_codegen_utils/symbol_names.rs | 4 | ||||
| -rw-r--r-- | src/librustc_mir/borrow_check/mod.rs | 2 | ||||
| -rw-r--r-- | src/librustdoc/clean/mod.rs | 2 | ||||
| -rw-r--r-- | src/test/ui/namespace/namespace-mix.stderr | 4 |
10 files changed, 79 insertions, 88 deletions
diff --git a/src/librustc/infer/error_reporting/mod.rs b/src/librustc/infer/error_reporting/mod.rs index 52b42881038..5026074f5e8 100644 --- a/src/librustc/infer/error_reporting/mod.rs +++ b/src/librustc/infer/error_reporting/mod.rs @@ -533,7 +533,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { if !(did1.is_local() || did2.is_local()) && did1.krate != did2.krate { let abs_path = |def_id| { AbsolutePathPrinter { tcx: self.tcx } - .print_def_path(def_id, None) + .print_def_path(def_id, &[]) }; // We compare strings because DefPath can be different diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index 3218f8ea5de..718b506d051 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -2410,7 +2410,7 @@ impl<'tcx> Debug for Rvalue<'tcx> { ty::tls::with(|tcx| { let substs = tcx.lift(&substs).expect("could not lift for printing"); FmtPrinter::new(tcx, f, Namespace::ValueNS) - .print_def_path(variant_def.did, Some(substs))?; + .print_def_path(variant_def.did, substs)?; Ok(()) })?; diff --git a/src/librustc/ty/instance.rs b/src/librustc/ty/instance.rs index 89d956c8bfa..995e85fc5f4 100644 --- a/src/librustc/ty/instance.rs +++ b/src/librustc/ty/instance.rs @@ -179,7 +179,7 @@ impl<'tcx> fmt::Display for Instance<'tcx> { ty::tls::with(|tcx| { let substs = tcx.lift(&self.substs).expect("could not lift for printing"); FmtPrinter::new(tcx, &mut *f, Namespace::ValueNS) - .print_def_path(self.def_id(), Some(substs))?; + .print_def_path(self.def_id(), substs)?; Ok(()) })?; diff --git a/src/librustc/ty/print/mod.rs b/src/librustc/ty/print/mod.rs index ad17a8114ca..c3f4a26a57e 100644 --- a/src/librustc/ty/print/mod.rs +++ b/src/librustc/ty/print/mod.rs @@ -1,7 +1,7 @@ use crate::hir::map::DefPathData; use crate::hir::def_id::{CrateNum, DefId}; use crate::ty::{self, DefIdTree, Ty, TyCtxt}; -use crate::ty::subst::{Kind, Subst, SubstsRef}; +use crate::ty::subst::{Kind, Subst}; use rustc_data_structures::fx::FxHashSet; @@ -29,14 +29,14 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized { fn print_def_path( self, def_id: DefId, - substs: Option<SubstsRef<'tcx>>, + substs: &'tcx [Kind<'tcx>], ) -> Result<Self::Path, Self::Error> { self.default_print_def_path(def_id, substs) } fn print_impl_path( self, impl_def_id: DefId, - substs: Option<SubstsRef<'tcx>>, + substs: &'tcx [Kind<'tcx>], self_ty: Ty<'tcx>, trait_ref: Option<ty::TraitRef<'tcx>>, ) -> Result<Self::Path, Self::Error> { @@ -90,7 +90,7 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized { fn default_print_def_path( self, def_id: DefId, - substs: Option<SubstsRef<'tcx>>, + substs: &'tcx [Kind<'tcx>], ) -> Result<Self::Path, Self::Error> { debug!("default_print_def_path: def_id={:?}, substs={:?}", def_id, substs); let key = self.tcx().def_key(def_id); @@ -103,61 +103,61 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized { } DefPathData::Impl => { + let generics = self.tcx().generics_of(def_id); let mut self_ty = self.tcx().type_of(def_id); - if let Some(substs) = substs { - self_ty = self_ty.subst(self.tcx(), substs); - } - let mut impl_trait_ref = self.tcx().impl_trait_ref(def_id); - if let Some(substs) = substs { + if substs.len() >= generics.count() { + self_ty = self_ty.subst(self.tcx(), substs); impl_trait_ref = impl_trait_ref.subst(self.tcx(), substs); } self.print_impl_path(def_id, substs, self_ty, impl_trait_ref) } _ => { - let generics = substs.map(|_| self.tcx().generics_of(def_id)); - let generics_parent = generics.as_ref().and_then(|g| g.parent); let parent_def_id = DefId { index: key.parent.unwrap(), ..def_id }; - let print_parent_path = |cx: Self| { - if let Some(generics_parent_def_id) = generics_parent { - assert_eq!(parent_def_id, generics_parent_def_id); - - // FIXME(eddyb) try to move this into the parent's printing - // logic, instead of doing it when printing the child. - let parent_generics = cx.tcx().generics_of(parent_def_id); - let parent_has_own_self = - parent_generics.has_self && parent_generics.parent_count == 0; - if let (Some(substs), true) = (substs, parent_has_own_self) { - let trait_ref = ty::TraitRef::new(parent_def_id, substs); - cx.path_qualified(trait_ref.self_ty(), Some(trait_ref)) - } else { - cx.print_def_path(parent_def_id, substs) - } - } else { - cx.print_def_path(parent_def_id, None) - } - }; - let print_path = |cx: Self| { + + let mut parent_substs = substs; + let mut trait_qualify_parent = false; + if !substs.is_empty() { + let generics = self.tcx().generics_of(def_id); + parent_substs = &substs[..generics.parent_count.min(substs.len())]; + match key.disambiguated_data.data { - // Skip `::{{constructor}}` on tuple/unit structs. - DefPathData::StructCtor => print_parent_path(cx), - - _ => { - cx.path_append( - print_parent_path, - &key.disambiguated_data.data.as_interned_str().as_str(), - ) + // Closures' own generics are only captures, don't print them. + DefPathData::ClosureExpr => {} + + // If we have any generic arguments to print, we do that + // on top of the same path, but without its own generics. + _ => if !generics.params.is_empty() && substs.len() >= generics.count() { + let args = self.generic_args_to_print(generics, substs); + return self.path_generic_args( + |cx| cx.print_def_path(def_id, parent_substs), + args, + ); } } - }; - if let (Some(generics), Some(substs)) = (generics, substs) { - let args = self.generic_args_to_print(generics, substs); - self.path_generic_args(print_path, args) - } else { - print_path(self) + // FIXME(eddyb) try to move this into the parent's printing + // logic, instead of doing it when printing the child. + trait_qualify_parent = + generics.has_self && + generics.parent == Some(parent_def_id) && + parent_substs.len() == generics.parent_count && + self.tcx().generics_of(parent_def_id).parent_count == 0; } + + self.path_append( + |cx: Self| if trait_qualify_parent { + let trait_ref = ty::TraitRef::new( + parent_def_id, + cx.tcx().intern_substs(parent_substs), + ); + cx.path_qualified(trait_ref.self_ty(), Some(trait_ref)) + } else { + cx.print_def_path(parent_def_id, parent_substs) + }, + &key.disambiguated_data.data.as_interned_str().as_str(), + ) } } } @@ -165,7 +165,7 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized { fn generic_args_to_print( &self, generics: &'tcx ty::Generics, - substs: SubstsRef<'tcx>, + substs: &'tcx [Kind<'tcx>], ) -> &'tcx [Kind<'tcx>] { let mut own_params = generics.parent_count..generics.count(); @@ -193,7 +193,7 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized { fn default_print_impl_path( self, impl_def_id: DefId, - _substs: Option<SubstsRef<'tcx>>, + _substs: &'tcx [Kind<'tcx>], self_ty: Ty<'tcx>, impl_trait_ref: Option<ty::TraitRef<'tcx>>, ) -> Result<Self::Path, Self::Error> { @@ -220,7 +220,7 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized { // trait-type, then fallback to a format that identifies // the module more clearly. self.path_append_impl( - |cx| cx.print_def_path(parent_def_id, None), + |cx| cx.print_def_path(parent_def_id, &[]), self_ty, impl_trait_ref, ) diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs index bc98aee233a..54ca63b457a 100644 --- a/src/librustc/ty/print/pretty.rs +++ b/src/librustc/ty/print/pretty.rs @@ -5,7 +5,7 @@ use crate::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; use crate::middle::cstore::{ExternCrate, ExternCrateSource}; use crate::middle::region; use crate::ty::{self, DefIdTree, ParamConst, Ty, TyCtxt, TypeFoldable}; -use crate::ty::subst::{Kind, Subst, SubstsRef, UnpackedKind}; +use crate::ty::subst::{Kind, Subst, UnpackedKind}; use crate::mir::interpret::ConstValue; use syntax::symbol::{keywords, Symbol}; @@ -178,7 +178,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>: fn print_value_path( self, def_id: DefId, - substs: Option<SubstsRef<'tcx>>, + substs: &'tcx [Kind<'tcx>], ) -> Result<Self::Path, Self::Error> { self.print_def_path(def_id, substs) } @@ -264,7 +264,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>: }) => { debug!("try_print_visible_def_path: def_id={:?}", def_id); return Ok((if !span.is_dummy() { - self.print_def_path(def_id, None)? + self.print_def_path(def_id, &[])? } else { self.path_crate(cnum)? }, true)); @@ -469,8 +469,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>: } ty::FnDef(def_id, substs) => { let sig = self.tcx().fn_sig(def_id).subst(self.tcx(), substs); - p!(print(sig), - write(" {{"), print_value_path(def_id, Some(substs)), write("}}")); + p!(print(sig), write(" {{"), print_value_path(def_id, substs), write("}}")); } ty::FnPtr(ref bare_fn) => { p!(print(bare_fn)) @@ -492,7 +491,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>: } } ty::Adt(def, substs) => { - p!(print_def_path(def.did, Some(substs))); + p!(print_def_path(def.did, substs)); } ty::Dynamic(data, r) => { let print_r = self.region_should_not_be_omitted(r); @@ -505,7 +504,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>: } } ty::Foreign(def_id) => { - p!(print_def_path(def_id, None)); + p!(print_def_path(def_id, &[])); } ty::Projection(ref data) => p!(print(data)), ty::UnnormalizedProjection(ref data) => { @@ -691,7 +690,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>: let mut first = true; if let Some(principal) = predicates.principal() { - p!(print_def_path(principal.def_id, None)); + p!(print_def_path(principal.def_id, &[])); let mut resugared = false; @@ -774,7 +773,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>: } first = false; - p!(print_def_path(def_id, None)); + p!(print_def_path(def_id, &[])); } Ok(self) @@ -879,7 +878,7 @@ impl TyCtxt<'_, '_, '_> { debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns); let mut s = String::new(); let _ = FmtPrinter::new(self, &mut s, ns) - .print_def_path(def_id, None); + .print_def_path(def_id, &[]); s } } @@ -905,21 +904,13 @@ impl<F: fmt::Write> Printer<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> { fn print_def_path( mut self, def_id: DefId, - substs: Option<SubstsRef<'tcx>>, + substs: &'tcx [Kind<'tcx>], ) -> Result<Self::Path, Self::Error> { define_scoped_cx!(self); - // FIXME(eddyb) avoid querying `tcx.generics_of` and `tcx.def_key` - // both here and in `default_print_def_path`. - let generics = substs.map(|_| self.tcx.generics_of(def_id)); - if generics.as_ref().and_then(|g| g.parent).is_none() { + if substs.is_empty() { match self.try_print_visible_def_path(def_id)? { - (cx, true) => return if let (Some(generics), Some(substs)) = (generics, substs) { - let args = cx.generic_args_to_print(generics, substs); - cx.path_generic_args(Ok, args) - } else { - Ok(cx) - }, + (cx, true) => return Ok(cx), (cx, false) => self = cx, } } @@ -942,7 +933,7 @@ impl<F: fmt::Write> Printer<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> { let parent_def_id = DefId { index: key.parent.unwrap(), ..def_id }; let span = self.tcx.def_span(def_id); return self.path_append( - |cx| cx.print_def_path(parent_def_id, None), + |cx| cx.print_def_path(parent_def_id, &[]), &format!("<impl at {:?}>", span), ); } @@ -1073,7 +1064,7 @@ impl<F: fmt::Write> PrettyPrinter<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> fn print_value_path( mut self, def_id: DefId, - substs: Option<SubstsRef<'tcx>>, + substs: &'tcx [Kind<'tcx>], ) -> Result<Self::Path, Self::Error> { let was_in_value = std::mem::replace(&mut self.in_value, true); self = self.print_def_path(def_id, substs)?; @@ -1476,7 +1467,7 @@ define_print_and_forward_display! { ty::ExistentialPredicate::Trait(x) => p!(print(x)), ty::ExistentialPredicate::Projection(x) => p!(print(x)), ty::ExistentialPredicate::AutoTrait(def_id) => { - p!(print_def_path(def_id, None)); + p!(print_def_path(def_id, &[])); } } } @@ -1509,7 +1500,7 @@ define_print_and_forward_display! { } ty::TraitRef<'tcx> { - p!(print_def_path(self.def_id, Some(self.substs))); + p!(print_def_path(self.def_id, self.substs)); } ConstValue<'tcx> { @@ -1553,7 +1544,7 @@ define_print_and_forward_display! { } ty::ProjectionTy<'tcx> { - p!(print_def_path(self.item_def_id, Some(self.substs))); + p!(print_def_path(self.item_def_id, self.substs)); } ty::ClosureKind { @@ -1574,17 +1565,17 @@ define_print_and_forward_display! { ty::Predicate::WellFormed(ty) => p!(print(ty), write(" well-formed")), ty::Predicate::ObjectSafe(trait_def_id) => { p!(write("the trait `"), - print_def_path(trait_def_id, None), + print_def_path(trait_def_id, &[]), write("` is object-safe")) } ty::Predicate::ClosureKind(closure_def_id, _closure_substs, kind) => { p!(write("the closure `"), - print_value_path(closure_def_id, None), + print_value_path(closure_def_id, &[]), write("` implements the trait `{}`", kind)) } ty::Predicate::ConstEvaluatable(def_id, substs) => { p!(write("the constant `"), - print_value_path(def_id, Some(substs)), + print_value_path(def_id, substs), write("` can be evaluated")) } } diff --git a/src/librustc/ty/structural_impls.rs b/src/librustc/ty/structural_impls.rs index 4cbb315cefa..6aabdf1e566 100644 --- a/src/librustc/ty/structural_impls.rs +++ b/src/librustc/ty/structural_impls.rs @@ -36,7 +36,7 @@ impl fmt::Debug for ty::TraitDef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { ty::tls::with(|tcx| { FmtPrinter::new(tcx, f, Namespace::TypeNS) - .print_def_path(self.def_id, None)?; + .print_def_path(self.def_id, &[])?; Ok(()) }) } @@ -46,7 +46,7 @@ impl fmt::Debug for ty::AdtDef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { ty::tls::with(|tcx| { FmtPrinter::new(tcx, f, Namespace::TypeNS) - .print_def_path(self.did, None)?; + .print_def_path(self.did, &[])?; Ok(()) }) } diff --git a/src/librustc_codegen_utils/symbol_names.rs b/src/librustc_codegen_utils/symbol_names.rs index d1fcc88e599..262ba8a1c74 100644 --- a/src/librustc_codegen_utils/symbol_names.rs +++ b/src/librustc_codegen_utils/symbol_names.rs @@ -227,7 +227,7 @@ fn def_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> ty:: tcx, path: SymbolPath::new(), keep_within_component: false, - }.print_def_path(def_id, None).unwrap().path.into_interned() + }.print_def_path(def_id, &[]).unwrap().path.into_interned() } fn symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance<'tcx>) -> ty::SymbolName { @@ -438,7 +438,7 @@ impl Printer<'tcx, 'tcx> for SymbolPrinter<'_, 'tcx> { ty::UnnormalizedProjection(ty::ProjectionTy { item_def_id: def_id, substs }) | ty::Closure(def_id, ty::ClosureSubsts { substs }) | ty::Generator(def_id, ty::GeneratorSubsts { substs }, _) => { - self.print_def_path(def_id, Some(substs)) + self.print_def_path(def_id, substs) } _ => self.pretty_print_type(ty), } diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs index 0f0811b562e..c4e371d5afe 100644 --- a/src/librustc_mir/borrow_check/mod.rs +++ b/src/librustc_mir/borrow_check/mod.rs @@ -12,7 +12,7 @@ use rustc::mir::{ClearCrossCrate, Local, Location, Mir, Mutability, Operand, Pla use rustc::mir::{Field, Projection, ProjectionElem, Rvalue, Statement, StatementKind}; use rustc::mir::{Terminator, TerminatorKind}; use rustc::ty::query::Providers; -use rustc::ty::{self, DefIdTree, TyCtxt}; +use rustc::ty::{self, TyCtxt}; use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, Level}; use rustc_data_structures::bit_set::BitSet; diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 53087eb1b1a..da73eb0cda5 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -4322,7 +4322,7 @@ pub fn get_path_for_type( } let names = AbsolutePathPrinter { tcx: tcx.global_tcx() } - .print_def_path(def_id, None) + .print_def_path(def_id, &[]) .unwrap(); hir::Path { diff --git a/src/test/ui/namespace/namespace-mix.stderr b/src/test/ui/namespace/namespace-mix.stderr index 22c871dd3c0..99fa3b96794 100644 --- a/src/test/ui/namespace/namespace-mix.stderr +++ b/src/test/ui/namespace/namespace-mix.stderr @@ -462,11 +462,11 @@ note: required by `check` LL | fn check<T: Impossible>(_: T) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error[E0277]: the trait bound `fn() -> namespace_mix::c::E {namespace_mix::c::E::TV}: Impossible` is not satisfied +error[E0277]: the trait bound `fn() -> namespace_mix::c::E {namespace_mix::xm7::TV}: Impossible` is not satisfied --> $DIR/namespace-mix.rs:128:5 | LL | check(xm9::TV); - | ^^^^^ the trait `Impossible` is not implemented for `fn() -> namespace_mix::c::E {namespace_mix::c::E::TV}` + | ^^^^^ the trait `Impossible` is not implemented for `fn() -> namespace_mix::c::E {namespace_mix::xm7::TV}` | note: required by `check` --> $DIR/namespace-mix.rs:21:1 |
