Skip to content

Commit 7500e09

Browse files
Move trait bound modifiers into hir::PolyTraitRef
1 parent f6648f2 commit 7500e09

File tree

32 files changed

+102
-100
lines changed

32 files changed

+102
-100
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+20-23
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12251225
itctx,
12261226
TraitBoundModifiers::NONE,
12271227
);
1228-
let bound = (bound, hir::TraitBoundModifier::None);
12291228
let bounds = this.arena.alloc_from_iter([bound]);
12301229
let lifetime_bound = this.elided_dyn_bound(t.span);
12311230
(bounds, lifetime_bound)
@@ -1328,8 +1327,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13281327
// const trait bounds in trait object types.
13291328
GenericBound::Trait(ty, modifiers) => {
13301329
let trait_ref = this.lower_poly_trait_ref(ty, itctx, *modifiers);
1331-
let polarity = this.lower_trait_bound_modifiers(*modifiers);
1332-
Some((trait_ref, polarity))
1330+
Some(trait_ref)
13331331
}
13341332
GenericBound::Outlives(lifetime) => {
13351333
if lifetime_bound.is_none() {
@@ -1958,21 +1956,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19581956
span_ext: DUMMY_SP,
19591957
});
19601958

1961-
hir::GenericBound::Trait(
1962-
hir::PolyTraitRef {
1963-
bound_generic_params: &[],
1964-
trait_ref: hir::TraitRef {
1965-
path: self.make_lang_item_path(
1966-
trait_lang_item,
1967-
opaque_ty_span,
1968-
Some(bound_args),
1969-
),
1970-
hir_ref_id: self.next_id(),
1971-
},
1972-
span: opaque_ty_span,
1959+
hir::GenericBound::Trait(hir::PolyTraitRef {
1960+
bound_generic_params: &[],
1961+
modifiers: hir::TraitBoundModifier::None,
1962+
trait_ref: hir::TraitRef {
1963+
path: self.make_lang_item_path(trait_lang_item, opaque_ty_span, Some(bound_args)),
1964+
hir_ref_id: self.next_id(),
19731965
},
1974-
hir::TraitBoundModifier::None,
1975-
)
1966+
span: opaque_ty_span,
1967+
})
19761968
}
19771969

19781970
#[instrument(level = "trace", skip(self))]
@@ -1982,10 +1974,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19821974
itctx: ImplTraitContext,
19831975
) -> hir::GenericBound<'hir> {
19841976
match tpb {
1985-
GenericBound::Trait(p, modifiers) => hir::GenericBound::Trait(
1986-
self.lower_poly_trait_ref(p, itctx, *modifiers),
1987-
self.lower_trait_bound_modifiers(*modifiers),
1988-
),
1977+
GenericBound::Trait(p, modifiers) => {
1978+
hir::GenericBound::Trait(self.lower_poly_trait_ref(p, itctx, *modifiers))
1979+
}
19891980
GenericBound::Outlives(lifetime) => {
19901981
hir::GenericBound::Outlives(self.lower_lifetime(lifetime))
19911982
}
@@ -2194,7 +2185,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21942185
let bound_generic_params =
21952186
self.lower_lifetime_binder(p.trait_ref.ref_id, &p.bound_generic_params);
21962187
let trait_ref = self.lower_trait_ref(modifiers, &p.trait_ref, itctx);
2197-
hir::PolyTraitRef { bound_generic_params, trait_ref, span: self.lower_span(p.span) }
2188+
let modifiers = self.lower_trait_bound_modifiers(modifiers);
2189+
hir::PolyTraitRef {
2190+
bound_generic_params,
2191+
modifiers,
2192+
trait_ref,
2193+
span: self.lower_span(p.span),
2194+
}
21982195
}
21992196

22002197
fn lower_mt(&mut self, mt: &MutTy, itctx: ImplTraitContext) -> hir::MutTy<'hir> {
@@ -2634,10 +2631,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
26342631
Res::Def(DefKind::Trait | DefKind::TraitAlias, _) => {
26352632
let principal = hir::PolyTraitRef {
26362633
bound_generic_params: &[],
2634+
modifiers: hir::TraitBoundModifier::None,
26372635
trait_ref: hir::TraitRef { path, hir_ref_id: hir_id },
26382636
span: self.lower_span(span),
26392637
};
2640-
let principal = (principal, hir::TraitBoundModifier::None);
26412638

26422639
// The original ID is taken by the `PolyTraitRef`,
26432640
// so the `Ty` itself needs a different one.

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
254254
debug!(?hrtb_bounds);
255255

256256
hrtb_bounds.iter().for_each(|bound| {
257-
let Trait(PolyTraitRef { trait_ref, span: trait_span, .. }, _) = bound else {
257+
let Trait(PolyTraitRef { trait_ref, span: trait_span, .. }) = bound else {
258258
return;
259259
};
260260
diag.span_note(*trait_span, fluent::borrowck_limitations_implies_static);
@@ -277,7 +277,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
277277
return;
278278
};
279279
bounds.iter().for_each(|bd| {
280-
if let Trait(PolyTraitRef { trait_ref: tr_ref, .. }, _) = bd
280+
if let Trait(PolyTraitRef { trait_ref: tr_ref, .. }) = bd
281281
&& let Def(_, res_defid) = tr_ref.path.res
282282
&& res_defid == trait_res_defid // trait id matches
283283
&& let TyKind::Path(Resolved(_, path)) = bounded_ty.kind

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
837837
hir_ty
838838
);
839839
};
840-
if let hir::OpaqueTy { bounds: [hir::GenericBound::Trait(trait_ref, _)], .. } = opaque_ty
840+
if let hir::OpaqueTy { bounds: [hir::GenericBound::Trait(trait_ref)], .. } = opaque_ty
841841
&& let Some(segment) = trait_ref.trait_ref.path.segments.last()
842842
&& let Some(args) = segment.args
843843
&& let [constraint] = args.constraints

compiler/rustc_hir/src/hir.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -520,15 +520,15 @@ pub enum TraitBoundModifier {
520520

521521
#[derive(Clone, Copy, Debug, HashStable_Generic)]
522522
pub enum GenericBound<'hir> {
523-
Trait(PolyTraitRef<'hir>, TraitBoundModifier),
523+
Trait(PolyTraitRef<'hir>),
524524
Outlives(&'hir Lifetime),
525525
Use(&'hir [PreciseCapturingArg<'hir>], Span),
526526
}
527527

528528
impl GenericBound<'_> {
529529
pub fn trait_ref(&self) -> Option<&TraitRef<'_>> {
530530
match self {
531-
GenericBound::Trait(data, _) => Some(&data.trait_ref),
531+
GenericBound::Trait(data) => Some(&data.trait_ref),
532532
_ => None,
533533
}
534534
}
@@ -2874,11 +2874,7 @@ pub enum TyKind<'hir> {
28742874
OpaqueDef(&'hir OpaqueTy<'hir>, &'hir [GenericArg<'hir>]),
28752875
/// A trait object type `Bound1 + Bound2 + Bound3`
28762876
/// where `Bound` is a trait or a lifetime.
2877-
TraitObject(
2878-
&'hir [(PolyTraitRef<'hir>, TraitBoundModifier)],
2879-
&'hir Lifetime,
2880-
TraitObjectSyntax,
2881-
),
2877+
TraitObject(&'hir [PolyTraitRef<'hir>], &'hir Lifetime, TraitObjectSyntax),
28822878
/// Unused for now.
28832879
Typeof(&'hir AnonConst),
28842880
/// `TyKind::Infer` means the type should be inferred instead of it having been
@@ -3182,6 +3178,11 @@ pub struct PolyTraitRef<'hir> {
31823178
/// The `'a` in `for<'a> Foo<&'a T>`.
31833179
pub bound_generic_params: &'hir [GenericParam<'hir>],
31843180

3181+
/// The constness and polarity of the trait ref.
3182+
///
3183+
/// The `async` modifier is lowered directly into a different trait for now.
3184+
pub modifiers: TraitBoundModifier,
3185+
31853186
/// The `Foo<&'a T>` in `for<'a> Foo<&'a T>`.
31863187
pub trait_ref: TraitRef<'hir>,
31873188

compiler/rustc_hir/src/intravisit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) -> V::Resul
905905
try_visit!(visitor.visit_array_length(length));
906906
}
907907
TyKind::TraitObject(bounds, ref lifetime, _syntax) => {
908-
for (bound, _modifier) in bounds {
908+
for bound in bounds {
909909
try_visit!(visitor.visit_poly_trait_ref(bound));
910910
}
911911
try_visit!(visitor.visit_lifetime(lifetime));
@@ -1160,7 +1160,7 @@ pub fn walk_param_bound<'v, V: Visitor<'v>>(
11601160
bound: &'v GenericBound<'v>,
11611161
) -> V::Result {
11621162
match *bound {
1163-
GenericBound::Trait(ref typ, _modifier) => visitor.visit_poly_trait_ref(typ),
1163+
GenericBound::Trait(ref typ) => visitor.visit_poly_trait_ref(typ),
11641164
GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
11651165
GenericBound::Use(args, _) => {
11661166
walk_list!(visitor, visit_precise_capturing_arg, args);

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GATArgsCollector<'tcx> {
832832

833833
fn could_be_self(trait_def_id: LocalDefId, ty: &hir::Ty<'_>) -> bool {
834834
match ty.kind {
835-
hir::TyKind::TraitObject([(trait_ref, _)], ..) => match trait_ref.trait_ref.path.segments {
835+
hir::TyKind::TraitObject([trait_ref], ..) => match trait_ref.trait_ref.path.segments {
836836
[s] => s.res.opt_def_id() == Some(trait_def_id.to_def_id()),
837837
_ => false,
838838
},

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ impl<'tcx> ItemCtxt<'tcx> {
866866
#[instrument(level = "trace", skip(self))]
867867
fn bound_defines_assoc_item(&self, b: &hir::GenericBound<'_>, assoc_name: Ident) -> bool {
868868
match b {
869-
hir::GenericBound::Trait(poly_trait_ref, _) => {
869+
hir::GenericBound::Trait(poly_trait_ref) => {
870870
let trait_ref = &poly_trait_ref.trait_ref;
871871
if let Some(trait_did) = trait_ref.trait_def_id() {
872872
self.tcx.trait_may_define_assoc_item(trait_did, assoc_name)

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
644644
debug!(?bounds, ?lifetime, "TraitObject");
645645
let scope = Scope::TraitRefBoundary { s: self.scope };
646646
self.with(scope, |this| {
647-
for (bound, _) in bounds {
647+
for bound in bounds {
648648
this.visit_poly_trait_ref_inner(
649649
bound,
650650
NonLifetimeBinderAllowed::Deny("trait object types"),
@@ -1918,7 +1918,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
19181918
return None;
19191919
}
19201920
predicate.bounds.iter().find_map(|bound| {
1921-
let hir::GenericBound::Trait(trait_, _) = bound else {
1921+
let hir::GenericBound::Trait(trait_) = bound else {
19221922
return None;
19231923
};
19241924
BoundVarContext::supertrait_hrtb_vars(

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
4444
let mut unbounds: SmallVec<[_; 1]> = SmallVec::new();
4545
let mut search_bounds = |hir_bounds: &'tcx [hir::GenericBound<'tcx>]| {
4646
for hir_bound in hir_bounds {
47-
let hir::GenericBound::Trait(ptr, modifier) = hir_bound else {
47+
let hir::GenericBound::Trait(ptr) = hir_bound else {
4848
continue;
4949
};
50-
match modifier {
50+
match ptr.modifiers {
5151
hir::TraitBoundModifier::Maybe => unbounds.push(ptr),
5252
hir::TraitBoundModifier::Negative => {
5353
if let Some(sized_def_id) = sized_def_id
@@ -156,8 +156,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
156156
{
157157
for hir_bound in hir_bounds {
158158
match hir_bound {
159-
hir::GenericBound::Trait(poly_trait_ref, modifier) => {
160-
let (constness, polarity) = match modifier {
159+
hir::GenericBound::Trait(poly_trait_ref) => {
160+
let (constness, polarity) = match poly_trait_ref.modifiers {
161161
hir::TraitBoundModifier::Const => {
162162
(ty::BoundConstness::Const, ty::PredicatePolarity::Positive)
163163
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
3030
&self,
3131
span: Span,
3232
hir_id: hir::HirId,
33-
hir_trait_bounds: &[(hir::PolyTraitRef<'tcx>, hir::TraitBoundModifier)],
33+
hir_trait_bounds: &[hir::PolyTraitRef<'tcx>],
3434
lifetime: &hir::Lifetime,
3535
representation: DynKind,
3636
) -> Ty<'tcx> {
@@ -39,8 +39,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
3939
let mut bounds = Bounds::default();
4040
let mut potential_assoc_types = Vec::new();
4141
let dummy_self = self.tcx().types.trait_object_dummy_self;
42-
for (trait_bound, modifier) in hir_trait_bounds.iter().rev() {
43-
if *modifier == hir::TraitBoundModifier::Maybe {
42+
for trait_bound in hir_trait_bounds.iter().rev() {
43+
// FIXME: This doesn't handle `? const`.
44+
if trait_bound.modifiers == hir::TraitBoundModifier::Maybe {
4445
continue;
4546
}
4647
if let GenericArgCountResult {
@@ -263,7 +264,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
263264
let args = tcx.mk_args(&args);
264265

265266
let span = i.bottom().1;
266-
let empty_generic_args = hir_trait_bounds.iter().any(|(hir_bound, _)| {
267+
let empty_generic_args = hir_trait_bounds.iter().any(|hir_bound| {
267268
hir_bound.trait_ref.path.res == Res::Def(DefKind::Trait, trait_ref.def_id)
268269
&& hir_bound.span.contains(span)
269270
});

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
718718
&self,
719719
associated_types: FxIndexMap<Span, FxIndexSet<DefId>>,
720720
potential_assoc_types: Vec<usize>,
721-
trait_bounds: &[(hir::PolyTraitRef<'_>, hir::TraitBoundModifier)],
721+
trait_bounds: &[hir::PolyTraitRef<'_>],
722722
) {
723723
if associated_types.values().all(|v| v.is_empty()) {
724724
return;
@@ -764,12 +764,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
764764
// related to issue #91997, turbofishes added only when in an expr or pat
765765
let mut in_expr_or_pat = false;
766766
if let ([], [bound]) = (&potential_assoc_types[..], &trait_bounds) {
767-
let grandparent = tcx.parent_hir_node(tcx.parent_hir_id(bound.0.trait_ref.hir_ref_id));
767+
let grandparent = tcx.parent_hir_node(tcx.parent_hir_id(bound.trait_ref.hir_ref_id));
768768
in_expr_or_pat = match grandparent {
769769
hir::Node::Expr(_) | hir::Node::Pat(_) => true,
770770
_ => false,
771771
};
772-
match bound.0.trait_ref.path.segments {
772+
match bound.trait_ref.path.segments {
773773
// FIXME: `trait_ref.path.span` can point to a full path with multiple
774774
// segments, even though `trait_ref.path.segments` is of length `1`. Work
775775
// around that bug here, even though it should be fixed elsewhere.
@@ -810,7 +810,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
810810
// and we can then use their span to indicate this to the user.
811811
let bound_names = trait_bounds
812812
.iter()
813-
.filter_map(|(poly_trait_ref, _)| {
813+
.filter_map(|poly_trait_ref| {
814814
let path = poly_trait_ref.trait_ref.path.segments.last()?;
815815
let args = path.args?;
816816

compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
5050
.ok()
5151
.is_some_and(|s| s.trim_end().ends_with('<'));
5252

53-
let is_global = poly_trait_ref.0.trait_ref.path.is_global();
53+
let is_global = poly_trait_ref.trait_ref.path.is_global();
5454

5555
let mut sugg = vec![(
5656
self_ty.span.shrink_to_lo(),
@@ -211,7 +211,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
211211
// Check if trait object is safe for suggesting dynamic dispatch.
212212
let is_dyn_compatible = match self_ty.kind {
213213
hir::TyKind::TraitObject(objects, ..) => {
214-
objects.iter().all(|(o, _)| match o.trait_ref.path.res {
214+
objects.iter().all(|o| match o.trait_ref.path.res {
215215
Res::Def(DefKind::Trait, id) => tcx.is_dyn_compatible(id),
216216
_ => false,
217217
})

compiler/rustc_hir_pretty/src/lib.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -301,16 +301,13 @@ impl<'a> State<'a> {
301301
self.word_space("dyn");
302302
}
303303
let mut first = true;
304-
for (bound, modifier) in bounds {
304+
for bound in bounds {
305305
if first {
306306
first = false;
307307
} else {
308308
self.nbsp();
309309
self.word_space("+");
310310
}
311-
if *modifier == TraitBoundModifier::Maybe {
312-
self.word("?");
313-
}
314311
self.print_poly_trait_ref(bound);
315312
}
316313
if !lifetime.is_elided() {
@@ -679,6 +676,10 @@ impl<'a> State<'a> {
679676
}
680677

681678
fn print_poly_trait_ref(&mut self, t: &hir::PolyTraitRef<'_>) {
679+
// FIXME: This isn't correct!
680+
if t.modifiers == TraitBoundModifier::Maybe {
681+
self.word("?");
682+
}
682683
self.print_formal_generic_params(t.bound_generic_params);
683684
self.print_trait_ref(&t.trait_ref);
684685
}
@@ -2077,10 +2078,7 @@ impl<'a> State<'a> {
20772078
}
20782079

20792080
match bound {
2080-
GenericBound::Trait(tref, modifier) => {
2081-
if modifier == &TraitBoundModifier::Maybe {
2082-
self.word("?");
2083-
}
2081+
GenericBound::Trait(tref) => {
20842082
self.print_poly_trait_ref(tref);
20852083
}
20862084
GenericBound::Outlives(lt) => {

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
849849
hir::FnRetTy::Return(hir_ty) => {
850850
if let hir::TyKind::OpaqueDef(op_ty, ..) = hir_ty.kind
851851
// FIXME: account for RPITIT.
852-
&& let [hir::GenericBound::Trait(trait_ref, _)] = op_ty.bounds
852+
&& let [hir::GenericBound::Trait(trait_ref)] = op_ty.bounds
853853
&& let Some(hir::PathSegment { args: Some(generic_args), .. }) =
854854
trait_ref.trait_ref.path.segments.last()
855855
&& let [constraint] = generic_args.constraints
@@ -1035,7 +1035,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10351035
// extract all bounds from the source code using their spans
10361036
let all_matching_bounds_strs = predicates_from_where
10371037
.filter_map(|bound| match bound {
1038-
GenericBound::Trait(_, _) => {
1038+
GenericBound::Trait(_) => {
10391039
self.tcx.sess.source_map().span_to_snippet(bound.span()).ok()
10401040
}
10411041
_ => None,

compiler/rustc_lint/src/traits.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
112112

113113
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx hir::Ty<'tcx>) {
114114
let hir::TyKind::TraitObject(bounds, _lifetime, _syntax) = &ty.kind else { return };
115-
for (bound, modifier) in &bounds[..] {
115+
for bound in &bounds[..] {
116116
let def_id = bound.trait_ref.trait_def_id();
117117
if def_id.is_some_and(|def_id| cx.tcx.is_lang_item(def_id, LangItem::Drop))
118-
&& *modifier != hir::TraitBoundModifier::Maybe
118+
// FIXME: ?Drop is not a thing.
119+
&& bound.modifiers != hir::TraitBoundModifier::Maybe
119120
{
120121
let Some(def_id) = cx.tcx.get_diagnostic_item(sym::needs_drop) else { return };
121122
cx.emit_span_lint(DYN_DROP, bound.span, DropGlue { tcx: cx.tcx, def_id });

0 commit comments

Comments
 (0)