Skip to content

Commit 6c4a2f9

Browse files
committed
Auto merge of #158259 - oli-obk:expn-info-default-bounds, r=<try>
Add expansion info to default bounds
2 parents cddcbec + d30eea8 commit 6c4a2f9

20 files changed

Lines changed: 126 additions & 61 deletions

File tree

compiler/rustc_ast_lowering/src/delegation/generics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
363363
pure_wrt_drop: p.pure_wrt_drop,
364364
source: hir::GenericParamSource::Generics,
365365
span,
366+
implicit_bounds_span: span,
366367
}
367368
}));
368369

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,15 +1065,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
10651065

10661066
let hir_id = self.lower_node_id(node_id);
10671067
let def_id = self.local_def_id(node_id);
1068+
let span = self.lower_span(ident.span);
10681069
hir::GenericParam {
10691070
hir_id,
10701071
def_id,
10711072
name: hir::ParamName::Fresh,
1072-
span: self.lower_span(ident.span),
1073+
span,
10731074
pure_wrt_drop: false,
10741075
kind: hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Elided(kind) },
10751076
colon_span: None,
10761077
source,
1078+
implicit_bounds_span: span,
10771079
}
10781080
}
10791081

@@ -2203,15 +2205,26 @@ impl<'hir> LoweringContext<'_, 'hir> {
22032205
let hir_id = self.lower_node_id(param.id);
22042206
let param_attrs = &param.attrs;
22052207
let param_span = param.span();
2208+
let def_id = self.local_def_id(param.id);
2209+
let span = self.lower_span(param.span());
22062210
let param = hir::GenericParam {
22072211
hir_id,
2208-
def_id: self.local_def_id(param.id),
2212+
def_id,
22092213
name,
2210-
span: self.lower_span(param.span()),
2214+
span,
22112215
pure_wrt_drop: attr::contains_name(&param.attrs, sym::may_dangle),
22122216
kind,
22132217
colon_span: param.colon_span.map(|s| self.lower_span(s)),
22142218
source,
2219+
implicit_bounds_span: match kind {
2220+
hir::GenericParamKind::Lifetime { .. } => span,
2221+
hir::GenericParamKind::Type { .. } => self.mark_span_with_reason(
2222+
DesugaringKind::DefaultBound { def_id: def_id.into() },
2223+
span,
2224+
None,
2225+
),
2226+
hir::GenericParamKind::Const { .. } => span,
2227+
},
22152228
};
22162229
self.lower_attrs(hir_id, param_attrs, param_span, Target::from_generic_param(&param));
22172230
param
@@ -2479,6 +2492,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
24792492
kind: hir::GenericParamKind::Type { default: None, synthetic: true },
24802493
colon_span: None,
24812494
source: hir::GenericParamSource::Generics,
2495+
implicit_bounds_span: span,
24822496
};
24832497

24842498
let preds = self.lower_generic_bound_predicate(

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,7 @@ pub struct GenericParam<'hir> {
834834
#[stable_hash(ignore)]
835835
pub hir_id: HirId,
836836
pub def_id: LocalDefId,
837+
pub implicit_bounds_span: Span,
837838
pub name: ParamName,
838839
pub span: Span,
839840
pub pure_wrt_drop: bool,

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,7 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(
11331133
kind,
11341134
colon_span: _,
11351135
source: _,
1136+
implicit_bounds_span: _,
11361137
} = param;
11371138
try_visit!(visitor.visit_id(*hir_id));
11381139
match *name {

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_middle::ty::{
2424
TypeVisitable, TypeVisitableExt, Unnormalized, fold_regions,
2525
};
2626
use rustc_session::lint::builtin::UNINHABITED_STATIC;
27-
use rustc_span::sym;
27+
use rustc_span::{DesugaringKind, sym};
2828
use rustc_target::spec::{AbiMap, AbiMapping};
2929
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
3030
use rustc_trait_selection::traits;
@@ -2117,7 +2117,9 @@ fn check_type_alias_type_params_are_used<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalD
21172117
// * check for emptiness to detect lone user-written `?Sized` bounds
21182118
// * compare the param span to the pred span to detect lone user-written `Sized` bounds
21192119
let has_explicit_bounds = bounded_params.is_empty()
2120-
|| (*bounded_params).get(&param.index).is_some_and(|&&pred_sp| pred_sp != span);
2120+
|| (*bounded_params).get(&param.index).is_some_and(|&&pred_sp| {
2121+
!pred_sp.is_desugaring(DesugaringKind::DefaultBound { def_id: param.def_id })
2122+
});
21212123
let const_param_help = !has_explicit_bounds;
21222124

21232125
let mut diag = tcx.dcx().create_err(diagnostics::UnusedGenericParameter {

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ fn associated_type_bounds<'tcx>(
5858
&mut bounds,
5959
item_ty,
6060
hir_bounds,
61+
&[],
6162
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
6263
span,
6364
);
6465
icx.lowerer().add_default_traits(
6566
&mut bounds,
6667
item_ty,
6768
hir_bounds,
69+
&[],
6870
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
6971
span,
7072
);
@@ -384,13 +386,15 @@ fn opaque_type_bounds<'tcx>(
384386
&mut bounds,
385387
item_ty,
386388
hir_bounds,
389+
&[],
387390
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
388391
span,
389392
);
390393
icx.lowerer().add_default_traits(
391394
&mut bounds,
392395
item_ty,
393396
hir_bounds,
397+
&[],
394398
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
395399
span,
396400
);

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,15 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
200200
&mut bounds,
201201
tcx.types.self_param,
202202
self_bounds,
203+
&[],
203204
ImpliedBoundsContext::TraitDef(def_id),
204205
span,
205206
);
206207
icx.lowerer().add_default_traits(
207208
&mut bounds,
208209
tcx.types.self_param,
209210
self_bounds,
211+
&[],
210212
ImpliedBoundsContext::TraitDef(def_id),
211213
span,
212214
);
@@ -239,15 +241,17 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
239241
&mut bounds,
240242
param_ty,
241243
&[],
242-
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
243-
param.span,
244+
hir_generics.predicates,
245+
ImpliedBoundsContext::TyParam(param.def_id),
246+
param.implicit_bounds_span,
244247
);
245248
icx.lowerer().add_default_traits(
246249
&mut bounds,
247250
param_ty,
248251
&[],
249-
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
250-
param.span,
252+
hir_generics.predicates,
253+
ImpliedBoundsContext::TyParam(param.def_id),
254+
param.implicit_bounds_span,
251255
);
252256
trace!(?bounds);
253257
predicates.extend(bounds);
@@ -692,13 +696,15 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
692696
&mut bounds,
693697
self_param_ty,
694698
superbounds,
699+
&[],
695700
ImpliedBoundsContext::TraitDef(trait_def_id),
696701
item.span,
697702
);
698703
icx.lowerer().add_default_traits(
699704
&mut bounds,
700705
self_param_ty,
701706
superbounds,
707+
&[],
702708
ImpliedBoundsContext::TraitDef(trait_def_id),
703709
item.span,
704710
);
@@ -994,15 +1000,17 @@ impl<'tcx> ItemCtxt<'tcx> {
9941000
&mut bounds,
9951001
param_ty,
9961002
&[],
997-
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
998-
param.span,
1003+
hir_generics.predicates,
1004+
ImpliedBoundsContext::TyParam(param.def_id),
1005+
param.implicit_bounds_span,
9991006
);
10001007
self.lowerer().add_default_traits(
10011008
&mut bounds,
10021009
param_ty,
10031010
&[],
1004-
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
1005-
param.span,
1011+
hir_generics.predicates,
1012+
ImpliedBoundsContext::TyParam(param.def_id),
1013+
param.implicit_bounds_span,
10061014
);
10071015
}
10081016
hir::GenericParamKind::Lifetime { .. }

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ impl CollectedSizednessBounds {
5959

6060
fn search_bounds_for<'tcx>(
6161
hir_bounds: &'tcx [hir::GenericBound<'tcx>],
62-
context: ImpliedBoundsContext<'tcx>,
62+
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
63+
context: ImpliedBoundsContext,
6364
mut f: impl FnMut(&'tcx PolyTraitRef<'tcx>),
6465
) {
6566
let mut search_bounds = |hir_bounds: &'tcx [hir::GenericBound<'tcx>]| {
@@ -73,8 +74,8 @@ fn search_bounds_for<'tcx>(
7374
};
7475

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

8788
fn collect_bounds<'a, 'tcx>(
8889
hir_bounds: &'a [hir::GenericBound<'tcx>],
89-
context: ImpliedBoundsContext<'tcx>,
90+
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
91+
context: ImpliedBoundsContext,
9092
target_did: DefId,
9193
) -> CollectedBound {
9294
let mut collect_into = CollectedBound::default();
93-
search_bounds_for(hir_bounds, context, |ptr| {
95+
search_bounds_for(hir_bounds, where_bounds, context, |ptr| {
9496
if !matches!(ptr.trait_ref.path.res, Res::Def(DefKind::Trait, did) if did == target_did) {
9597
return;
9698
}
@@ -107,17 +109,18 @@ fn collect_bounds<'a, 'tcx>(
107109
fn collect_sizedness_bounds<'tcx>(
108110
tcx: TyCtxt<'tcx>,
109111
hir_bounds: &'tcx [hir::GenericBound<'tcx>],
110-
context: ImpliedBoundsContext<'tcx>,
112+
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
113+
context: ImpliedBoundsContext,
111114
span: Span,
112115
) -> CollectedSizednessBounds {
113116
let sized_did = tcx.require_lang_item(hir::LangItem::Sized, span);
114-
let sized = collect_bounds(hir_bounds, context, sized_did);
117+
let sized = collect_bounds(hir_bounds, where_bounds, context, sized_did);
115118

116119
let meta_sized_did = tcx.require_lang_item(hir::LangItem::MetaSized, span);
117-
let meta_sized = collect_bounds(hir_bounds, context, meta_sized_did);
120+
let meta_sized = collect_bounds(hir_bounds, where_bounds, context, meta_sized_did);
118121

119122
let pointee_sized_did = tcx.require_lang_item(hir::LangItem::PointeeSized, span);
120-
let pointee_sized = collect_bounds(hir_bounds, context, pointee_sized_did);
123+
let pointee_sized = collect_bounds(hir_bounds, where_bounds, context, pointee_sized_did);
121124

122125
CollectedSizednessBounds { sized, meta_sized, pointee_sized }
123126
}
@@ -150,7 +153,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
150153
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
151154
self_ty: Ty<'tcx>,
152155
hir_bounds: &'tcx [hir::GenericBound<'tcx>],
153-
context: ImpliedBoundsContext<'tcx>,
156+
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
157+
context: ImpliedBoundsContext,
154158
span: Span,
155159
) {
156160
let tcx = self.tcx();
@@ -181,7 +185,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
181185
}
182186
}
183187

184-
let collected = collect_sizedness_bounds(tcx, hir_bounds, context, span);
188+
let collected = collect_sizedness_bounds(tcx, hir_bounds, where_bounds, context, span);
185189
if (collected.sized.maybe || collected.sized.negative)
186190
&& !collected.sized.positive
187191
&& !collected.meta_sized.any()
@@ -213,11 +217,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
213217
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
214218
self_ty: Ty<'tcx>,
215219
hir_bounds: &[hir::GenericBound<'tcx>],
216-
context: ImpliedBoundsContext<'tcx>,
220+
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
221+
context: ImpliedBoundsContext,
217222
span: Span,
218223
) {
219224
self.tcx().default_traits().iter().for_each(|default_trait| {
220-
self.add_default_trait(*default_trait, bounds, self_ty, hir_bounds, context, span);
225+
self.add_default_trait(
226+
*default_trait,
227+
bounds,
228+
self_ty,
229+
hir_bounds,
230+
where_bounds,
231+
context,
232+
span,
233+
);
221234
});
222235
}
223236

@@ -230,7 +243,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
230243
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
231244
self_ty: Ty<'tcx>,
232245
hir_bounds: &[hir::GenericBound<'tcx>],
233-
context: ImpliedBoundsContext<'tcx>,
246+
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
247+
context: ImpliedBoundsContext,
234248
span: Span,
235249
) {
236250
let tcx = self.tcx();
@@ -244,7 +258,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
244258
}
245259

246260
if let Some(trait_did) = tcx.lang_items().get(trait_)
247-
&& self.should_add_default_traits(trait_did, hir_bounds, context)
261+
&& self.should_add_default_traits(trait_did, hir_bounds, where_bounds, context)
248262
{
249263
add_trait_bound(tcx, bounds, self_ty, trait_did, span);
250264
}
@@ -255,9 +269,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
255269
&self,
256270
trait_def_id: DefId,
257271
hir_bounds: &'a [hir::GenericBound<'tcx>],
258-
context: ImpliedBoundsContext<'tcx>,
272+
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
273+
context: ImpliedBoundsContext,
259274
) -> bool {
260-
let collected = collect_bounds(hir_bounds, context, trait_def_id);
275+
let collected = collect_bounds(hir_bounds, where_bounds, context, trait_def_id);
261276
!find_attr!(self.tcx(), crate, RustcNoImplicitBounds) && !collected.any()
262277
}
263278

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_trait.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
8383
.iter()
8484
.map(|&trait_ref| hir::GenericBound::Trait(trait_ref))
8585
.collect::<Vec<_>>(),
86+
&[],
8687
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
8788
span,
8889
);

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ use crate::{NoVariantNamed, check_c_variadic_abi};
5959
/// The context in which an implied bound is being added to a item being lowered (i.e. a sizedness
6060
/// trait or a default trait)
6161
#[derive(Clone, Copy)]
62-
pub(crate) enum ImpliedBoundsContext<'tcx> {
62+
pub(crate) enum ImpliedBoundsContext {
6363
/// An implied bound is added to a trait definition (i.e. a new supertrait), used when adding
6464
/// a default `MetaSized` supertrait
6565
TraitDef(LocalDefId),
6666
/// An implied bound is added to a type parameter
67-
TyParam(LocalDefId, &'tcx [hir::WherePredicate<'tcx>]),
67+
TyParam(LocalDefId),
6868
/// An implied bound being added in any other context
6969
AssociatedTypeOrImplTrait,
7070
}
@@ -3143,6 +3143,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
31433143
&mut bounds,
31443144
self_ty,
31453145
hir_bounds,
3146+
&[],
31463147
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
31473148
hir_ty.span,
31483149
);

0 commit comments

Comments
 (0)