Skip to content
Permalink
Browse files

rustc: don't thread existential projections through path_generic_args.

  • Loading branch information...
eddyb committed Jan 24, 2019
1 parent ffa00d4 commit c0c485c3ad7017c99137d8c9bc9afc729e7db534
@@ -58,7 +58,7 @@ use crate::traits::{ObligationCause, ObligationCauseCode};
use crate::ty::error::TypeError;
use crate::ty::{self, subst::{Subst, SubstsRef}, Region, Ty, TyCtxt, TyKind, TypeFoldable};
use errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString};
use std::{cmp, fmt, iter};
use std::{cmp, fmt};
use syntax_pos::{Pos, Span};

mod note;
@@ -458,6 +458,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
type Path = Vec<String>;
type Region = !;
type Type = !;
type DynExistential = !;

fn print_region(
self: PrintCx<'_, '_, '_, Self>,
@@ -473,6 +474,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
Err(NonTrivialPath)
}

fn print_dyn_existential<'tcx>(
self: PrintCx<'_, '_, 'tcx, Self>,
_predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error> {
Err(NonTrivialPath)
}

fn path_crate(
self: PrintCx<'_, '_, '_, Self>,
cnum: CrateNum,
@@ -513,8 +521,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
print_prefix: impl FnOnce(
PrintCx<'_, 'gcx, 'tcx, Self>,
) -> Result<Self::Path, Self::Error>,
_args: impl Iterator<Item = Kind<'tcx>> + Clone,
_projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
_args: &[Kind<'tcx>],
) -> Result<Self::Path, Self::Error> {
print_prefix(self)
}
@@ -526,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| {
PrintCx::new(self.tcx, AbsolutePathPrinter)
.print_def_path(def_id, None, iter::empty())
.print_def_path(def_id, None)
};

// We compare strings because DefPath can be different
@@ -2409,7 +2409,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
let f = &mut *fmt;
PrintCx::with_tls_tcx(FmtPrinter::new(f, Namespace::ValueNS), |cx| {
let substs = cx.tcx.lift(&substs).expect("could not lift for printing");
cx.print_def_path(variant_def.did, Some(substs), iter::empty())?;
cx.print_def_path(variant_def.did, Some(substs))?;
Ok(())
})?;

@@ -178,7 +178,7 @@ impl<'tcx> fmt::Display for Instance<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
PrintCx::with_tls_tcx(FmtPrinter::new(&mut *f, Namespace::ValueNS), |cx| {
let substs = cx.tcx.lift(&self.substs).expect("could not lift for printing");
cx.print_def_path(self.def_id(), Some(substs), iter::empty())?;
cx.print_def_path(self.def_id(), Some(substs))?;
Ok(())
})?;

@@ -5,7 +5,6 @@ use crate::ty::subst::{Kind, Subst, SubstsRef};

use rustc_data_structures::fx::FxHashSet;

use std::iter;
use std::ops::{Deref, DerefMut};

// `pretty` is a separate module only for organization.
@@ -64,14 +63,14 @@ pub trait Printer: Sized {
type Path;
type Region;
type Type;
type DynExistential;

fn print_def_path(
self: PrintCx<'_, '_, 'tcx, Self>,
def_id: DefId,
substs: Option<SubstsRef<'tcx>>,
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
) -> Result<Self::Path, Self::Error> {
self.default_print_def_path(def_id, substs, projections)
self.default_print_def_path(def_id, substs)
}
fn print_impl_path(
self: PrintCx<'_, '_, 'tcx, Self>,
@@ -93,6 +92,11 @@ pub trait Printer: Sized {
ty: Ty<'tcx>,
) -> Result<Self::Type, Self::Error>;

fn print_dyn_existential(
self: PrintCx<'_, '_, 'tcx, Self>,
predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error>;

fn path_crate(
self: PrintCx<'_, '_, '_, Self>,
cnum: CrateNum,
@@ -123,8 +127,7 @@ pub trait Printer: Sized {
print_prefix: impl FnOnce(
PrintCx<'_, 'gcx, 'tcx, Self>,
) -> Result<Self::Path, Self::Error>,
args: impl Iterator<Item = Kind<'tcx>> + Clone,
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
args: &[Kind<'tcx>],
) -> Result<Self::Path, Self::Error>;
}

@@ -133,7 +136,6 @@ impl<P: Printer> PrintCx<'_, 'gcx, 'tcx, P> {
self,
def_id: DefId,
substs: Option<SubstsRef<'tcx>>,
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
) -> Result<P::Path, P::Error> {
debug!("default_print_def_path: def_id={:?}, substs={:?}", def_id, substs);
let key = self.tcx.def_key(def_id);
@@ -175,10 +177,10 @@ impl<P: Printer> PrintCx<'_, 'gcx, 'tcx, P> {
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, iter::empty())
cx.print_def_path(parent_def_id, substs)
}
} else {
cx.print_def_path(parent_def_id, None, iter::empty())
cx.print_def_path(parent_def_id, None)
}
};
let print_path = |cx: PrintCx<'_, 'gcx, 'tcx, P>| {
@@ -197,7 +199,7 @@ impl<P: Printer> PrintCx<'_, 'gcx, 'tcx, P> {

if let (Some(generics), Some(substs)) = (generics, substs) {
let args = self.generic_args_to_print(generics, substs);
self.path_generic_args(print_path, args, projections)
self.path_generic_args(print_path, args)
} else {
print_path(self)
}
@@ -209,13 +211,16 @@ impl<P: Printer> PrintCx<'_, 'gcx, 'tcx, P> {
&self,
generics: &'tcx ty::Generics,
substs: SubstsRef<'tcx>,
) -> impl Iterator<Item = Kind<'tcx>> + Clone {
) -> &'tcx [Kind<'tcx>] {
let mut own_params = generics.parent_count..generics.count();

// Don't print args for `Self` parameters (of traits).
let has_own_self = generics.has_self && generics.parent_count == 0;
let params = &generics.params[has_own_self as usize..];
if generics.has_self && own_params.start == 0 {
own_params.start = 1;
}

// Don't print args that are the defaults of their respective parameters.
let num_supplied_defaults = params.iter().rev().take_while(|param| {
own_params.end -= generics.params.iter().rev().take_while(|param| {
match param.kind {
ty::GenericParamDefKind::Lifetime => false,
ty::GenericParamDefKind::Type { has_default, .. } => {
@@ -226,9 +231,8 @@ impl<P: Printer> PrintCx<'_, 'gcx, 'tcx, P> {
ty::GenericParamDefKind::Const => false, // FIXME(const_generics:defaults)
}
}).count();
params[..params.len() - num_supplied_defaults].iter().map(move |param| {
substs[param.index as usize]
})

&substs[own_params]
}

fn default_print_impl_path(
@@ -261,7 +265,7 @@ impl<P: Printer> PrintCx<'_, 'gcx, 'tcx, P> {
// 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, iter::empty()),
|cx| cx.print_def_path(parent_def_id, None),
self_ty,
impl_trait_ref,
)
@@ -344,3 +348,11 @@ impl<P: Printer> Print<'tcx, P> for Ty<'tcx> {
cx.print_type(self)
}
}

impl<P: Printer> Print<'tcx, P> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
type Output = P::DynExistential;
type Error = P::Error;
fn print(&self, cx: PrintCx<'_, '_, 'tcx, P>) -> Result<Self::Output, Self::Error> {
cx.print_dyn_existential(self)
}
}
Oops, something went wrong.

0 comments on commit c0c485c

Please sign in to comment.
You can’t perform that action at this time.