Skip to content

Commit 9222ccd

Browse files
committed
Remove astconv::ConvertedBinding
1 parent 8581437 commit 9222ccd

File tree

3 files changed

+97
-152
lines changed

3 files changed

+97
-152
lines changed

compiler/rustc_hir_analysis/src/astconv/bounds.rs

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

@@ -220,7 +218,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
220218
&self,
221219
hir_ref_id: hir::HirId,
222220
trait_ref: ty::PolyTraitRef<'tcx>,
223-
binding: &ConvertedBinding<'_, 'tcx>,
221+
binding: &hir::TypeBinding<'_>,
224222
bounds: &mut Bounds<'tcx>,
225223
speculative: bool,
226224
dup_bindings: &mut FxHashMap<DefId, Span>,
@@ -247,21 +245,20 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
247245

248246
let tcx = self.tcx();
249247

250-
let assoc_kind =
251-
if binding.gen_args.parenthesized == hir::GenericArgsParentheses::ReturnTypeNotation {
252-
ty::AssocKind::Fn
253-
} else if let ConvertedBindingKind::Equality(term) = binding.kind
254-
&& let ty::TermKind::Const(_) = term.node.unpack()
255-
{
256-
ty::AssocKind::Const
257-
} else {
258-
ty::AssocKind::Type
259-
};
248+
let assoc_kind = if binding.gen_args.parenthesized
249+
== hir::GenericArgsParentheses::ReturnTypeNotation
250+
{
251+
ty::AssocKind::Fn
252+
} else if let hir::TypeBindingKind::Equality { term: hir::Term::Const(_) } = binding.kind {
253+
ty::AssocKind::Const
254+
} else {
255+
ty::AssocKind::Type
256+
};
260257

261258
let candidate = if self.trait_defines_associated_item_named(
262259
trait_ref.def_id(),
263260
assoc_kind,
264-
binding.item_name,
261+
binding.ident,
265262
) {
266263
// Simple case: The assoc item is defined in the current trait.
267264
trait_ref
@@ -273,14 +270,14 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
273270
trait_ref.skip_binder().print_only_trait_name(),
274271
None,
275272
assoc_kind,
276-
binding.item_name,
273+
binding.ident,
277274
path_span,
278-
Some(&binding),
275+
Some(binding),
279276
)?
280277
};
281278

282279
let (assoc_ident, def_scope) =
283-
tcx.adjust_ident_and_get_scope(binding.item_name, candidate.def_id(), hir_ref_id);
280+
tcx.adjust_ident_and_get_scope(binding.ident, candidate.def_id(), hir_ref_id);
284281

285282
// We have already adjusted the item name above, so compare with `.normalize_to_macros_2_0()`
286283
// instead of calling `filter_by_name_and_kind` which would needlessly normalize the
@@ -295,7 +292,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
295292
tcx.dcx()
296293
.struct_span_err(
297294
binding.span,
298-
format!("{} `{}` is private", assoc_item.kind, binding.item_name),
295+
format!("{} `{}` is private", assoc_item.kind, binding.ident),
299296
)
300297
.span_label(binding.span, format!("private {}", assoc_item.kind))
301298
.emit();
@@ -309,7 +306,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
309306
tcx.dcx().emit_err(errors::ValueOfAssociatedStructAlreadySpecified {
310307
span: binding.span,
311308
prev_span: *prev_span,
312-
item_name: binding.item_name,
309+
item_name: binding.ident,
313310
def_path: tcx.def_path_str(assoc_item.container_id(tcx)),
314311
});
315312
})
@@ -409,7 +406,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
409406
} else {
410407
// Append the generic arguments of the associated type or const to the `trait_ref`.
411408
candidate.map_bound(|trait_ref| {
412-
let ident = Ident::new(assoc_item.name, binding.item_name.span);
409+
let ident = Ident::new(assoc_item.name, binding.ident.span);
413410
let item_segment = hir::PathSegment {
414411
ident,
415412
hir_id: binding.hir_id,
@@ -433,67 +430,68 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
433430
})
434431
};
435432

436-
// FIXME(fmease): This doesn't check actually seem to work for assoc consts.
437-
// We want to deny escaping late-bound vars in general anyways for assoc consts.
438-
// If the diagnostic *is* reachable, update it (“type” → “term” or similar).
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-
// FIXME: point at the type params that don't have appropriate lifetimes:
456-
// struct S1<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
457-
// ---- ---- ^^^^^^^
458-
self.validate_late_bound_regions(
459-
late_bound_in_projection_ty,
460-
late_bound_in_term,
461-
|br_name| {
462-
struct_span_err!(
463-
tcx.dcx(),
464-
binding.span,
465-
E0582,
466-
"binding for associated type `{}` references {}, \
467-
which does not appear in the trait input types",
468-
binding.item_name,
469-
br_name
470-
)
471-
},
472-
);
473-
}
474-
}
475-
476433
match binding.kind {
477-
ConvertedBindingKind::Equality(..) if let ty::AssocKind::Fn = assoc_kind => {
434+
hir::TypeBindingKind::Equality { .. } if let ty::AssocKind::Fn = assoc_kind => {
478435
return Err(self.tcx().dcx().emit_err(
479436
crate::errors::ReturnTypeNotationEqualityBound { span: binding.span },
480437
));
481438
}
482-
ConvertedBindingKind::Equality(term) => {
439+
hir::TypeBindingKind::Equality { term } => {
440+
let term = match term {
441+
hir::Term::Ty(ty) => self.ast_ty_to_ty(ty).into(),
442+
hir::Term::Const(ct) => ty::Const::from_anon_const(tcx, ct.def_id).into(),
443+
};
444+
445+
// FIXME(fmease): This doesn't check actually seem to work for assoc consts.
446+
// We want to deny escaping late-bound vars in general anyways for assoc consts.
447+
// If the diagnostic *is* reachable, update it (“type” → “term” or similar).
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+
self.validate_late_bound_regions(
467+
late_bound_in_projection_ty,
468+
late_bound_in_term,
469+
|br_name| {
470+
struct_span_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.ident,
477+
br_name
478+
)
479+
},
480+
);
481+
}
482+
483483
// "Desugar" a constraint like `T: Iterator<Item = u32>` this to
484484
// the "projection predicate" for:
485485
//
486486
// `<T as Iterator>::Item = u32`
487487
bounds.push_projection_bound(
488488
tcx,
489-
projection_ty.map_bound(|projection_ty| ty::ProjectionPredicate {
490-
projection_ty,
491-
term: term.node,
492-
}),
489+
projection_ty
490+
.map_bound(|projection_ty| ty::ProjectionPredicate { projection_ty, term }),
493491
binding.span,
494492
);
495493
}
496-
ConvertedBindingKind::Constraint(ast_bounds) => {
494+
hir::TypeBindingKind::Constraint { bounds: ast_bounds } => {
497495
// "Desugar" a constraint like `T: Iterator<Item: Debug>` to
498496
//
499497
// `<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,
@@ -106,7 +106,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
106106
assoc_kind: ty::AssocKind,
107107
assoc_name: Ident,
108108
span: Span,
109-
binding: Option<&super::ConvertedBinding<'_, 'tcx>>,
109+
binding: Option<&hir::TypeBinding<'_>>,
110110
) -> ErrorGuaranteed
111111
where
112112
I: Iterator<Item = ty::PolyTraitRef<'tcx>>,
@@ -285,13 +285,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
285285
assoc_kind: ty::AssocKind,
286286
ident: Ident,
287287
span: Span,
288-
binding: Option<&super::ConvertedBinding<'_, 'tcx>>,
288+
binding: Option<&hir::TypeBinding<'_>>,
289289
) -> ErrorGuaranteed {
290290
let tcx = self.tcx();
291291

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

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

0 commit comments

Comments
 (0)