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

Tweak spans for trait bounds on associated types #85799

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_borrowck/src/type_check/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
trait_ref,
constness: ty::BoundConstness::NotConst,
polarity: ty::ImplPolarity::Positive,
// implicit: ty::ImplicitBound::No,
}))),
locations,
category,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
trait_ref,
constness: ty::BoundConstness::ConstIfConst,
polarity: ty::ImplPolarity::Positive,
// implicit: ty::ImplicitBound::No,
}),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ impl Qualif for NeedsNonConstDrop {
trait_ref,
constness: ty::BoundConstness::ConstIfConst,
polarity: ty::ImplPolarity::Positive,
// implicit: ty::ImplicitBound::No,
}),
);

Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_infer/src/infer/outlives/obligations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
cause.span,
sup_type,
match cause.code.peel_derives() {
ObligationCauseCode::BindingObligation(_, span) => Some(*span),
ObligationCauseCode::BindingObligation(_, span)
| ObligationCauseCode::ImplicitSizedObligation(_, span) => Some(*span),
_ => None,
},
)
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ pub enum ObligationCauseCode<'tcx> {
/// Like `ItemObligation`, but with extra detail on the source of the obligation.
BindingObligation(DefId, Span),

/// Like `ItemObligation`, but with extra detail on the source of the obligation.
ImplicitSizedObligation(DefId, Span),

/// A type like `&'a T` is WF only if `T: 'a`.
ReferenceOutlivesReferent(Ty<'tcx>),

Expand Down
40 changes: 33 additions & 7 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,25 @@ pub struct ImplHeader<'tcx> {
pub predicates: Vec<Predicate<'tcx>>,
}

// #[derive(
// Copy,
// Clone,
// PartialEq,
// Eq,
// Hash,
// TyEncodable,
// TyDecodable,
// HashStable,
// Debug,
// TypeFoldable
// )]
// pub enum ImplicitBound {
// /// `T: Trait`
// No,
// /// implicit `T: Sized`
// Yes,
// }

#[derive(
Copy,
Clone,
Expand Down Expand Up @@ -518,13 +537,17 @@ impl<'tcx> Predicate<'tcx> {
.inner
.kind
.map_bound(|kind| match kind {
PredicateKind::Trait(TraitPredicate { trait_ref, constness, polarity }) => {
Some(PredicateKind::Trait(TraitPredicate {
trait_ref,
constness,
polarity: polarity.flip()?,
}))
}
PredicateKind::Trait(TraitPredicate {
trait_ref,
constness,
polarity,
// implicit,
}) => Some(PredicateKind::Trait(TraitPredicate {
trait_ref,
constness,
polarity: polarity.flip()?,
// implicit,
})),

_ => None,
})
Expand Down Expand Up @@ -729,6 +752,8 @@ pub struct TraitPredicate<'tcx> {
pub constness: BoundConstness,

pub polarity: ImplPolarity,

// pub implicit: ImplicitBound,
}

pub type PolyTraitPredicate<'tcx> = ty::Binder<'tcx, TraitPredicate<'tcx>>;
Expand Down Expand Up @@ -1425,6 +1450,7 @@ impl PolyTraitRef<'tcx> {
trait_ref,
constness,
polarity: ty::ImplPolarity::Positive,
// implicit: ty::ImplicitBound::No,
})
}
#[inline]
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_middle/src/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,21 @@ impl<'tcx> Relate<'tcx> for GenericArg<'tcx> {
}
}

// impl<'tcx> Relate<'tcx> for ty::ImplicitBound {
// fn relate<R: TypeRelation<'tcx>>(
// _relation: &mut R,
// a: ty::ImplicitBound,
// b: ty::ImplicitBound,
// ) -> RelateResult<'tcx, ty::ImplicitBound> {
// match (a, b) {
// (ty::ImplicitBound::No, ty::ty::ImplicitBound::No) => Ok(ty::ImplicitBound::No),
// (ty::ImplicitBound::Yes, ty::ty::ImplicitBound::Yes)
// | (ty::ImplicitBound::Yes, ty::ty::ImplicitBound::No)
// | (ty::ImplicitBound::No, ty::ty::ImplicitBound::Yes) => Ok(ty::ImplicitBound::Yes),
// }
// }
// }

impl<'tcx> Relate<'tcx> for ty::ImplPolarity {
fn relate<R: TypeRelation<'tcx>>(
relation: &mut R,
Expand All @@ -812,6 +827,7 @@ impl<'tcx> Relate<'tcx> for ty::TraitPredicate<'tcx> {
trait_ref: relation.relate(a.trait_ref, b.trait_ref)?,
constness: relation.relate(a.constness, b.constness)?,
polarity: relation.relate(a.polarity, b.polarity)?,
// implicit: relation.relate(a.implicit, b.implicit)?,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
trait_ref,
constness: self.constness,
polarity: self.polarity,
// implicit: ty::ImplicitBound::No,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,7 @@ impl<'tcx> PolyTraitRef<'tcx> {
trait_ref,
constness: ty::BoundConstness::NotConst,
polarity: ty::ImplPolarity::Positive,
// implicit: ty::ImplicitBound::No,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ where
trait_ref,
constness: _,
polarity: _,
// implicit: _,
}) => self.visit_trait(trait_ref),
ty::PredicateKind::Projection(ty::ProjectionPredicate { projection_ty, ty }) => {
ty.visit_with(self)?;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_trait_selection/src/traits/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ impl AutoTraitFinder<'tcx> {
constness: ty::BoundConstness::NotConst,
// Auto traits are positive
polarity: ty::ImplPolarity::Positive,
// implicit: ty::ImplicitBound::No,
}));

let computed_preds = param_env.caller_bounds().iter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
obligation.cause.code.peel_derives(),
ObligationCauseCode::ItemObligation(_)
| ObligationCauseCode::BindingObligation(_, _)
| ObligationCauseCode::ImplicitSizedObligation(_, _)
| ObligationCauseCode::ObjectCastObligation(_)
| ObligationCauseCode::OpaqueType
);
Expand Down Expand Up @@ -1702,7 +1703,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id());
} else if let (
Ok(ref snippet),
ObligationCauseCode::BindingObligation(ref def_id, _),
ObligationCauseCode::BindingObligation(ref def_id, _)
| ObligationCauseCode::ImplicitSizedObligation(ref def_id, _),
) =
(self.tcx.sess.source_map().span_to_snippet(span), &obligation.cause.code)
{
Expand Down Expand Up @@ -2005,7 +2007,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
{
(
ty::PredicateKind::Trait(pred),
&ObligationCauseCode::BindingObligation(item_def_id, span),
&ObligationCauseCode::BindingObligation(item_def_id, span)
| &ObligationCauseCode::ImplicitSizedObligation(item_def_id, span),
) => (pred, item_def_id, span),
_ => return,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
}

if let ObligationCauseCode::ItemObligation(item)
| ObligationCauseCode::BindingObligation(item, _) = obligation.cause.code
| ObligationCauseCode::BindingObligation(item, _)
| ObligationCauseCode::ImplicitSizedObligation(item, _) = obligation.cause.code
{
// FIXME: maybe also have some way of handling methods
// from other traits? That would require name resolution,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
if let ObligationCauseCode::ImplDerivedObligation(obligation) = &*code {
try_borrowing(obligation.parent_trait_ref, &[])
} else if let ObligationCauseCode::BindingObligation(_, _)
| ObligationCauseCode::ImplicitSizedObligation(..)
| ObligationCauseCode::ItemObligation(_) = &*code
{
try_borrowing(*poly_trait_ref, &never_suggest_borrow)
Expand Down Expand Up @@ -1961,6 +1962,62 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
// We hold the `DefId` of the item introducing the obligation, but displaying it
// doesn't add user usable information. It always point at an associated item.
}
ObligationCauseCode::ImplicitSizedObligation(item_def_id, span) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

similarly, this is the only site where ImplicitSizedObligation actually matters.

we could instead grab the trait from the predicate argument (need to adjust callers to pass the trait def id from the predicate, probably as an option).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@oli-obk let me know given the new changes whether this still applies.

let item_name = tcx.def_path_str(item_def_id);
let mut sp: MultiSpan = span.into();
match self.tcx.hir().get_if_local(item_def_id) {
Some(hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Type(bounds, _),
ident,
..
})) => {
sp.push_span_label(
span,
format!("required by associated type `{}`", item_name),
);
err.span_note(sp, "associated types have an implicit `Sized` obligation");

let sized_trait = self.tcx.lang_items().sized_trait();
if bounds.len() == 0 {
err.span_suggestion_verbose(
ident.span.shrink_to_hi(),
"consider relaxing the `Sized` obligation",
": ?Sized".to_string(),
Applicability::MaybeIncorrect,
);
} else if bounds.iter().all(|bound| {
bound.trait_ref().and_then(|tr| tr.trait_def_id()) != sized_trait
}) {
err.span_suggestion_verbose(
bounds.iter().last().unwrap().span().shrink_to_hi(),
"consider relaxing the `Sized` obligation",
" + ?Sized".to_string(),
Applicability::MaybeIncorrect,
);
}
}
Some(hir::Node::ImplItem(hir::ImplItem {
kind: hir::ImplItemKind::TyAlias(_),
..
})) => {
let msg = "associated types on `impl` blocks for types, have an implicit \
mandatory `Sized` obligation; associated types from `trait`s can be \
relaxed to `?Sized`";
sp.push_span_label(
span,
format!("required by associated type `{}`", item_name),
);
err.span_note(sp, msg);
}
_ => {
sp.push_span_label(
span,
format!("required by this bound in `{}`", item_name),
);
err.span_note(sp, "type parameters have an implicit `Sized` obligation");
}
}
}
ObligationCauseCode::BindingObligation(item_def_id, span) => {
let item_name = tcx.def_path_str(item_def_id);
let mut multispan = MultiSpan::from(span);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_trait_selection/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ pub fn vtable_trait_upcasting_coercion_new_vptr_slot(
trait_ref,
constness: ty::BoundConstness::NotConst,
polarity: ty::ImplPolarity::Positive,
// implicit: ty::ImplicitBound::No,
}),
);

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_trait_selection/src/traits/relationships.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub(crate) fn update<'tcx, T>(
trait_ref,
constness: predicate.constness,
polarity: predicate.polarity,
// implicit: predicate.implicit,
})
})
.to_predicate(infcx.tcx),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
},
constness: ty::BoundConstness::NotConst,
polarity: ty::ImplPolarity::Positive,
// implicit: ty::ImplicitBound::No,
}));
copy_obligation.recursion_depth = depth + 1;
self.assemble_candidates_from_impls(&copy_obligation, &mut new_candidates);
Expand All @@ -951,6 +952,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
},
constness: ty::BoundConstness::ConstIfConst,
polarity: ty::ImplPolarity::Positive,
// implicit: ty::ImplicitBound::No,
}));

let const_drop_stack = self.push_stack(obligation_stack.list(), &const_drop_obligation);
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_trait_selection/src/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,13 @@ pub fn predicates_for_generics<'tcx>(
traits::ItemObligation(def_id) if !span.is_dummy() => traits::ObligationCause::new(
cause.span,
cause.body_id,
traits::BindingObligation(def_id, span),
match predicate.kind().skip_binder() {
// ty::PredicateKind::Trait(ty::TraitPredicate {
// implicit: ty::ImplicitBound::Yes,
// ..
// }) => traits::ImplicitSizedObligation(def_id, span),
_ => traits::BindingObligation(def_id, span),
},
),
_ => cause.clone(),
};
Expand Down
11 changes: 7 additions & 4 deletions compiler/rustc_trait_selection/src/traits/wf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,13 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {

iter::zip(iter::zip(predicates.predicates, predicates.spans), origins.into_iter().rev())
.map(|((pred, span), origin_def_id)| {
let code = if span.is_dummy() {
traits::MiscObligation
} else {
traits::BindingObligation(origin_def_id, span)
let code = match () {// pred.kind().skip_binder() {
_ if span.is_dummy() => traits::MiscObligation,
// ty::PredicateKind::Trait(ty::TraitPredicate {
// implicit: ty::ImplicitBound::Yes,
// ..
// }) => traits::ImplicitSizedObligation(origin_def_id, span),
_ => traits::BindingObligation(origin_def_id, span),
};
let cause = self.cause(code);
traits::Obligation::with_depth(cause, self.recursion_depth, self.param_env, pred)
Expand Down
16 changes: 11 additions & 5 deletions compiler/rustc_typeck/src/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,17 @@ impl<'tcx> Bounds<'tcx> {
// If it could be sized, and is, add the `Sized` predicate.
let sized_predicate = self.implicitly_sized.and_then(|span| {
tcx.lang_items().sized_trait().map(|sized| {
let trait_ref = ty::Binder::dummy(ty::TraitRef {
def_id: sized,
substs: tcx.mk_substs_trait(param_ty, &[]),
});
(trait_ref.without_const().to_predicate(tcx), span)
let pred = ty::Binder::dummy(ty::PredicateKind::Trait(ty::TraitPredicate {
trait_ref: ty::TraitRef {
def_id: sized,
substs: tcx.mk_substs_trait(param_ty, &[]),
},
constness: ty::BoundConstness::NotConst,
polarity: ty::ImplPolarity::Positive,
// implicit: ty::ImplicitBound::Yes,
}))
.to_predicate(tcx);
(pred, span)
})
});

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_typeck/src/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
},
constness: t.constness,
polarity: t.polarity,
// implicit: t.implicit,
}));
let obl = Obligation::new(
o.cause.clone(),
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_typeck/src/check/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
fcx.ty_to_string(t_cast)
))
.help(&format!(
"cast can be replaced by coercion; this might \
require {}a temporary variable",
"cast can be replaced by coercion; this might require {}a temporary variable",
type_asc_or
))
.emit();
Expand Down
Loading