Skip to content

Commit 0676d2e

Browse files
Revert "Rollup merge of #157702 - oli-obk:having-fun-with-expansion-info, r=davidtwco,estebank"
This reverts commit 18509d2, reversing changes made to bf0dfb6.
1 parent 8e15021 commit 0676d2e

34 files changed

Lines changed: 117 additions & 188 deletions

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 2 additions & 4 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::{DesugaringKind, sym};
27+
use rustc_span::sym;
2828
use rustc_target::spec::{AbiMap, AbiMapping};
2929
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
3030
use rustc_trait_selection::traits;
@@ -2117,9 +2117,7 @@ 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| {
2121-
!pred_sp.is_desugaring(DesugaringKind::DefaultBound { def: param.def_id })
2122-
});
2120+
|| (*bounded_params).get(&param.index).is_some_and(|&&pred_sp| pred_sp != span);
21232121
let const_param_help = !has_explicit_bounds;
21242122

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

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,14 @@ fn associated_type_bounds<'tcx>(
5858
&mut bounds,
5959
item_ty,
6060
hir_bounds,
61-
&[],
62-
ImpliedBoundsContext::AssociatedType(assoc_item_def_id),
61+
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
6362
span,
6463
);
6564
icx.lowerer().add_default_traits(
6665
&mut bounds,
6766
item_ty,
6867
hir_bounds,
69-
&[],
70-
ImpliedBoundsContext::AssociatedType(assoc_item_def_id),
68+
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
7169
span,
7270
);
7371

@@ -386,16 +384,14 @@ fn opaque_type_bounds<'tcx>(
386384
&mut bounds,
387385
item_ty,
388386
hir_bounds,
389-
&[],
390-
ImpliedBoundsContext::ImplTrait,
387+
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
391388
span,
392389
);
393390
icx.lowerer().add_default_traits(
394391
&mut bounds,
395392
item_ty,
396393
hir_bounds,
397-
&[],
398-
ImpliedBoundsContext::ImplTrait,
394+
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
399395
span,
400396
);
401397
}

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,13 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
200200
&mut bounds,
201201
tcx.types.self_param,
202202
self_bounds,
203-
&[],
204203
ImpliedBoundsContext::TraitDef(def_id),
205204
span,
206205
);
207206
icx.lowerer().add_default_traits(
208207
&mut bounds,
209208
tcx.types.self_param,
210209
self_bounds,
211-
&[],
212210
ImpliedBoundsContext::TraitDef(def_id),
213211
span,
214212
);
@@ -241,16 +239,14 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
241239
&mut bounds,
242240
param_ty,
243241
&[],
244-
hir_generics.predicates,
245-
ImpliedBoundsContext::TyParam(param.def_id),
242+
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
246243
param.span,
247244
);
248245
icx.lowerer().add_default_traits(
249246
&mut bounds,
250247
param_ty,
251248
&[],
252-
hir_generics.predicates,
253-
ImpliedBoundsContext::TyParam(param.def_id),
249+
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
254250
param.span,
255251
);
256252
trace!(?bounds);
@@ -696,15 +692,13 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
696692
&mut bounds,
697693
self_param_ty,
698694
superbounds,
699-
&[],
700695
ImpliedBoundsContext::TraitDef(trait_def_id),
701696
item.span,
702697
);
703698
icx.lowerer().add_default_traits(
704699
&mut bounds,
705700
self_param_ty,
706701
superbounds,
707-
&[],
708702
ImpliedBoundsContext::TraitDef(trait_def_id),
709703
item.span,
710704
);
@@ -1000,16 +994,14 @@ impl<'tcx> ItemCtxt<'tcx> {
1000994
&mut bounds,
1001995
param_ty,
1002996
&[],
1003-
hir_generics.predicates,
1004-
ImpliedBoundsContext::TyParam(param.def_id),
997+
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
1005998
param.span,
1006999
);
10071000
self.lowerer().add_default_traits(
10081001
&mut bounds,
10091002
param_ty,
10101003
&[],
1011-
hir_generics.predicates,
1012-
ImpliedBoundsContext::TyParam(param.def_id),
1004+
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
10131005
param.span,
10141006
);
10151007
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 31 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_middle::ty::{
1212
self as ty, IsSuggestable, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
1313
TypeVisitor, Upcast,
1414
};
15-
use rustc_span::{DesugaringKind, ErrorGuaranteed, Ident, Span, kw};
15+
use rustc_span::{ErrorGuaranteed, Ident, Span, kw};
1616
use rustc_trait_selection::traits;
1717
use tracing::{debug, instrument};
1818

@@ -25,17 +25,17 @@ use crate::hir_ty_lowering::{
2525
#[derive(Debug, Default)]
2626
struct CollectedBound {
2727
/// `Trait`
28-
positive: Option<Span>,
28+
positive: bool,
2929
/// `?Trait`
30-
maybe: Option<Span>,
30+
maybe: bool,
3131
/// `!Trait`
32-
negative: Option<Span>,
32+
negative: bool,
3333
}
3434

3535
impl CollectedBound {
3636
/// Returns `true` if any of `Trait`, `?Trait` or `!Trait` were encountered.
3737
fn any(&self) -> bool {
38-
self.positive.is_some() || self.maybe.is_some() || self.negative.is_some()
38+
self.positive || self.maybe || self.negative
3939
}
4040
}
4141

@@ -59,8 +59,7 @@ impl CollectedSizednessBounds {
5959

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

7675
search_bounds(hir_bounds);
77-
if let ImpliedBoundsContext::TyParam(self_ty) = context {
78-
for clause in where_bounds {
76+
if let ImpliedBoundsContext::TyParam(self_ty, where_clause) = context {
77+
for clause in where_clause {
7978
if let hir::WherePredicateKind::BoundPredicate(pred) = clause.kind
8079
&& pred.is_param_bound(self_ty.to_def_id())
8180
{
@@ -87,20 +86,19 @@ fn search_bounds_for<'tcx>(
8786

8887
fn collect_bounds<'a, 'tcx>(
8988
hir_bounds: &'a [hir::GenericBound<'tcx>],
90-
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
91-
context: ImpliedBoundsContext,
89+
context: ImpliedBoundsContext<'tcx>,
9290
target_did: DefId,
9391
) -> CollectedBound {
9492
let mut collect_into = CollectedBound::default();
95-
search_bounds_for(hir_bounds, where_bounds, context, |ptr| {
93+
search_bounds_for(hir_bounds, context, |ptr| {
9694
if !matches!(ptr.trait_ref.path.res, Res::Def(DefKind::Trait, did) if did == target_did) {
9795
return;
9896
}
9997

10098
match ptr.modifiers.polarity {
101-
hir::BoundPolarity::Maybe(_) => collect_into.maybe = Some(ptr.span),
102-
hir::BoundPolarity::Negative(_) => collect_into.negative = Some(ptr.span),
103-
hir::BoundPolarity::Positive => collect_into.positive = Some(ptr.span),
99+
hir::BoundPolarity::Maybe(_) => collect_into.maybe = true,
100+
hir::BoundPolarity::Negative(_) => collect_into.negative = true,
101+
hir::BoundPolarity::Positive => collect_into.positive = true,
104102
}
105103
});
106104
collect_into
@@ -109,18 +107,17 @@ fn collect_bounds<'a, 'tcx>(
109107
fn collect_sizedness_bounds<'tcx>(
110108
tcx: TyCtxt<'tcx>,
111109
hir_bounds: &'tcx [hir::GenericBound<'tcx>],
112-
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
113-
context: ImpliedBoundsContext,
110+
context: ImpliedBoundsContext<'tcx>,
114111
span: Span,
115112
) -> CollectedSizednessBounds {
116113
let sized_did = tcx.require_lang_item(hir::LangItem::Sized, span);
117-
let sized = collect_bounds(hir_bounds, where_bounds, context, sized_did);
114+
let sized = collect_bounds(hir_bounds, context, sized_did);
118115

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

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

125122
CollectedSizednessBounds { sized, meta_sized, pointee_sized }
126123
}
@@ -153,8 +150,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
153150
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
154151
self_ty: Ty<'tcx>,
155152
hir_bounds: &'tcx [hir::GenericBound<'tcx>],
156-
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
157-
context: ImpliedBoundsContext,
153+
context: ImpliedBoundsContext<'tcx>,
158154
span: Span,
159155
) {
160156
let tcx = self.tcx();
@@ -181,48 +177,28 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
181177
return;
182178
}
183179
}
184-
ImpliedBoundsContext::TyParam(..)
185-
| ImpliedBoundsContext::AssociatedType(..)
186-
| ImpliedBoundsContext::TraitObject
187-
| ImpliedBoundsContext::TraitAscription
188-
| ImpliedBoundsContext::ImplTrait => {}
180+
ImpliedBoundsContext::TyParam(..) | ImpliedBoundsContext::AssociatedTypeOrImplTrait => {
181+
}
189182
}
190-
let collected = collect_sizedness_bounds(tcx, hir_bounds, where_bounds, context, span);
191-
if let Some(span) = collected.sized.maybe.or(collected.sized.negative)
192-
&& collected.sized.positive.is_none()
183+
184+
let collected = collect_sizedness_bounds(tcx, hir_bounds, context, span);
185+
if (collected.sized.maybe || collected.sized.negative)
186+
&& !collected.sized.positive
193187
&& !collected.meta_sized.any()
194188
&& !collected.pointee_sized.any()
195189
{
196190
// `?Sized` is equivalent to `MetaSized` (but only add the bound if there aren't any
197191
// other explicit ones) - this can happen for trait aliases as well as bounds.
198192
add_trait_bound(tcx, bounds, self_ty, meta_sized_did, span);
199193
} else if !collected.any() {
200-
let span = match context {
201-
ImpliedBoundsContext::TraitDef(def)
202-
| ImpliedBoundsContext::TyParam(def)
203-
| ImpliedBoundsContext::AssociatedType(def) => {
204-
self.tcx().with_stable_hashing_context(|hcx| {
205-
span.mark_with_reason(
206-
None,
207-
DesugaringKind::DefaultBound { def: def.into() },
208-
span.edition(),
209-
hcx,
210-
)
211-
})
212-
}
213-
_ => span,
214-
};
215194
match context {
216195
ImpliedBoundsContext::TraitDef(..) => {
217196
// If there are no explicit sizedness bounds on a trait then add a default
218197
// `MetaSized` supertrait.
219198
add_trait_bound(tcx, bounds, self_ty, meta_sized_did, span);
220199
}
221200
ImpliedBoundsContext::TyParam(..)
222-
| ImpliedBoundsContext::AssociatedType(..)
223-
| ImpliedBoundsContext::TraitObject
224-
| ImpliedBoundsContext::TraitAscription
225-
| ImpliedBoundsContext::ImplTrait => {
201+
| ImpliedBoundsContext::AssociatedTypeOrImplTrait => {
226202
// If there are no explicit sizedness bounds on a parameter then add a default
227203
// `Sized` bound.
228204
let sized_did = tcx.require_lang_item(hir::LangItem::Sized, span);
@@ -237,20 +213,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
237213
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
238214
self_ty: Ty<'tcx>,
239215
hir_bounds: &[hir::GenericBound<'tcx>],
240-
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
241-
context: ImpliedBoundsContext,
216+
context: ImpliedBoundsContext<'tcx>,
242217
span: Span,
243218
) {
244219
self.tcx().default_traits().iter().for_each(|default_trait| {
245-
self.add_default_trait(
246-
*default_trait,
247-
bounds,
248-
self_ty,
249-
hir_bounds,
250-
where_bounds,
251-
context,
252-
span,
253-
);
220+
self.add_default_trait(*default_trait, bounds, self_ty, hir_bounds, context, span);
254221
});
255222
}
256223

@@ -263,8 +230,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
263230
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
264231
self_ty: Ty<'tcx>,
265232
hir_bounds: &[hir::GenericBound<'tcx>],
266-
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
267-
context: ImpliedBoundsContext,
233+
context: ImpliedBoundsContext<'tcx>,
268234
span: Span,
269235
) {
270236
let tcx = self.tcx();
@@ -278,7 +244,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
278244
}
279245

280246
if let Some(trait_did) = tcx.lang_items().get(trait_)
281-
&& self.should_add_default_traits(trait_did, hir_bounds, where_bounds, context)
247+
&& self.should_add_default_traits(trait_did, hir_bounds, context)
282248
{
283249
add_trait_bound(tcx, bounds, self_ty, trait_did, span);
284250
}
@@ -289,10 +255,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
289255
&self,
290256
trait_def_id: DefId,
291257
hir_bounds: &'a [hir::GenericBound<'tcx>],
292-
where_bounds: &'tcx [hir::WherePredicate<'tcx>],
293-
context: ImpliedBoundsContext,
258+
context: ImpliedBoundsContext<'tcx>,
294259
) -> bool {
295-
let collected = collect_bounds(hir_bounds, where_bounds, context, trait_def_id);
260+
let collected = collect_bounds(hir_bounds, context, trait_def_id);
296261
!find_attr!(self.tcx(), crate, RustcNoImplicitBounds) && !collected.any()
297262
}
298263

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_trait.rs

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

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,14 @@ 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 {
62+
pub(crate) enum ImpliedBoundsContext<'tcx> {
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),
68-
/// Associated type bounds
69-
AssociatedType(LocalDefId),
70-
ImplTrait,
71-
TraitObject,
72-
TraitAscription,
67+
TyParam(LocalDefId, &'tcx [hir::WherePredicate<'tcx>]),
68+
/// An implied bound being added in any other context
69+
AssociatedTypeOrImplTrait,
7370
}
7471

7572
/// A path segment that is semantically allowed to have generic arguments.
@@ -3133,8 +3130,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
31333130
&mut bounds,
31343131
self_ty,
31353132
hir_bounds,
3136-
&[],
3137-
ImpliedBoundsContext::TraitAscription,
3133+
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
31383134
hir_ty.span,
31393135
);
31403136
self.register_trait_ascription_bounds(bounds, hir_ty.hir_id, hir_ty.span);

0 commit comments

Comments
 (0)