Skip to content

Commit 697b910

Browse files
committed
[rm convertedbinding]
1 parent d6daff5 commit 697b910

File tree

3 files changed

+89
-146
lines changed

3 files changed

+89
-146
lines changed

compiler/rustc_hir_analysis/src/astconv/bounds.rs

Lines changed: 63 additions & 65 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

@@ -217,7 +215,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
217215
&self,
218216
hir_ref_id: hir::HirId,
219217
trait_ref: ty::PolyTraitRef<'tcx>,
220-
binding: &ConvertedBinding<'_, 'tcx>,
218+
binding: &hir::TypeBinding<'_>,
221219
bounds: &mut Bounds<'tcx>,
222220
speculative: bool,
223221
dup_bindings: &mut FxHashMap<DefId, Span>,
@@ -244,21 +242,20 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
244242

245243
let tcx = self.tcx();
246244

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

258255
let candidate = if self.trait_defines_associated_item_named(
259256
trait_ref.def_id(),
260257
assoc_kind,
261-
binding.item_name,
258+
binding.ident,
262259
) {
263260
// Simple case: The assoc item is defined in the current trait.
264261
trait_ref
@@ -270,14 +267,14 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
270267
trait_ref.skip_binder().print_only_trait_name(),
271268
None,
272269
assoc_kind,
273-
binding.item_name,
270+
binding.ident,
274271
path_span,
275-
Some(&binding),
272+
Some(binding),
276273
)?
277274
};
278275

279276
let (assoc_ident, def_scope) =
280-
tcx.adjust_ident_and_get_scope(binding.item_name, candidate.def_id(), hir_ref_id);
277+
tcx.adjust_ident_and_get_scope(binding.ident, candidate.def_id(), hir_ref_id);
281278

282279
// We have already adjusted the item name above, so compare with `.normalize_to_macros_2_0()`
283280
// instead of calling `filter_by_name_and_kind` which would needlessly normalize the
@@ -292,7 +289,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
292289
tcx.dcx()
293290
.struct_span_err(
294291
binding.span,
295-
format!("{} `{}` is private", assoc_item.kind, binding.item_name),
292+
format!("{} `{}` is private", assoc_item.kind, binding.ident),
296293
)
297294
.span_label(binding.span, format!("private {}", assoc_item.kind))
298295
.emit();
@@ -306,7 +303,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
306303
tcx.dcx().emit_err(errors::ValueOfAssociatedStructAlreadySpecified {
307304
span: binding.span,
308305
prev_span: *prev_span,
309-
item_name: binding.item_name,
306+
item_name: binding.ident,
310307
def_path: tcx.def_path_str(assoc_item.container_id(tcx)),
311308
});
312309
})
@@ -406,7 +403,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
406403
} else {
407404
// Append the generic arguments of the associated type to the `trait_ref`.
408405
candidate.map_bound(|trait_ref| {
409-
let ident = Ident::new(assoc_item.name, binding.item_name.span);
406+
let ident = Ident::new(assoc_item.name, binding.ident.span);
410407
let item_segment = hir::PathSegment {
411408
ident,
412409
hir_id: binding.hir_id,
@@ -428,64 +425,65 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
428425
})
429426
};
430427

431-
if !speculative {
432-
// Find any late-bound regions declared in `ty` that are not
433-
// declared in the trait-ref or assoc_item. These are not well-formed.
434-
//
435-
// Example:
436-
//
437-
// for<'a> <T as Iterator>::Item = &'a str // <-- 'a is bad
438-
// for<'a> <T as FnMut<(&'a u32,)>>::Output = &'a str // <-- 'a is ok
439-
if let ConvertedBindingKind::Equality(ty) = binding.kind {
440-
let late_bound_in_trait_ref =
441-
tcx.collect_constrained_late_bound_regions(&projection_ty);
442-
let late_bound_in_ty =
443-
tcx.collect_referenced_late_bound_regions(&trait_ref.rebind(ty.node));
444-
debug!(?late_bound_in_trait_ref);
445-
debug!(?late_bound_in_ty);
446-
447-
// FIXME: point at the type params that don't have appropriate lifetimes:
448-
// struct S1<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
449-
// ---- ---- ^^^^^^^
450-
self.validate_late_bound_regions(
451-
late_bound_in_trait_ref,
452-
late_bound_in_ty,
453-
|br_name| {
454-
struct_span_err!(
455-
tcx.dcx(),
456-
binding.span,
457-
E0582,
458-
"binding for associated type `{}` references {}, \
459-
which does not appear in the trait input types",
460-
binding.item_name,
461-
br_name
462-
)
463-
},
464-
);
465-
}
466-
}
467-
468428
match binding.kind {
469-
ConvertedBindingKind::Equality(..) if let ty::AssocKind::Fn = assoc_kind => {
429+
hir::TypeBindingKind::Equality { .. } if let ty::AssocKind::Fn = assoc_kind => {
470430
return Err(self.tcx().dcx().emit_err(
471431
crate::errors::ReturnTypeNotationEqualityBound { span: binding.span },
472432
));
473433
}
474-
ConvertedBindingKind::Equality(term) => {
434+
hir::TypeBindingKind::Equality { term } => {
435+
let term = match term {
436+
hir::Term::Ty(ty) => self.ast_ty_to_ty(ty).into(),
437+
hir::Term::Const(ct) => ty::Const::from_anon_const(tcx, ct.def_id).into(),
438+
};
439+
440+
if !speculative {
441+
// Find any late-bound regions declared in `ty` that are not
442+
// declared in the trait-ref or assoc_item. These are not well-formed.
443+
//
444+
// Example:
445+
//
446+
// for<'a> <T as Iterator>::Item = &'a str // <-- 'a is bad
447+
// for<'a> <T as FnMut<(&'a u32,)>>::Output = &'a str // <-- 'a is ok
448+
let late_bound_in_trait_ref =
449+
tcx.collect_constrained_late_bound_regions(&projection_ty);
450+
let late_bound_in_ty =
451+
tcx.collect_referenced_late_bound_regions(&trait_ref.rebind(term));
452+
debug!(?late_bound_in_trait_ref);
453+
debug!(?late_bound_in_ty);
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_trait_ref,
460+
late_bound_in_ty,
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.ident,
469+
br_name
470+
)
471+
},
472+
);
473+
}
474+
475475
// "Desugar" a constraint like `T: Iterator<Item = u32>` this to
476476
// the "projection predicate" for:
477477
//
478478
// `<T as Iterator>::Item = u32`
479479
bounds.push_projection_bound(
480480
tcx,
481-
projection_ty.map_bound(|projection_ty| ty::ProjectionPredicate {
482-
projection_ty,
483-
term: term.node,
484-
}),
481+
projection_ty
482+
.map_bound(|projection_ty| ty::ProjectionPredicate { projection_ty, term }),
485483
binding.span,
486484
);
487485
}
488-
ConvertedBindingKind::Constraint(ast_bounds) => {
486+
hir::TypeBindingKind::Constraint { bounds: ast_bounds } => {
489487
// "Desugar" a constraint like `T: Iterator<Item: Debug>` to
490488
//
491489
// `<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,
@@ -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)