Skip to content

Commit e21e039

Browse files
authored
Rollup merge of rust-lang#114746 - compiler-errors:atb-no-const, r=TaKO8Ki
Don't add associated type bound for non-types We had this fix for equality constraints (rust-lang#99890), but for some reason not trait constraints 😅 Fixes rust-lang#114744
2 parents 4b2d87d + e4cf708 commit e21e039

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

compiler/rustc_hir_analysis/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
hir_analysis_ambiguous_lifetime_bound =
22
ambiguous lifetime bound, explicit lifetime bound required
33
4+
hir_analysis_assoc_bound_on_const = expected associated type, found {$descr}
5+
.note = trait bounds not allowed on {$descr}
6+
47
hir_analysis_assoc_type_binding_not_allowed =
58
associated type bindings are not allowed here
69
.label = associated type not allowed here

compiler/rustc_hir_analysis/src/astconv/bounds.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::astconv::{
1313
AstConv, ConvertedBinding, ConvertedBindingKind, OnlySelfBounds, PredicateFilter,
1414
};
1515
use crate::bounds::Bounds;
16-
use crate::errors::{MultipleRelaxedDefaultBounds, ValueOfAssociatedStructAlreadySpecified};
16+
use crate::errors;
1717

1818
impl<'tcx> dyn AstConv<'tcx> + '_ {
1919
/// Sets `implicitly_sized` to true on `Bounds` if necessary
@@ -35,7 +35,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
3535
if unbound.is_none() {
3636
unbound = Some(&ptr.trait_ref);
3737
} else {
38-
tcx.sess.emit_err(MultipleRelaxedDefaultBounds { span });
38+
tcx.sess.emit_err(errors::MultipleRelaxedDefaultBounds { span });
3939
}
4040
}
4141
}
@@ -326,7 +326,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
326326
dup_bindings
327327
.entry(assoc_item.def_id)
328328
.and_modify(|prev_span| {
329-
tcx.sess.emit_err(ValueOfAssociatedStructAlreadySpecified {
329+
tcx.sess.emit_err(errors::ValueOfAssociatedStructAlreadySpecified {
330330
span: binding.span,
331331
prev_span: *prev_span,
332332
item_name: binding.item_name,
@@ -488,6 +488,8 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
488488
}
489489
}
490490

491+
let assoc_item_def_id = projection_ty.skip_binder().def_id;
492+
let def_kind = tcx.def_kind(assoc_item_def_id);
491493
match binding.kind {
492494
ConvertedBindingKind::Equality(..) if return_type_notation => {
493495
return Err(self.tcx().sess.emit_err(
@@ -499,11 +501,9 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
499501
// the "projection predicate" for:
500502
//
501503
// `<T as Iterator>::Item = u32`
502-
let assoc_item_def_id = projection_ty.skip_binder().def_id;
503-
let def_kind = tcx.def_kind(assoc_item_def_id);
504504
match (def_kind, term.unpack()) {
505-
(hir::def::DefKind::AssocTy, ty::TermKind::Ty(_))
506-
| (hir::def::DefKind::AssocConst, ty::TermKind::Const(_)) => (),
505+
(DefKind::AssocTy, ty::TermKind::Ty(_))
506+
| (DefKind::AssocConst, ty::TermKind::Const(_)) => (),
507507
(_, _) => {
508508
let got = if let Some(_) = term.ty() { "type" } else { "constant" };
509509
let expected = tcx.def_descr(assoc_item_def_id);
@@ -516,7 +516,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
516516
format!("{expected} defined here"),
517517
);
518518

519-
if let hir::def::DefKind::AssocConst = def_kind
519+
if let DefKind::AssocConst = def_kind
520520
&& let Some(t) = term.ty() && (t.is_enum() || t.references_error())
521521
&& tcx.features().associated_const_equality {
522522
err.span_suggestion(
@@ -528,8 +528,8 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
528528
}
529529
let reported = err.emit();
530530
term = match def_kind {
531-
hir::def::DefKind::AssocTy => Ty::new_error(tcx, reported).into(),
532-
hir::def::DefKind::AssocConst => ty::Const::new_error(
531+
DefKind::AssocTy => Ty::new_error(tcx, reported).into(),
532+
DefKind::AssocConst => ty::Const::new_error(
533533
tcx,
534534
reported,
535535
tcx.type_of(assoc_item_def_id)
@@ -548,6 +548,15 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
548548
);
549549
}
550550
ConvertedBindingKind::Constraint(ast_bounds) => {
551+
match def_kind {
552+
DefKind::AssocTy => {}
553+
_ => {
554+
return Err(tcx.sess.emit_err(errors::AssocBoundOnConst {
555+
span: assoc_ident.span,
556+
descr: tcx.def_descr(assoc_item_def_id),
557+
}));
558+
}
559+
}
551560
// "Desugar" a constraint like `T: Iterator<Item: Debug>` to
552561
//
553562
// `<T as Iterator>::Item: Debug`

compiler/rustc_hir_analysis/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -918,3 +918,12 @@ pub struct UnusedAssociatedTypeBounds {
918918
#[suggestion(code = "")]
919919
pub span: Span,
920920
}
921+
922+
#[derive(Diagnostic)]
923+
#[diag(hir_analysis_assoc_bound_on_const)]
924+
#[note]
925+
pub struct AssocBoundOnConst {
926+
#[primary_span]
927+
pub span: Span,
928+
pub descr: &'static str,
929+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(associated_type_bounds)]
2+
3+
pub fn accept(_: impl Trait<K: Copy>) {}
4+
//~^ ERROR expected associated type, found associated constant
5+
6+
pub trait Trait {
7+
const K: i32;
8+
}
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: expected associated type, found associated constant
2+
--> $DIR/consts.rs:3:29
3+
|
4+
LL | pub fn accept(_: impl Trait<K: Copy>) {}
5+
| ^
6+
|
7+
= note: trait bounds not allowed on associated constant
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)