Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 38a2a65

Browse files
committedFeb 17, 2024
Remove astconv::ConvertedBinding
1 parent f62a695 commit 38a2a65

File tree

3 files changed

+96
-151
lines changed

3 files changed

+96
-151
lines changed
 

‎compiler/rustc_hir_analysis/src/astconv/bounds.rs

Lines changed: 65 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ use rustc_span::{ErrorGuaranteed, Span};
99
use rustc_trait_selection::traits;
1010
use smallvec::SmallVec;
1111

12-
use crate::astconv::{
13-
AstConv, ConvertedBinding, ConvertedBindingKind, OnlySelfBounds, PredicateFilter,
14-
};
12+
use crate::astconv::{AstConv, OnlySelfBounds, PredicateFilter};
1513
use crate::bounds::Bounds;
1614
use crate::errors;
1715

@@ -238,7 +236,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
238236
&self,
239237
hir_ref_id: hir::HirId,
240238
trait_ref: ty::PolyTraitRef<'tcx>,
241-
binding: &ConvertedBinding<'_, 'tcx>,
239+
binding: &hir::TypeBinding<'tcx>,
242240
bounds: &mut Bounds<'tcx>,
243241
speculative: bool,
244242
dup_bindings: &mut FxIndexMap<DefId, Span>,
@@ -263,21 +261,20 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
263261

264262
let tcx = self.tcx();
265263

266-
let assoc_kind =
267-
if binding.gen_args.parenthesized == hir::GenericArgsParentheses::ReturnTypeNotation {
268-
ty::AssocKind::Fn
269-
} else if let ConvertedBindingKind::Equality(term) = binding.kind
270-
&& let ty::TermKind::Const(_) = term.node.unpack()
271-
{
272-
ty::AssocKind::Const
273-
} else {
274-
ty::AssocKind::Type
275-
};
264+
let assoc_kind = if binding.gen_args.parenthesized
265+
== hir::GenericArgsParentheses::ReturnTypeNotation
266+
{
267+
ty::AssocKind::Fn
268+
} else if let hir::TypeBindingKind::Equality { term: hir::Term::Const(_) } = binding.kind {
269+
ty::AssocKind::Const
270+
} else {
271+
ty::AssocKind::Type
272+
};
276273

277274
let candidate = if self.trait_defines_associated_item_named(
278275
trait_ref.def_id(),
279276
assoc_kind,
280-
binding.item_name,
277+
binding.ident,
281278
) {
282279
// Simple case: The assoc item is defined in the current trait.
283280
trait_ref
@@ -289,14 +286,14 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
289286
trait_ref.skip_binder().print_only_trait_name(),
290287
None,
291288
assoc_kind,
292-
binding.item_name,
289+
binding.ident,
293290
path_span,
294-
Some(&binding),
291+
Some(binding),
295292
)?
296293
};
297294

298295
let (assoc_ident, def_scope) =
299-
tcx.adjust_ident_and_get_scope(binding.item_name, candidate.def_id(), hir_ref_id);
296+
tcx.adjust_ident_and_get_scope(binding.ident, candidate.def_id(), hir_ref_id);
300297

301298
// We have already adjusted the item name above, so compare with `.normalize_to_macros_2_0()`
302299
// instead of calling `filter_by_name_and_kind` which would needlessly normalize the
@@ -312,7 +309,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
312309
.dcx()
313310
.struct_span_err(
314311
binding.span,
315-
format!("{} `{}` is private", assoc_item.kind, binding.item_name),
312+
format!("{} `{}` is private", assoc_item.kind, binding.ident),
316313
)
317314
.with_span_label(binding.span, format!("private {}", assoc_item.kind))
318315
.emit();
@@ -327,7 +324,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
327324
tcx.dcx().emit_err(errors::ValueOfAssociatedStructAlreadySpecified {
328325
span: binding.span,
329326
prev_span: *prev_span,
330-
item_name: binding.item_name,
327+
item_name: binding.ident,
331328
def_path: tcx.def_path_str(assoc_item.container_id(tcx)),
332329
});
333330
})
@@ -412,7 +409,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
412409
// parent arguments (the arguments of the trait) and the own arguments (the ones of
413410
// the associated item itself) and construct an alias type using them.
414411
candidate.map_bound(|trait_ref| {
415-
let ident = Ident::new(assoc_item.name, binding.item_name.span);
412+
let ident = Ident::new(assoc_item.name, binding.ident.span);
416413
let item_segment = hir::PathSegment {
417414
ident,
418415
hir_id: binding.hir_id,
@@ -436,66 +433,67 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
436433
})
437434
};
438435

439-
if !speculative {
440-
// Find any late-bound regions declared in `ty` that are not
441-
// declared in the trait-ref or assoc_item. These are not well-formed.
442-
//
443-
// Example:
444-
//
445-
// for<'a> <T as Iterator>::Item = &'a str // <-- 'a is bad
446-
// for<'a> <T as FnMut<(&'a u32,)>>::Output = &'a str // <-- 'a is ok
447-
if let ConvertedBindingKind::Equality(term) = binding.kind {
448-
let late_bound_in_projection_ty =
449-
tcx.collect_constrained_late_bound_regions(&projection_ty);
450-
let late_bound_in_term =
451-
tcx.collect_referenced_late_bound_regions(&trait_ref.rebind(term.node));
452-
debug!(?late_bound_in_projection_ty);
453-
debug!(?late_bound_in_term);
454-
455-
// NOTE(associated_const_equality): This error should be impossible to trigger
456-
// with associated const equality bounds.
457-
// FIXME: point at the type params that don't have appropriate lifetimes:
458-
// struct S1<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
459-
// ---- ---- ^^^^^^^
460-
self.validate_late_bound_regions(
461-
late_bound_in_projection_ty,
462-
late_bound_in_term,
463-
|br_name| {
464-
struct_span_code_err!(
465-
tcx.dcx(),
466-
binding.span,
467-
E0582,
468-
"binding for associated type `{}` references {}, \
469-
which does not appear in the trait input types",
470-
binding.item_name,
471-
br_name
472-
)
473-
},
474-
);
475-
}
476-
}
477-
478436
match binding.kind {
479-
ConvertedBindingKind::Equality(..) if let ty::AssocKind::Fn = assoc_kind => {
437+
hir::TypeBindingKind::Equality { .. } if let ty::AssocKind::Fn = assoc_kind => {
480438
return Err(tcx.dcx().emit_err(crate::errors::ReturnTypeNotationEqualityBound {
481439
span: binding.span,
482440
}));
483441
}
484-
ConvertedBindingKind::Equality(term) => {
442+
hir::TypeBindingKind::Equality { term } => {
443+
let term = match term {
444+
hir::Term::Ty(ty) => self.ast_ty_to_ty(ty).into(),
445+
hir::Term::Const(ct) => ty::Const::from_anon_const(tcx, ct.def_id).into(),
446+
};
447+
448+
if !speculative {
449+
// Find any late-bound regions declared in `ty` that are not
450+
// declared in the trait-ref or assoc_item. These are not well-formed.
451+
//
452+
// Example:
453+
//
454+
// for<'a> <T as Iterator>::Item = &'a str // <-- 'a is bad
455+
// for<'a> <T as FnMut<(&'a u32,)>>::Output = &'a str // <-- 'a is ok
456+
let late_bound_in_projection_ty =
457+
tcx.collect_constrained_late_bound_regions(&projection_ty);
458+
let late_bound_in_term =
459+
tcx.collect_referenced_late_bound_regions(&trait_ref.rebind(term));
460+
debug!(?late_bound_in_projection_ty);
461+
debug!(?late_bound_in_term);
462+
463+
// FIXME: point at the type params that don't have appropriate lifetimes:
464+
// struct S1<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
465+
// ---- ---- ^^^^^^^
466+
// NOTE(associated_const_equality): This error should be impossible to trigger
467+
// with associated const equality bounds.
468+
self.validate_late_bound_regions(
469+
late_bound_in_projection_ty,
470+
late_bound_in_term,
471+
|br_name| {
472+
struct_span_code_err!(
473+
tcx.dcx(),
474+
binding.span,
475+
E0582,
476+
"binding for associated type `{}` references {}, \
477+
which does not appear in the trait input types",
478+
binding.ident,
479+
br_name
480+
)
481+
},
482+
);
483+
}
484+
485485
// "Desugar" a constraint like `T: Iterator<Item = u32>` this to
486486
// the "projection predicate" for:
487487
//
488488
// `<T as Iterator>::Item = u32`
489489
bounds.push_projection_bound(
490490
tcx,
491-
projection_ty.map_bound(|projection_ty| ty::ProjectionPredicate {
492-
projection_ty,
493-
term: term.node,
494-
}),
491+
projection_ty
492+
.map_bound(|projection_ty| ty::ProjectionPredicate { projection_ty, term }),
495493
binding.span,
496494
);
497495
}
498-
ConvertedBindingKind::Constraint(ast_bounds) => {
496+
hir::TypeBindingKind::Constraint { bounds: ast_bounds } => {
499497
// "Desugar" a constraint like `T: Iterator<Item: Debug>` to
500498
//
501499
// `<T as Iterator>::Item: Debug`

‎compiler/rustc_hir_analysis/src/astconv/errors.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::astconv::{AstConv, ConvertedBindingKind};
1+
use crate::astconv::AstConv;
22
use crate::errors::{
33
self, AssocTypeBindingNotAllowed, ManualImplementation, MissingTypeParams,
44
ParenthesizedFnTraitExpansion,
@@ -111,7 +111,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
111111
assoc_kind: ty::AssocKind,
112112
assoc_name: Ident,
113113
span: Span,
114-
binding: Option<&super::ConvertedBinding<'_, 'tcx>>,
114+
binding: Option<&hir::TypeBinding<'tcx>>,
115115
) -> ErrorGuaranteed
116116
where
117117
I: Iterator<Item = ty::PolyTraitRef<'tcx>>,
@@ -290,13 +290,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
290290
assoc_kind: ty::AssocKind,
291291
ident: Ident,
292292
span: Span,
293-
binding: Option<&super::ConvertedBinding<'_, 'tcx>>,
293+
binding: Option<&hir::TypeBinding<'tcx>>,
294294
) -> ErrorGuaranteed {
295295
let tcx = self.tcx();
296296

297297
let bound_on_assoc_const_label = if let ty::AssocKind::Const = assoc_item.kind
298298
&& let Some(binding) = binding
299-
&& let ConvertedBindingKind::Constraint(_) = binding.kind
299+
&& let hir::TypeBindingKind::Constraint { .. } = binding.kind
300300
{
301301
let lo = if binding.gen_args.span_ext.is_dummy() {
302302
ident.span
@@ -310,14 +310,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
310310

311311
// FIXME(associated_const_equality): This has quite a few false positives and negatives.
312312
let wrap_in_braces_sugg = if let Some(binding) = binding
313-
&& let ConvertedBindingKind::Equality(term) = binding.kind
314-
&& let ty::TermKind::Ty(ty) = term.node.unpack()
313+
&& let hir::TypeBindingKind::Equality { term: hir::Term::Ty(hir_ty) } = binding.kind
314+
&& let ty = self.ast_ty_to_ty(hir_ty)
315315
&& (ty.is_enum() || ty.references_error())
316316
&& tcx.features().associated_const_equality
317317
{
318318
Some(errors::AssocKindMismatchWrapInBracesSugg {
319-
lo: term.span.shrink_to_lo(),
320-
hi: term.span.shrink_to_hi(),
319+
lo: hir_ty.span.shrink_to_lo(),
320+
hi: hir_ty.span.shrink_to_hi(),
321321
})
322322
} else {
323323
None
@@ -326,9 +326,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
326326
// For equality bounds, we want to blame the term (RHS) instead of the item (LHS) since
327327
// one can argue that that's more “intuitive” to the user.
328328
let (span, expected_because_label, expected, got) = if let Some(binding) = binding
329-
&& let ConvertedBindingKind::Equality(term) = binding.kind
329+
&& let hir::TypeBindingKind::Equality { term } = binding.kind
330330
{
331-
(term.span, Some(ident.span), assoc_item.kind, assoc_kind)
331+
let span = match term {
332+
hir::Term::Ty(ty) => ty.span,
333+
hir::Term::Const(ct) => tcx.def_span(ct.def_id),
334+
};
335+
(span, Some(ident.span), assoc_item.kind, assoc_kind)
332336
} else {
333337
(ident.span, None, assoc_kind, assoc_item.kind)
334338
};

‎compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 17 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use rustc_middle::ty::{
3535
};
3636
use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
3737
use rustc_span::edit_distance::find_best_match_for_name;
38-
use rustc_span::source_map::{respan, Spanned};
3938
use rustc_span::symbol::{kw, Ident, Symbol};
4039
use rustc_span::{sym, BytePos, Span, DUMMY_SP};
4140
use rustc_target::spec::abi;
@@ -151,21 +150,6 @@ pub trait AstConv<'tcx> {
151150
fn infcx(&self) -> Option<&InferCtxt<'tcx>>;
152151
}
153152

154-
#[derive(Debug)]
155-
struct ConvertedBinding<'a, 'tcx> {
156-
hir_id: hir::HirId,
157-
item_name: Ident,
158-
kind: ConvertedBindingKind<'a, 'tcx>,
159-
gen_args: &'tcx GenericArgs<'tcx>,
160-
span: Span,
161-
}
162-
163-
#[derive(Debug)]
164-
enum ConvertedBindingKind<'a, 'tcx> {
165-
Equality(Spanned<ty::Term<'tcx>>),
166-
Constraint(&'a [hir::GenericBound<'tcx>]),
167-
}
168-
169153
/// New-typed boolean indicating whether explicit late-bound lifetimes
170154
/// are present in a set of generic arguments.
171155
///
@@ -316,7 +300,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
316300
/// Given the type/lifetime/const arguments provided to some path (along with
317301
/// an implicit `Self`, if this is a trait reference), returns the complete
318302
/// set of generic arguments. This may involve applying defaulted type parameters.
319-
/// Constraints on associated types are created from `create_assoc_bindings_for_generic_args`.
303+
///
304+
/// Constraints on associated types are not converted here but
305+
/// separately in `add_predicates_for_ast_type_binding`.
320306
///
321307
/// Example:
322308
///
@@ -329,8 +315,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
329315
/// 2. The path in question is the path to the trait `std::ops::Index`,
330316
/// which will have been resolved to a `def_id`
331317
/// 3. The `generic_args` contains info on the `<...>` contents. The `usize` type
332-
/// parameters are returned in the `GenericArgsRef`, the associated type bindings like
333-
/// `Output = u32` are returned from `create_assoc_bindings_for_generic_args`.
318+
/// parameters are returned in the `GenericArgsRef`
319+
/// 4. Associated type bindings like `Output = u32` are contained in `generic_args.bindings`.
334320
///
335321
/// Note that the type listing given here is *exactly* what the user provided.
336322
///
@@ -591,52 +577,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
591577
(args, arg_count)
592578
}
593579

594-
fn create_assoc_bindings_for_generic_args<'a>(
595-
&self,
596-
generic_args: &'a hir::GenericArgs<'tcx>,
597-
) -> Vec<ConvertedBinding<'a, 'tcx>> {
598-
// Convert associated-type bindings or constraints into a separate vector.
599-
// Example: Given this:
600-
//
601-
// T: Iterator<Item = u32>
602-
//
603-
// The `T` is passed in as a self-type; the `Item = u32` is
604-
// not a "type parameter" of the `Iterator` trait, but rather
605-
// a restriction on `<T as Iterator>::Item`, so it is passed
606-
// back separately.
607-
let assoc_bindings = generic_args
608-
.bindings
609-
.iter()
610-
.map(|binding| {
611-
let kind = match &binding.kind {
612-
hir::TypeBindingKind::Equality { term } => match term {
613-
hir::Term::Ty(ty) => ConvertedBindingKind::Equality(respan(
614-
ty.span,
615-
self.ast_ty_to_ty(ty).into(),
616-
)),
617-
hir::Term::Const(c) => {
618-
let span = self.tcx().def_span(c.def_id);
619-
let c = Const::from_anon_const(self.tcx(), c.def_id);
620-
ConvertedBindingKind::Equality(respan(span, c.into()))
621-
}
622-
},
623-
hir::TypeBindingKind::Constraint { bounds } => {
624-
ConvertedBindingKind::Constraint(bounds)
625-
}
626-
};
627-
ConvertedBinding {
628-
hir_id: binding.hir_id,
629-
item_name: binding.ident,
630-
kind,
631-
gen_args: binding.gen_args,
632-
span: binding.span,
633-
}
634-
})
635-
.collect();
636-
637-
assoc_bindings
638-
}
639-
640580
pub fn create_args_for_associated_item(
641581
&self,
642582
span: Span,
@@ -742,18 +682,16 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
742682
let bound_vars = tcx.late_bound_vars(trait_ref.hir_ref_id);
743683
debug!(?bound_vars);
744684

745-
let assoc_bindings = self.create_assoc_bindings_for_generic_args(args);
746-
747685
let poly_trait_ref = ty::Binder::bind_with_vars(
748686
ty::TraitRef::new(tcx, trait_def_id, generic_args),
749687
bound_vars,
750688
);
751689

752-
debug!(?poly_trait_ref, ?assoc_bindings);
690+
debug!(?poly_trait_ref);
753691
bounds.push_trait_bound(tcx, poly_trait_ref, span, polarity);
754692

755693
let mut dup_bindings = FxIndexMap::default();
756-
for binding in &assoc_bindings {
694+
for binding in args.bindings {
757695
// Don't register additional associated type bounds for negative bounds,
758696
// since we should have emitten an error for them earlier, and they will
759697
// not be well-formed!
@@ -1029,7 +967,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1029967
assoc_kind: ty::AssocKind,
1030968
assoc_name: Ident,
1031969
span: Span,
1032-
binding: Option<&ConvertedBinding<'_, 'tcx>>,
970+
binding: Option<&hir::TypeBinding<'tcx>>,
1033971
) -> Result<ty::PolyTraitRef<'tcx>, ErrorGuaranteed>
1034972
where
1035973
I: Iterator<Item = ty::PolyTraitRef<'tcx>>,
@@ -1069,7 +1007,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10691007
// Provide a more specific error code index entry for equality bindings.
10701008
err.code(
10711009
if let Some(binding) = binding
1072-
&& let ConvertedBindingKind::Equality(_) = binding.kind
1010+
&& let hir::TypeBindingKind::Equality { .. } = binding.kind
10731011
{
10741012
E0222
10751013
} else {
@@ -1094,16 +1032,21 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10941032
);
10951033
if let Some(binding) = binding {
10961034
match binding.kind {
1097-
ConvertedBindingKind::Equality(term) => {
1035+
hir::TypeBindingKind::Equality { term } => {
1036+
let term: ty::Term<'_> = match term {
1037+
hir::Term::Ty(ty) => self.ast_ty_to_ty(ty).into(),
1038+
hir::Term::Const(ct) => {
1039+
ty::Const::from_anon_const(tcx, ct.def_id).into()
1040+
}
1041+
};
10981042
// FIXME(#97583): This isn't syntactically well-formed!
10991043
where_bounds.push(format!(
11001044
" T: {trait}::{assoc_name} = {term}",
11011045
trait = bound.print_only_trait_path(),
1102-
term = term.node,
11031046
));
11041047
}
11051048
// FIXME: Provide a suggestion.
1106-
ConvertedBindingKind::Constraint(_bounds) => {}
1049+
hir::TypeBindingKind::Constraint { bounds: _ } => {}
11071050
}
11081051
} else {
11091052
err.span_suggestion_verbose(

0 commit comments

Comments
 (0)
Please sign in to comment.