Skip to content
Draft
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_ast_lowering/src/delegation/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
pure_wrt_drop: p.pure_wrt_drop,
source: hir::GenericParamSource::Generics,
span,
implicit_bounds_span: span,
})
}));

Expand Down
20 changes: 17 additions & 3 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,15 +1068,17 @@ impl<'hir> LoweringContext<'_, 'hir> {

let hir_id = self.lower_node_id(node_id);
let def_id = self.local_def_id(node_id);
let span = self.lower_span(ident.span);
hir::GenericParam {
hir_id,
def_id,
name: hir::ParamName::Fresh,
span: self.lower_span(ident.span),
span,
pure_wrt_drop: false,
kind: hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Elided(kind) },
colon_span: None,
source,
implicit_bounds_span: span,
}
}

Expand Down Expand Up @@ -2206,15 +2208,26 @@ impl<'hir> LoweringContext<'_, 'hir> {
let hir_id = self.lower_node_id(param.id);
let param_attrs = &param.attrs;
let param_span = param.span();
let def_id = self.local_def_id(param.id);
let span = self.lower_span(param.span());
let param = hir::GenericParam {
hir_id,
def_id: self.local_def_id(param.id),
def_id,
name,
span: self.lower_span(param.span()),
span,
pure_wrt_drop: attr::contains_name(&param.attrs, sym::may_dangle),
kind,
colon_span: param.colon_span.map(|s| self.lower_span(s)),
source,
implicit_bounds_span: match kind {
hir::GenericParamKind::Lifetime { .. } => span,
hir::GenericParamKind::Type { .. } => self.mark_span_with_reason(
DesugaringKind::DefaultBound { def_id: def_id.into() },
span,
None,
),
hir::GenericParamKind::Const { .. } => span,
},
};
self.lower_attrs(hir_id, param_attrs, param_span, Target::from_generic_param(&param));
param
Expand Down Expand Up @@ -2482,6 +2495,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
kind: hir::GenericParamKind::Type { default: None, synthetic: true },
colon_span: None,
source: hir::GenericParamSource::Generics,
implicit_bounds_span: span,
};

let preds = self.lower_generic_bound_predicate(
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,7 @@ pub struct GenericParam<'hir> {
#[stable_hash(ignore)]
pub hir_id: HirId,
pub def_id: LocalDefId,
pub implicit_bounds_span: Span,
pub name: ParamName,
pub span: Span,
pub pure_wrt_drop: bool,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,7 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(
kind,
colon_span: _,
source: _,
implicit_bounds_span: _,
} = param;
try_visit!(visitor.visit_id(*hir_id));
match *name {
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use rustc_middle::ty::{
TypeVisitable, TypeVisitableExt, Unnormalized, fold_regions,
};
use rustc_session::lint::builtin::UNINHABITED_STATIC;
use rustc_span::sym;
use rustc_span::{DesugaringKind, sym};
use rustc_target::spec::{AbiMap, AbiMapping};
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
use rustc_trait_selection::traits;
Expand Down Expand Up @@ -2117,7 +2117,9 @@ fn check_type_alias_type_params_are_used<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalD
// * check for emptiness to detect lone user-written `?Sized` bounds
// * compare the param span to the pred span to detect lone user-written `Sized` bounds
let has_explicit_bounds = bounded_params.is_empty()
|| (*bounded_params).get(&param.index).is_some_and(|&&pred_sp| pred_sp != span);
|| (*bounded_params).get(&param.index).is_some_and(|&&pred_sp| {
!pred_sp.is_desugaring(DesugaringKind::DefaultBound { def_id: param.def_id })
});
let const_param_help = !has_explicit_bounds;

let mut diag = tcx.dcx().create_err(diagnostics::UnusedGenericParameter {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_hir_analysis/src/collect/item_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ fn associated_type_bounds<'tcx>(
&mut bounds,
item_ty,
hir_bounds,
&[],
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
span,
);
icx.lowerer().add_default_traits(
&mut bounds,
item_ty,
hir_bounds,
&[],
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
span,
);
Expand Down Expand Up @@ -386,13 +388,15 @@ fn opaque_type_bounds<'tcx>(
&mut bounds,
item_ty,
hir_bounds,
&[],
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
span,
);
icx.lowerer().add_default_traits(
&mut bounds,
item_ty,
hir_bounds,
&[],
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
span,
);
Expand Down
24 changes: 16 additions & 8 deletions compiler/rustc_hir_analysis/src/collect/predicates_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,15 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
&mut bounds,
tcx.types.self_param,
self_bounds,
&[],
ImpliedBoundsContext::TraitDef(def_id),
span,
);
icx.lowerer().add_default_traits(
&mut bounds,
tcx.types.self_param,
self_bounds,
&[],
ImpliedBoundsContext::TraitDef(def_id),
span,
);
Expand Down Expand Up @@ -239,15 +241,17 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
&mut bounds,
param_ty,
&[],
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
param.span,
hir_generics.predicates,
ImpliedBoundsContext::TyParam(param.def_id),
param.implicit_bounds_span,
);
icx.lowerer().add_default_traits(
&mut bounds,
param_ty,
&[],
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
param.span,
hir_generics.predicates,
ImpliedBoundsContext::TyParam(param.def_id),
param.implicit_bounds_span,
);
trace!(?bounds);
predicates.extend(bounds);
Expand Down Expand Up @@ -691,13 +695,15 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
&mut bounds,
self_param_ty,
superbounds,
&[],
ImpliedBoundsContext::TraitDef(trait_def_id),
item.span,
);
icx.lowerer().add_default_traits(
&mut bounds,
self_param_ty,
superbounds,
&[],
ImpliedBoundsContext::TraitDef(trait_def_id),
item.span,
);
Expand Down Expand Up @@ -993,15 +999,17 @@ impl<'tcx> ItemCtxt<'tcx> {
&mut bounds,
param_ty,
&[],
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
param.span,
hir_generics.predicates,
ImpliedBoundsContext::TyParam(param.def_id),
param.implicit_bounds_span,
);
self.lowerer().add_default_traits(
&mut bounds,
param_ty,
&[],
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
param.span,
hir_generics.predicates,
ImpliedBoundsContext::TyParam(param.def_id),
param.implicit_bounds_span,
);
}
hir::GenericParamKind::Lifetime { .. }
Expand Down
49 changes: 32 additions & 17 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ impl CollectedSizednessBounds {

fn search_bounds_for<'tcx>(
hir_bounds: &'tcx [hir::GenericBound<'tcx>],
context: ImpliedBoundsContext<'tcx>,
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
context: ImpliedBoundsContext,
mut f: impl FnMut(&'tcx PolyTraitRef<'tcx>),
) {
let mut search_bounds = |hir_bounds: &'tcx [hir::GenericBound<'tcx>]| {
Expand All @@ -73,8 +74,8 @@ fn search_bounds_for<'tcx>(
};

search_bounds(hir_bounds);
if let ImpliedBoundsContext::TyParam(self_ty, where_clause) = context {
for clause in where_clause {
if let ImpliedBoundsContext::TyParam(self_ty) = context {
for clause in where_bounds {
if let hir::WherePredicateKind::BoundPredicate(pred) = clause.kind
&& pred.is_param_bound(self_ty.to_def_id())
{
Expand All @@ -86,11 +87,12 @@ fn search_bounds_for<'tcx>(

fn collect_bounds<'a, 'tcx>(
hir_bounds: &'a [hir::GenericBound<'tcx>],
context: ImpliedBoundsContext<'tcx>,
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
context: ImpliedBoundsContext,
target_did: DefId,
) -> CollectedBound {
let mut collect_into = CollectedBound::default();
search_bounds_for(hir_bounds, context, |ptr| {
search_bounds_for(hir_bounds, where_bounds, context, |ptr| {
if !matches!(ptr.trait_ref.path.res, Res::Def(DefKind::Trait, did) if did == target_did) {
return;
}
Expand All @@ -107,17 +109,18 @@ fn collect_bounds<'a, 'tcx>(
fn collect_sizedness_bounds<'tcx>(
tcx: TyCtxt<'tcx>,
hir_bounds: &'tcx [hir::GenericBound<'tcx>],
context: ImpliedBoundsContext<'tcx>,
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
context: ImpliedBoundsContext,
span: Span,
) -> CollectedSizednessBounds {
let sized_did = tcx.require_lang_item(hir::LangItem::Sized, span);
let sized = collect_bounds(hir_bounds, context, sized_did);
let sized = collect_bounds(hir_bounds, where_bounds, context, sized_did);

let meta_sized_did = tcx.require_lang_item(hir::LangItem::MetaSized, span);
let meta_sized = collect_bounds(hir_bounds, context, meta_sized_did);
let meta_sized = collect_bounds(hir_bounds, where_bounds, context, meta_sized_did);

let pointee_sized_did = tcx.require_lang_item(hir::LangItem::PointeeSized, span);
let pointee_sized = collect_bounds(hir_bounds, context, pointee_sized_did);
let pointee_sized = collect_bounds(hir_bounds, where_bounds, context, pointee_sized_did);

CollectedSizednessBounds { sized, meta_sized, pointee_sized }
}
Expand Down Expand Up @@ -150,7 +153,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
self_ty: Ty<'tcx>,
hir_bounds: &'tcx [hir::GenericBound<'tcx>],
context: ImpliedBoundsContext<'tcx>,
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
context: ImpliedBoundsContext,
span: Span,
) {
let tcx = self.tcx();
Expand Down Expand Up @@ -180,7 +184,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
ImpliedBoundsContext::TyParam(..) | ImpliedBoundsContext::AssociatedTypeOrImplTrait => {
}
}
let collected = collect_sizedness_bounds(tcx, hir_bounds, context, span);
let collected = collect_sizedness_bounds(tcx, hir_bounds, where_bounds, context, span);
if let Some(span) = collected.sized.maybe.or(collected.sized.negative)
&& collected.sized.positive.is_none()
&& !collected.meta_sized.any()
Expand Down Expand Up @@ -212,11 +216,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
self_ty: Ty<'tcx>,
hir_bounds: &[hir::GenericBound<'tcx>],
context: ImpliedBoundsContext<'tcx>,
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
context: ImpliedBoundsContext,
span: Span,
) {
self.tcx().default_traits().iter().for_each(|default_trait| {
self.add_default_trait(*default_trait, bounds, self_ty, hir_bounds, context, span);
self.add_default_trait(
*default_trait,
bounds,
self_ty,
hir_bounds,
where_bounds,
context,
span,
);
});
}

Expand All @@ -229,7 +242,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
self_ty: Ty<'tcx>,
hir_bounds: &[hir::GenericBound<'tcx>],
context: ImpliedBoundsContext<'tcx>,
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
context: ImpliedBoundsContext,
span: Span,
) {
let tcx = self.tcx();
Expand All @@ -243,7 +257,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
}

if let Some(trait_did) = tcx.lang_items().get(trait_)
&& self.should_add_default_traits(trait_did, hir_bounds, context)
&& self.should_add_default_traits(trait_did, hir_bounds, where_bounds, context)
{
add_trait_bound(tcx, bounds, self_ty, trait_did, span);
}
Expand All @@ -254,9 +268,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
&self,
trait_def_id: DefId,
hir_bounds: &'a [hir::GenericBound<'tcx>],
context: ImpliedBoundsContext<'tcx>,
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
context: ImpliedBoundsContext,
) -> bool {
let collected = collect_bounds(hir_bounds, context, trait_def_id);
let collected = collect_bounds(hir_bounds, where_bounds, context, trait_def_id);
!find_attr!(self.tcx(), crate, RustcNoImplicitBounds) && !collected.any()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
.iter()
.map(|&trait_ref| hir::GenericBound::Trait(trait_ref))
.collect::<Vec<_>>(),
&[],
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
span,
);
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ use crate::{NoVariantNamed, check_c_variadic_abi};
/// The context in which an implied bound is being added to a item being lowered (i.e. a sizedness
/// trait or a default trait)
#[derive(Clone, Copy)]
pub(crate) enum ImpliedBoundsContext<'tcx> {
pub(crate) enum ImpliedBoundsContext {
/// An implied bound is added to a trait definition (i.e. a new supertrait), used when adding
/// a default `MetaSized` supertrait
TraitDef(LocalDefId),
/// An implied bound is added to a type parameter
TyParam(LocalDefId, &'tcx [hir::WherePredicate<'tcx>]),
TyParam(LocalDefId),
/// An implied bound being added in any other context
AssociatedTypeOrImplTrait,
}
Expand Down Expand Up @@ -3174,6 +3174,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
&mut bounds,
self_ty,
hir_bounds,
&[],
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
hir_ty.span,
);
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ use rustc_middle::ty::print::{
use rustc_middle::ty::{self, GenericArgKind, IsSuggestable, Ty, TyCtxt, TypeVisitableExt};
use rustc_span::def_id::DefIdSet;
use rustc_span::{
DUMMY_SP, ErrorGuaranteed, ExpnKind, FileName, Ident, MacroKind, Span, Symbol, edit_distance,
kw, sym,
DUMMY_SP, DesugaringKind, ErrorGuaranteed, ExpnKind, FileName, Ident, MacroKind, Span, Symbol,
edit_distance, kw, sym,
};
use rustc_trait_selection::error_reporting::traits::DefIdOrName;
use rustc_trait_selection::infer::InferCtxtExt;
Expand Down Expand Up @@ -1952,8 +1952,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ => false,
}
});
for param in generics.params {
if param.span == cause_span && sized_pred {
if sized_pred && let Some(DesugaringKind::DefaultBound { def_id: def }) = cause_span.desugaring_kind() {
if let Some(param) = generics.params.iter().find(|p| p.def_id.to_def_id() == def) {
let (sp, sugg) = match param.colon_span {
Some(sp) => (sp.shrink_to_hi(), " ?Sized +"),
None => (param.span.shrink_to_hi(), ": ?Sized"),
Expand Down
Loading
Loading