use crate::hir::def_id::DefId;
use crate::hir::HirVec;
use crate::lint;
+use crate::middle::lang_items::SizedTraitLangItem;
use crate::middle::resolve_lifetime as rl;
use crate::namespace::Namespace;
use rustc::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
fn get_type_parameter_bounds(&self, span: Span, def_id: DefId)
-> &'tcx ty::GenericPredicates<'tcx>;
- /// What lifetime should we use when a lifetime is omitted (and not elided)?
+ /// Returns the lifetime to use when a lifetime is omitted (and not elided).
fn re_infer(&self, span: Span, _def: Option<&ty::GenericParamDef>)
-> Option<ty::Region<'tcx>>;
- /// What type should we use when a type is omitted?
+ /// Returns the type to use when a type is omitted.
fn ty_infer(&self, span: Span) -> Ty<'tcx>;
- /// Same as ty_infer, but with a known type parameter definition.
+ /// Same as `ty_infer`, but with a known type parameter definition.
fn ty_infer_for_def(&self,
_def: &ty::GenericParamDef,
span: Span) -> Ty<'tcx> {
fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span);
}
+pub enum SizedByDefault {
+ Yes,
+ No,
+}
+
struct ConvertedBinding<'tcx> {
item_name: ast::Ident,
- ty: Ty<'tcx>,
+ kind: ConvertedBindingKind<'tcx>,
span: Span,
}
+enum ConvertedBindingKind<'tcx> {
+ Equality(Ty<'tcx>),
+ Constraint(P<[hir::GenericBound]>),
+}
+
#[derive(PartialEq)]
enum GenericArgPosition {
Type,
}
err.emit();
- (provided > required, // `suppress_error`
- potential_assoc_types)
+ (
+ provided > required, // `suppress_error`
+ potential_assoc_types,
+ )
};
if reported_late_bound_region_err.is_none()
}
/// Given the type/lifetime/const arguments provided to some path (along with
- /// an implicit `Self`, if this is a trait reference) returns the complete
+ /// an implicit `Self`, if this is a trait reference), returns the complete
/// set of substitutions. This may involve applying defaulted type parameters.
///
/// Note that the type listing given here is *exactly* what the user provided.
- fn create_substs_for_ast_path(&self,
+ fn create_substs_for_ast_path<'a>(&self,
span: Span,
def_id: DefId,
- generic_args: &hir::GenericArgs,
+ generic_args: &'a hir::GenericArgs,
infer_types: bool,
self_ty: Option<Ty<'tcx>>)
-> (SubstsRef<'tcx>, Vec<ConvertedBinding<'tcx>>, Option<Vec<Span>>)
},
);
- let assoc_bindings = generic_args.bindings.iter().map(|binding| {
- ConvertedBinding {
- item_name: binding.ident,
- ty: self.ast_ty_to_ty(&binding.ty),
- span: binding.span,
- }
- }).collect();
+ // Convert associated-type bindings or constraints into a separate vector.
+ // Example: Given this:
+ //
+ // T: Iterator<Item = u32>
+ //
+ // The `T` is passed in as a self-type; the `Item = u32` is
+ // not a "type parameter" of the `Iterator` trait, but rather
+ // a restriction on `<T as Iterator>::Item`, so it is passed
+ // back separately.
+ let assoc_bindings = generic_args.bindings.iter()
+ .map(|binding| {
+ let kind = if let hir::TyKind::AssocTyExistential(ref bounds) = binding.ty.node {
+ ConvertedBindingKind::Constraint(bounds.clone())
+ } else {
+ ConvertedBindingKind::Equality(self.ast_ty_to_ty(&binding.ty))
+ };
+ ConvertedBinding {
+ item_name: binding.ident,
+ kind,
+ span: binding.span,
+ }
+ })
+ .collect();
debug!("create_substs_for_ast_path(generic_params={:?}, self_ty={:?}) -> {:?}",
generic_params, self_ty, substs);
/// are disallowed. Otherwise, they are pushed onto the vector given.
pub fn instantiate_mono_trait_ref(&self,
trait_ref: &hir::TraitRef,
- self_ty: Ty<'tcx>)
- -> ty::TraitRef<'tcx>
+ self_ty: Ty<'tcx>
+ ) -> ty::TraitRef<'tcx>
{
self.prohibit_generics(trait_ref.path.segments.split_last().unwrap().1);
pub(super) fn instantiate_poly_trait_ref_inner(&self,
trait_ref: &hir::TraitRef,
self_ty: Ty<'tcx>,
- poly_projections: &mut Vec<(ty::PolyProjectionPredicate<'tcx>, Span)>,
- speculative: bool)
- -> (ty::PolyTraitRef<'tcx>, Option<Vec<Span>>)
+ bounds: &mut Bounds<'tcx>,
+ speculative: bool,
+ ) -> (ty::PolyTraitRef<'tcx>, Option<Vec<Span>>)
{
let trait_def_id = trait_ref.trait_def_id();
let poly_trait_ref = ty::Binder::bind(ty::TraitRef::new(trait_def_id, substs));
let mut dup_bindings = FxHashMap::default();
- poly_projections.extend(assoc_bindings.iter().filter_map(|binding| {
- // specify type to assert that error was already reported in Err case:
- let predicate: Result<_, ErrorReported> =
- self.ast_type_binding_to_poly_projection_predicate(
- trait_ref.hir_ref_id, poly_trait_ref, binding, speculative, &mut dup_bindings);
- // okay to ignore Err because of ErrorReported (see above)
- Some((predicate.ok()?, binding.span))
- }));
-
- debug!("instantiate_poly_trait_ref({:?}, projections={:?}) -> {:?}",
- trait_ref, poly_projections, poly_trait_ref);
+ for binding in &assoc_bindings {
+ // Specify type to assert that error was already reported in `Err` case.
+ let _: Result<_, ErrorReported> =
+ self.add_predicates_for_ast_type_binding(
+ trait_ref.hir_ref_id,
+ poly_trait_ref,
+ binding,
+ bounds,
+ speculative,
+ &mut dup_bindings
+ );
+ // Okay to ignore `Err` because of `ErrorReported` (see above).
+ }
+
+ debug!("instantiate_poly_trait_ref({:?}, bounds={:?}) -> {:?}",
+ trait_ref, bounds, poly_trait_ref);
(poly_trait_ref, potential_assoc_types)
}
pub fn instantiate_poly_trait_ref(&self,
poly_trait_ref: &hir::PolyTraitRef,
self_ty: Ty<'tcx>,
- poly_projections: &mut Vec<(ty::PolyProjectionPredicate<'tcx>, Span)>)
- -> (ty::PolyTraitRef<'tcx>, Option<Vec<Span>>)
+ bounds: &mut Bounds<'tcx>
+ ) -> (ty::PolyTraitRef<'tcx>, Option<Vec<Span>>)
{
- self.instantiate_poly_trait_ref_inner(&poly_trait_ref.trait_ref, self_ty,
- poly_projections, false)
+ self.instantiate_poly_trait_ref_inner(&poly_trait_ref.trait_ref, self_ty, bounds, false)
}
fn ast_path_to_mono_trait_ref(&self,
- span: Span,
- trait_def_id: DefId,
- self_ty: Ty<'tcx>,
- trait_segment: &hir::PathSegment)
- -> ty::TraitRef<'tcx>
+ span: Span,
+ trait_def_id: DefId,
+ self_ty: Ty<'tcx>,
+ trait_segment: &hir::PathSegment
+ ) -> ty::TraitRef<'tcx>
{
let (substs, assoc_bindings, _) =
self.create_substs_for_ast_trait_ref(span,
})
}
- fn ast_type_binding_to_poly_projection_predicate(
+ // Returns `true` if a bounds list includes `?Sized`.
+ pub fn is_unsized(&self, ast_bounds: &[hir::GenericBound], span: Span) -> bool {
+ let tcx = self.tcx();
+
+ // Try to find an unbound in bounds.
+ let mut unbound = None;
+ for ab in ast_bounds {
+ if let &hir::GenericBound::Trait(ref ptr, hir::TraitBoundModifier::Maybe) = ab {
+ if unbound.is_none() {
+ unbound = Some(ptr.trait_ref.clone());
+ } else {
+ span_err!(
+ tcx.sess,
+ span,
+ E0203,
+ "type parameter has more than one relaxed default \
+ bound, only one is supported"
+ );
+ }
+ }
+ }
+
+ let kind_id = tcx.lang_items().require(SizedTraitLangItem);
+ match unbound {
+ Some(ref tpb) => {
+ // FIXME(#8559) currently requires the unbound to be built-in.
+ if let Ok(kind_id) = kind_id {
+ if tpb.path.res != Res::Def(DefKind::Trait, kind_id) {
+ tcx.sess.span_warn(
+ span,
+ "default bound relaxed for a type parameter, but \
+ this does nothing because the given bound is not \
+ a default. Only `?Sized` is supported",
+ );
+ }
+ }
+ }
+ _ if kind_id.is_ok() => {
+ return false;
+ }
+ // No lang item for `Sized`, so we can't add it as a bound.
+ None => {}
+ }
+
+ true
+ }
+
+ fn add_bounds(&self,
+ param_ty: Ty<'tcx>,
+ ast_bounds: &[hir::GenericBound],
+ bounds: &mut Bounds<'tcx>,
+ ) {
+ let mut trait_bounds = Vec::new();
+ let mut region_bounds = Vec::new();
+
+ for ast_bound in ast_bounds {
+ match *ast_bound {
+ hir::GenericBound::Trait(ref b, hir::TraitBoundModifier::None) =>
+ trait_bounds.push(b),
+ hir::GenericBound::Trait(_, hir::TraitBoundModifier::Maybe) => {}
+ hir::GenericBound::Outlives(ref l) =>
+ region_bounds.push(l),
+ }
+ }
+
+ for bound in trait_bounds {
+ let (poly_trait_ref, _) = self.instantiate_poly_trait_ref(
+ bound,
+ param_ty,
+ bounds,
+ );
+ bounds.trait_bounds.push((poly_trait_ref, bound.span))
+ }
+
+ bounds.region_bounds.extend(region_bounds
+ .into_iter()
+ .map(|r| (self.ast_region_to_region(r, None), r.span))
+ );
+ }
+
+ /// Translates the AST's notion of ty param bounds (which are an enum consisting of a newtyped
+ /// `Ty` or a region) to ty's notion of ty param bounds (which can either be user-defined traits
+ /// or the built-in trait `Sized`).
+ pub fn compute_bounds(&self,
+ param_ty: Ty<'tcx>,
+ ast_bounds: &[hir::GenericBound],
+ sized_by_default: SizedByDefault,
+ span: Span,
+ ) -> Bounds<'tcx> {
+ let mut bounds = Bounds::default();
+
+ self.add_bounds(param_ty, ast_bounds, &mut bounds);
+ bounds.trait_bounds.sort_by_key(|(t, _)| t.def_id());
+
+ bounds.implicitly_sized = if let SizedByDefault::Yes = sized_by_default {
+ if !self.is_unsized(ast_bounds, span) {
+ Some(span)
+ } else {
+ None
+ }
+ } else {
+ None
+ };
+
+ bounds
+ }
+
+ fn add_predicates_for_ast_type_binding(
&self,
hir_ref_id: hir::HirId,
trait_ref: ty::PolyTraitRef<'tcx>,
binding: &ConvertedBinding<'tcx>,
+ bounds: &mut Bounds<'tcx>,
speculative: bool,
- dup_bindings: &mut FxHashMap<DefId, Span>)
- -> Result<ty::PolyProjectionPredicate<'tcx>, ErrorReported>
- {
+ dup_bindings: &mut FxHashMap<DefId, Span>,
+ ) -> Result<(), ErrorReported> {
let tcx = self.tcx();
if !speculative {
// trait SubTrait: SuperTrait<int> { }
// trait SuperTrait<A> { type T; }
//
- // ... B : SubTrait<T=foo> ...
+ // ... B: SubTrait<T = foo> ...
// ```
//
// We want to produce `<B as SuperTrait<int>>::T == foo`.
// Find any late-bound regions declared in `ty` that are not
- // declared in the trait-ref. These are not wellformed.
+ // declared in the trait-ref. These are not well-formed.
//
// Example:
//
// for<'a> <T as Iterator>::Item = &'a str // <-- 'a is bad
// for<'a> <T as FnMut<(&'a u32,)>>::Output = &'a str // <-- 'a is ok
- let late_bound_in_trait_ref = tcx.collect_constrained_late_bound_regions(&trait_ref);
- let late_bound_in_ty =
- tcx.collect_referenced_late_bound_regions(&ty::Binder::bind(binding.ty));
- debug!("late_bound_in_trait_ref = {:?}", late_bound_in_trait_ref);
- debug!("late_bound_in_ty = {:?}", late_bound_in_ty);
- for br in late_bound_in_ty.difference(&late_bound_in_trait_ref) {
- let br_name = match *br {
- ty::BrNamed(_, name) => name,
- _ => {
- span_bug!(
- binding.span,
- "anonymous bound region {:?} in binding but not trait ref",
- br);
- }
- };
- struct_span_err!(tcx.sess,
+ if let ConvertedBindingKind::Equality(ty) = binding.kind {
+ let late_bound_in_trait_ref =
+ tcx.collect_constrained_late_bound_regions(&trait_ref);
+ let late_bound_in_ty =
+ tcx.collect_referenced_late_bound_regions(&ty::Binder::bind(ty));
+ debug!("late_bound_in_trait_ref = {:?}", late_bound_in_trait_ref);
+ debug!("late_bound_in_ty = {:?}", late_bound_in_ty);
+ for br in late_bound_in_ty.difference(&late_bound_in_trait_ref) {
+ let br_name = match *br {
+ ty::BrNamed(_, name) => name,
+ _ => {
+ span_bug!(
binding.span,
- E0582,
- "binding for associated type `{}` references lifetime `{}`, \
- which does not appear in the trait input types",
- binding.item_name, br_name)
- .emit();
+ "anonymous bound region {:?} in binding but not trait ref",
+ br);
+ }
+ };
+ struct_span_err!(tcx.sess,
+ binding.span,
+ E0582,
+ "binding for associated type `{}` references lifetime `{}`, \
+ which does not appear in the trait input types",
+ binding.item_name, br_name)
+ .emit();
+ }
}
}
}?;
let (assoc_ident, def_scope) =
- tcx.adjust_ident(binding.item_name, candidate.def_id(), hir_ref_id);
+ tcx.adjust_ident_and_get_scope(binding.item_name, candidate.def_id(), hir_ref_id);
let assoc_ty = tcx.associated_items(candidate.def_id()).find(|i| {
i.kind == ty::AssocKind::Type && i.ident.modern() == assoc_ident
}).expect("missing associated type");
.or_insert(binding.span);
}
- Ok(candidate.map_bound(|trait_ref| {
- ty::ProjectionPredicate {
- projection_ty: ty::ProjectionTy::from_ref_and_name(
- tcx,
- trait_ref,
- binding.item_name,
- ),
- ty: binding.ty,
+ match binding.kind {
+ ConvertedBindingKind::Equality(ref ty) => {
+ // "Desugar" a constraint like `T: Iterator<Item = u32>` this to
+ // the "projection predicate" for:
+ //
+ // `<T as Iterator>::Item = u32`
+ bounds.projection_bounds.push((candidate.map_bound(|trait_ref| {
+ ty::ProjectionPredicate {
+ projection_ty: ty::ProjectionTy::from_ref_and_name(
+ tcx,
+ trait_ref,
+ binding.item_name,
+ ),
+ ty,
+ }
+ }), binding.span));
}
- }))
+ ConvertedBindingKind::Constraint(ref ast_bounds) => {
+ // Calling `skip_binder` is okay, because the predicates are re-bound later by
+ // `instantiate_poly_trait_ref`.
+ let param_ty = tcx.mk_projection(assoc_ty.def_id, candidate.skip_binder().substs);
+ self.add_bounds(
+ param_ty,
+ ast_bounds,
+ bounds,
+ );
+ }
+ }
+ Ok(())
}
fn ast_path_to_ty(&self,
{
let tcx = self.tcx();
- let mut projection_bounds = Vec::new();
+ let mut bounds = Bounds::default();
let mut potential_assoc_types = Vec::new();
let dummy_self = self.tcx().types.trait_object_dummy_self;
// FIXME: we want to avoid collecting into a `Vec` here, but simply cloning the iterator is
let (trait_ref, cur_potential_assoc_types) = self.instantiate_poly_trait_ref(
trait_bound,
dummy_self,
- &mut projection_bounds
+ &mut bounds,
);
potential_assoc_types.extend(cur_potential_assoc_types.into_iter().flatten());
(trait_ref, trait_bound.span)
// which is uglier but works. See the discussion in #56288 for alternatives.
if !references_self {
// Include projections defined on supertraits.
- projection_bounds.push((pred, DUMMY_SP))
+ bounds.projection_bounds.push((pred, DUMMY_SP))
}
}
_ => ()
}
}
- for (projection_bound, _) in &projection_bounds {
+ for (projection_bound, _) in &bounds.projection_bounds {
associated_types.remove(&projection_bound.projection_def_id());
}
let existential_trait_refs = regular_traits.iter().map(|i| {
i.trait_ref().map_bound(|trait_ref| self.trait_ref_to_existential(trait_ref))
});
- let existential_projections = projection_bounds.iter().map(|(bound, _)| {
+ let existential_projections = bounds.projection_bounds.iter().map(|(bound, _)| {
bound.map_bound(|b| {
let trait_ref = self.trait_ref_to_existential(b.projection_ty.trait_ref(tcx));
ty::ExistentialProjection {
};
let trait_did = bound.def_id();
- let (assoc_ident, def_scope) = tcx.adjust_ident(assoc_ident, trait_did, hir_ref_id);
+ let (assoc_ident, def_scope) =
+ tcx.adjust_ident_and_get_scope(assoc_ident, trait_did, hir_ref_id);
let item = tcx.associated_items(trait_did).find(|i| {
Namespace::from(i.kind) == Namespace::Type &&
i.ident.modern() == assoc_ident
let span = path.span;
match path.res {
Res::Def(DefKind::Existential, did) => {
- // Check for desugared impl trait.
+ // Check for desugared `impl Trait`.
assert!(ty::is_impl_trait_defn(tcx, did).is_none());
let item_segment = path.segments.split_last().unwrap();
self.prohibit_generics(item_segment.1);
&tcx.hir().local_def_id_from_hir_id(hir_id)];
tcx.mk_ty_param(index, tcx.hir().name_by_hir_id(hir_id).as_interned_str())
}
- Res::SelfTy(_, Some(def_id)) => {
- // `Self` in impl (we know the concrete type).
+ Res::SelfTy(Some(_), None) => {
+ // `Self` in trait or type alias.
assert_eq!(opt_self_ty, None);
self.prohibit_generics(&path.segments);
- // Try to evaluate any array length constants
- self.normalize_ty(span, tcx.at(span).type_of(def_id))
+ tcx.mk_self_type()
}
- Res::SelfTy(Some(_), None) => {
- // `Self` in trait.
+ Res::SelfTy(_, Some(def_id)) => {
+ // `Self` in impl (we know the concrete type).
assert_eq!(opt_self_ty, None);
self.prohibit_generics(&path.segments);
- tcx.mk_self_type()
+ // Try to evaluate any array length constants.
+ self.normalize_ty(span, tcx.at(span).type_of(def_id))
}
Res::Def(DefKind::AssocTy, def_id) => {
debug_assert!(path.segments.len() >= 2);
}
hir::TyKind::Rptr(ref region, ref mt) => {
let r = self.ast_region_to_region(region, None);
- debug!("Ref r={:?}", r);
+ debug!("ast_ty_to_ty: r={:?}", r);
let t = self.ast_ty_to_ty(&mt.ty);
tcx.mk_ref(r, ty::TypeAndMut {ty: t, mutbl: mt.mutbl})
}
hir::TyKind::Def(item_id, ref lifetimes) => {
let did = tcx.hir().local_def_id_from_hir_id(item_id.id);
self.impl_trait_ty_to_ty(did, lifetimes)
- },
+ }
hir::TyKind::Path(hir::QPath::TypeRelative(ref qself, ref segment)) => {
debug!("ast_ty_to_ty: qself={:?} segment={:?}", qself, segment);
let ty = self.ast_ty_to_ty(qself);
// handled specially and will not descend into this routine.
self.ty_infer(ast_ty.span)
}
- hir::TyKind::Err => {
- tcx.types.err
- }
hir::TyKind::CVarArgs(lt) => {
let va_list_did = match tcx.lang_items().va_list() {
Some(did) => did,
let region = self.ast_region_to_region(<, None);
tcx.type_of(va_list_did).subst(tcx, &[region.into()])
}
+ hir::TyKind::AssocTyExistential(..) => {
+ // Type is never actually used.
+ tcx.types.err
+ }
+ hir::TyKind::Err => {
+ tcx.types.err
+ }
};
+ debug!("ast_ty_to_ty: result_ty={:?}", result_ty);
+
self.record_ty(ast_ty.hir_id, result_ty, ast_ty.span);
result_ty
}
_ => bug!()
}
} else {
- // Replace all parent lifetimes with 'static.
+ // Replace all parent lifetimes with `'static`.
match param.kind {
GenericParamDefKind::Lifetime => {
tcx.lifetimes.re_static.into()
}
}
});
- debug!("impl_trait_ty_to_ty: final substs = {:?}", substs);
+ debug!("impl_trait_ty_to_ty: substs={:?}", substs);
let ty = tcx.mk_opaque(def_id, substs);
debug!("impl_trait_ty_to_ty: {}", ty);
for br in late_bound_in_ret.difference(&late_bound_in_args) {
let lifetime_name = match *br {
ty::BrNamed(_, name) => format!("lifetime `{}`,", name),
- ty::BrAnon(_) | ty::BrFresh(_) | ty::BrEnv => "an anonymous lifetime".to_string(),
+ ty::BrAnon(_) | ty::BrEnv => "an anonymous lifetime".to_string(),
};
let mut err = struct_span_err!(tcx.sess,
decl.output.span(),
// A helper struct for conveniently grouping a set of bounds which we pass to
// and return from functions in multiple places.
-#[derive(PartialEq, Eq, Clone, Debug)]
+#[derive(Default, PartialEq, Eq, Clone, Debug)]
pub struct Bounds<'tcx> {
pub region_bounds: Vec<(ty::Region<'tcx>, Span)>,
- pub implicitly_sized: Option<Span>,
pub trait_bounds: Vec<(ty::PolyTraitRef<'tcx>, Span)>,
pub projection_bounds: Vec<(ty::PolyProjectionPredicate<'tcx>, Span)>,
+ pub implicitly_sized: Option<Span>,
}
impl<'a, 'gcx, 'tcx> Bounds<'tcx> {