Skip to content

Commit a7f4fdb

Browse files
committed
Remove astconv::ConvertedBinding
1 parent 655fc8c commit a7f4fdb

File tree

3 files changed

+95
-150
lines changed

3 files changed

+95
-150
lines changed

compiler/rustc_hir_analysis/src/astconv/bounds.rs

+64-66
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

@@ -228,7 +226,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
228226
&self,
229227
hir_ref_id: hir::HirId,
230228
trait_ref: ty::PolyTraitRef<'tcx>,
231-
binding: &ConvertedBinding<'_, 'tcx>,
229+
binding: &hir::TypeBinding<'_>,
232230
bounds: &mut Bounds<'tcx>,
233231
speculative: bool,
234232
dup_bindings: &mut FxHashMap<DefId, Span>,
@@ -255,21 +253,20 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
255253

256254
let tcx = self.tcx();
257255

258-
let assoc_kind =
259-
if binding.gen_args.parenthesized == hir::GenericArgsParentheses::ReturnTypeNotation {
260-
ty::AssocKind::Fn
261-
} else if let ConvertedBindingKind::Equality(term) = binding.kind
262-
&& let ty::TermKind::Const(_) = term.node.unpack()
263-
{
264-
ty::AssocKind::Const
265-
} else {
266-
ty::AssocKind::Type
267-
};
256+
let assoc_kind = if binding.gen_args.parenthesized
257+
== hir::GenericArgsParentheses::ReturnTypeNotation
258+
{
259+
ty::AssocKind::Fn
260+
} else if let hir::TypeBindingKind::Equality { term: hir::Term::Const(_) } = binding.kind {
261+
ty::AssocKind::Const
262+
} else {
263+
ty::AssocKind::Type
264+
};
268265

269266
let candidate = if self.trait_defines_associated_item_named(
270267
trait_ref.def_id(),
271268
assoc_kind,
272-
binding.item_name,
269+
binding.ident,
273270
) {
274271
// Simple case: The assoc item is defined in the current trait.
275272
trait_ref
@@ -281,14 +278,14 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
281278
trait_ref.skip_binder().print_only_trait_name(),
282279
None,
283280
assoc_kind,
284-
binding.item_name,
281+
binding.ident,
285282
path_span,
286-
Some(&binding),
283+
Some(binding),
287284
)?
288285
};
289286

290287
let (assoc_ident, def_scope) =
291-
tcx.adjust_ident_and_get_scope(binding.item_name, candidate.def_id(), hir_ref_id);
288+
tcx.adjust_ident_and_get_scope(binding.ident, candidate.def_id(), hir_ref_id);
292289

293290
// We have already adjusted the item name above, so compare with `.normalize_to_macros_2_0()`
294291
// instead of calling `filter_by_name_and_kind` which would needlessly normalize the
@@ -304,7 +301,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
304301
.dcx()
305302
.struct_span_err(
306303
binding.span,
307-
format!("{} `{}` is private", assoc_item.kind, binding.item_name),
304+
format!("{} `{}` is private", assoc_item.kind, binding.ident),
308305
)
309306
.with_span_label(binding.span, format!("private {}", assoc_item.kind))
310307
.emit();
@@ -319,7 +316,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
319316
tcx.dcx().emit_err(errors::ValueOfAssociatedStructAlreadySpecified {
320317
span: binding.span,
321318
prev_span: *prev_span,
322-
item_name: binding.item_name,
319+
item_name: binding.ident,
323320
def_path: tcx.def_path_str(assoc_item.container_id(tcx)),
324321
});
325322
})
@@ -419,7 +416,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
419416
// parent arguments (the arguments of the trait) and the own arguments (the ones of
420417
// the associated item itself) and construct an alias type using them.
421418
candidate.map_bound(|trait_ref| {
422-
let ident = Ident::new(assoc_item.name, binding.item_name.span);
419+
let ident = Ident::new(assoc_item.name, binding.ident.span);
423420
let item_segment = hir::PathSegment {
424421
ident,
425422
hir_id: binding.hir_id,
@@ -443,65 +440,66 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
443440
})
444441
};
445442

446-
// FIXME(associated_const_equality): Can this actually ever fail with assoc consts?
447-
if !speculative {
448-
// Find any late-bound regions declared in `ty` that are not
449-
// declared in the trait-ref or assoc_item. These are not well-formed.
450-
//
451-
// Example:
452-
//
453-
// for<'a> <T as Iterator>::Item = &'a str // <-- 'a is bad
454-
// for<'a> <T as FnMut<(&'a u32,)>>::Output = &'a str // <-- 'a is ok
455-
if let ConvertedBindingKind::Equality(term) = binding.kind {
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.node));
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-
self.validate_late_bound_regions(
467-
late_bound_in_projection_ty,
468-
late_bound_in_term,
469-
|br_name| {
470-
struct_span_code_err!(
471-
tcx.dcx(),
472-
binding.span,
473-
E0582,
474-
"binding for associated type `{}` references {}, \
475-
which does not appear in the trait input types",
476-
binding.item_name,
477-
br_name
478-
)
479-
},
480-
);
481-
}
482-
}
483-
484443
match binding.kind {
485-
ConvertedBindingKind::Equality(..) if let ty::AssocKind::Fn = assoc_kind => {
444+
hir::TypeBindingKind::Equality { .. } if let ty::AssocKind::Fn = assoc_kind => {
486445
return Err(tcx.dcx().emit_err(crate::errors::ReturnTypeNotationEqualityBound {
487446
span: binding.span,
488447
}));
489448
}
490-
ConvertedBindingKind::Equality(term) => {
449+
hir::TypeBindingKind::Equality { term } => {
450+
let term = match term {
451+
hir::Term::Ty(ty) => self.ast_ty_to_ty(ty).into(),
452+
hir::Term::Const(ct) => ty::Const::from_anon_const(tcx, ct.def_id).into(),
453+
};
454+
455+
// FIXME(associated_const_equality): Can this actually ever fail with assoc consts?
456+
if !speculative {
457+
// Find any late-bound regions declared in `ty` that are not
458+
// declared in the trait-ref or assoc_item. These are not well-formed.
459+
//
460+
// Example:
461+
//
462+
// for<'a> <T as Iterator>::Item = &'a str // <-- 'a is bad
463+
// for<'a> <T as FnMut<(&'a u32,)>>::Output = &'a str // <-- 'a is ok
464+
let late_bound_in_projection_ty =
465+
tcx.collect_constrained_late_bound_regions(&projection_ty);
466+
let late_bound_in_term =
467+
tcx.collect_referenced_late_bound_regions(&trait_ref.rebind(term));
468+
debug!(?late_bound_in_projection_ty);
469+
debug!(?late_bound_in_term);
470+
471+
// FIXME: point at the type params that don't have appropriate lifetimes:
472+
// struct S1<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
473+
// ---- ---- ^^^^^^^
474+
self.validate_late_bound_regions(
475+
late_bound_in_projection_ty,
476+
late_bound_in_term,
477+
|br_name| {
478+
struct_span_code_err!(
479+
tcx.dcx(),
480+
binding.span,
481+
E0582,
482+
"binding for associated type `{}` references {}, \
483+
which does not appear in the trait input types",
484+
binding.ident,
485+
br_name
486+
)
487+
},
488+
);
489+
}
490+
491491
// "Desugar" a constraint like `T: Iterator<Item = u32>` this to
492492
// the "projection predicate" for:
493493
//
494494
// `<T as Iterator>::Item = u32`
495495
bounds.push_projection_bound(
496496
tcx,
497-
projection_ty.map_bound(|projection_ty| ty::ProjectionPredicate {
498-
projection_ty,
499-
term: term.node,
500-
}),
497+
projection_ty
498+
.map_bound(|projection_ty| ty::ProjectionPredicate { projection_ty, term }),
501499
binding.span,
502500
);
503501
}
504-
ConvertedBindingKind::Constraint(ast_bounds) => {
502+
hir::TypeBindingKind::Constraint { bounds: ast_bounds } => {
505503
// "Desugar" a constraint like `T: Iterator<Item: Debug>` to
506504
//
507505
// `<T as Iterator>::Item: Debug`

compiler/rustc_hir_analysis/src/astconv/errors.rs

+14-10
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,
@@ -107,7 +107,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
107107
assoc_kind: ty::AssocKind,
108108
assoc_name: Ident,
109109
span: Span,
110-
binding: Option<&super::ConvertedBinding<'_, 'tcx>>,
110+
binding: Option<&hir::TypeBinding<'_>>,
111111
) -> ErrorGuaranteed
112112
where
113113
I: Iterator<Item = ty::PolyTraitRef<'tcx>>,
@@ -286,13 +286,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
286286
assoc_kind: ty::AssocKind,
287287
ident: Ident,
288288
span: Span,
289-
binding: Option<&super::ConvertedBinding<'_, 'tcx>>,
289+
binding: Option<&hir::TypeBinding<'_>>,
290290
) -> ErrorGuaranteed {
291291
let tcx = self.tcx();
292292

293293
let bound_on_assoc_const_label = if let ty::AssocKind::Const = assoc_item.kind
294294
&& let Some(binding) = binding
295-
&& let ConvertedBindingKind::Constraint(_) = binding.kind
295+
&& let hir::TypeBindingKind::Constraint { .. } = binding.kind
296296
{
297297
let lo = if binding.gen_args.span_ext.is_dummy() {
298298
ident.span
@@ -306,14 +306,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
306306

307307
// FIXME(associated_const_equality): This has quite a few false positives and negatives.
308308
let wrap_in_braces_sugg = if let Some(binding) = binding
309-
&& let ConvertedBindingKind::Equality(term) = binding.kind
310-
&& let ty::TermKind::Ty(ty) = term.node.unpack()
309+
&& let hir::TypeBindingKind::Equality { term: hir::Term::Ty(hir_ty) } = binding.kind
310+
&& let ty = self.ast_ty_to_ty(hir_ty)
311311
&& (ty.is_enum() || ty.references_error())
312312
&& tcx.features().associated_const_equality
313313
{
314314
Some(errors::AssocKindMismatchWrapInBracesSugg {
315-
lo: term.span.shrink_to_lo(),
316-
hi: term.span.shrink_to_hi(),
315+
lo: hir_ty.span.shrink_to_lo(),
316+
hi: hir_ty.span.shrink_to_hi(),
317317
})
318318
} else {
319319
None
@@ -322,9 +322,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
322322
// For equality bounds, we want to blame the term (RHS) instead of the item (LHS) since
323323
// one can argue that that's more “intuitive” to the user.
324324
let (span, expected_because_label, expected, got) = if let Some(binding) = binding
325-
&& let ConvertedBindingKind::Equality(term) = binding.kind
325+
&& let hir::TypeBindingKind::Equality { term } = binding.kind
326326
{
327-
(term.span, Some(ident.span), assoc_item.kind, assoc_kind)
327+
let span = match term {
328+
hir::Term::Ty(ty) => ty.span,
329+
hir::Term::Const(ct) => tcx.def_span(ct.def_id),
330+
};
331+
(span, Some(ident.span), assoc_item.kind, assoc_kind)
328332
} else {
329333
(ident.span, None, assoc_kind, assoc_item.kind)
330334
};

0 commit comments

Comments
 (0)