Skip to content

Commit 8f72bb2

Browse files
BoxyUwUcamelid
authored andcommitted
fix associated_const_equality tests
1 parent bcfb692 commit 8f72bb2

39 files changed

+194
-164
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
55
use rustc_errors::codes::*;
66
use rustc_errors::struct_span_code_err;
77
use rustc_hir as hir;
8-
use rustc_hir::PolyTraitRef;
8+
use rustc_hir::attrs::AttributeKind;
99
use rustc_hir::def::{DefKind, Res};
1010
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
11+
use rustc_hir::{PolyTraitRef, find_attr};
1112
use rustc_middle::bug;
1213
use rustc_middle::ty::{
1314
self as ty, IsSuggestable, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
@@ -573,7 +574,32 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
573574
term,
574575
})
575576
});
576-
bounds.push((bound.upcast(tcx), constraint.span));
577+
578+
if let ty::AssocTag::Const = assoc_tag
579+
&& !find_attr!(
580+
self.tcx().get_all_attrs(assoc_item.def_id),
581+
AttributeKind::TypeConst(_)
582+
)
583+
{
584+
if tcx.features().min_generic_const_args()
585+
|| tcx.features().associated_const_equality()
586+
{
587+
let mut err = self.dcx().struct_span_err(
588+
constraint.span,
589+
"use of trait associated const without `#[type_const]`",
590+
);
591+
err.note("the declaration in the trait must be marked with `#[type_const]`");
592+
return Err(err.emit());
593+
} else {
594+
let err = self.dcx().span_delayed_bug(
595+
constraint.span,
596+
"use of trait associated const without `#[type_const]`",
597+
);
598+
return Err(err);
599+
}
600+
} else {
601+
bounds.push((bound.upcast(tcx), constraint.span));
602+
}
577603
}
578604
// SelfTraitThatDefines is only interested in trait predicates.
579605
PredicateFilter::SelfTraitThatDefines(_) => {}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ use rustc_errors::codes::*;
2727
use rustc_errors::{
2828
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, FatalError, struct_span_code_err,
2929
};
30+
use rustc_hir::attrs::AttributeKind;
3031
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
3132
use rustc_hir::def_id::{DefId, LocalDefId};
32-
use rustc_hir::{self as hir, AnonConst, GenericArg, GenericArgs, HirId};
33+
use rustc_hir::{self as hir, AnonConst, GenericArg, GenericArgs, HirId, find_attr};
3334
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
3435
use rustc_infer::traits::DynCompatibilityViolation;
3536
use rustc_macros::{TypeFoldable, TypeVisitable};
@@ -1231,7 +1232,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
12311232
LowerTypeRelativePathMode::Const,
12321233
)? {
12331234
TypeRelativePath::AssocItem(def_id, args) => {
1234-
if !tcx.associated_item(def_id).is_type_const_capable(tcx) {
1235+
if !find_attr!(self.tcx().get_all_attrs(def_id), AttributeKind::TypeConst(_)) {
12351236
let mut err = self.dcx().struct_span_err(
12361237
span,
12371238
"use of trait associated const without `#[type_const]`",
@@ -1672,6 +1673,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
16721673
ty::AssocTag::Const,
16731674
) {
16741675
Ok((item_def_id, item_args)) => {
1676+
if !find_attr!(self.tcx().get_all_attrs(item_def_id), AttributeKind::TypeConst(_)) {
1677+
let mut err = self.dcx().struct_span_err(
1678+
span,
1679+
"use of `const` in the type system without `#[type_const]`",
1680+
);
1681+
err.note("the declaration must be marked with `#[type_const]`");
1682+
return Const::new_error(self.tcx(), err.emit());
1683+
}
1684+
16751685
let uv = ty::UnevaluatedConst::new(item_def_id, item_args);
16761686
Const::new_unevaluated(self.tcx(), uv)
16771687
}

compiler/rustc_middle/src/ty/assoc.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use rustc_data_structures::sorted_map::SortedIndexMultiMap;
22
use rustc_hir as hir;
3-
use rustc_hir::attrs::AttributeKind;
43
use rustc_hir::def::{DefKind, Namespace};
54
use rustc_hir::def_id::DefId;
6-
use rustc_hir::find_attr;
75
use rustc_macros::{Decodable, Encodable, HashStable};
86
use rustc_span::{ErrorGuaranteed, Ident, Symbol};
97

@@ -173,24 +171,6 @@ impl AssocItem {
173171
pub fn is_impl_trait_in_trait(&self) -> bool {
174172
matches!(self.kind, AssocKind::Type { data: AssocTypeData::Rpitit(_) })
175173
}
176-
177-
/// Returns true if:
178-
/// - This trait associated item has the `#[type_const]` attribute,
179-
/// - If it is in a trait impl, the item from the original trait has this attribute, or
180-
/// - It is an inherent assoc const.
181-
pub fn is_type_const_capable(&self, tcx: TyCtxt<'_>) -> bool {
182-
if !matches!(self.kind, ty::AssocKind::Const { .. }) {
183-
return false;
184-
}
185-
186-
let def_id = match self.container {
187-
AssocContainer::Trait => self.def_id,
188-
AssocContainer::TraitImpl(Ok(trait_item_did)) => trait_item_did,
189-
AssocContainer::TraitImpl(Err(_)) => return false,
190-
AssocContainer::InherentImpl => return true,
191-
};
192-
find_attr!(tcx.get_all_attrs(def_id), AttributeKind::TypeConst(_))
193-
}
194174
}
195175

196176
#[derive(Copy, Clone, PartialEq, Debug, HashStable, Eq, Hash, Encodable, Decodable)]

compiler/rustc_passes/src/check_attr.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,19 +2047,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
20472047
}
20482048
}
20492049

2050-
fn check_type_const(&self, hir_id: HirId, attr_span: Span, target: Target) {
2051-
let tcx = self.tcx;
2052-
if target == Target::AssocConst
2053-
&& let parent = tcx.parent(hir_id.expect_owner().to_def_id())
2054-
&& self.tcx.def_kind(parent) == DefKind::Trait
2055-
{
2050+
fn check_type_const(&self, _hir_id: HirId, attr_span: Span, target: Target) {
2051+
if matches!(target, Target::AssocConst | Target::Const) {
20562052
return;
20572053
} else {
20582054
self.dcx()
2059-
.struct_span_err(
2060-
attr_span,
2061-
"`#[type_const]` must only be applied to trait associated constants",
2062-
)
2055+
.struct_span_err(attr_span, "`#[type_const]` must only be applied to const items")
20632056
.emit();
20642057
}
20652058
}

tests/ui/associated-consts/assoc-const-eq-ambiguity.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
// We used to say "ambiguous associated type" on ambiguous associated consts.
22
// Ensure that we now use the correct label.
33

4-
#![feature(associated_const_equality)]
4+
#![feature(associated_const_equality, min_generic_const_args)]
5+
#![allow(incomplete_features)]
56

67
trait Trait0: Parent0<i32> + Parent0<u32> {}
7-
trait Parent0<T> { const K: (); }
8+
trait Parent0<T> {
9+
#[type_const]
10+
const K: ();
11+
}
812

913
fn take0(_: impl Trait0<K = { () }>) {}
1014
//~^ ERROR ambiguous associated constant `K` in bounds of `Trait0`
1115

1216
trait Trait1: Parent1 + Parent2 {}
13-
trait Parent1 { const C: i32; }
14-
trait Parent2 { const C: &'static str; }
17+
trait Parent1 {
18+
#[type_const]
19+
const C: i32;
20+
}
21+
trait Parent2 {
22+
#[type_const]
23+
const C: &'static str;
24+
}
1525

1626
fn take1(_: impl Trait1<C = "?">) {}
1727
//~^ ERROR ambiguous associated constant `C` in bounds of `Trait1`

tests/ui/associated-consts/assoc-const-eq-ambiguity.stderr

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
error[E0222]: ambiguous associated constant `K` in bounds of `Trait0`
2-
--> $DIR/assoc-const-eq-ambiguity.rs:9:25
2+
--> $DIR/assoc-const-eq-ambiguity.rs:13:25
33
|
4-
LL | trait Parent0<T> { const K: (); }
5-
| -----------
6-
| |
7-
| ambiguous `K` from `Parent0<u32>`
8-
| ambiguous `K` from `Parent0<i32>`
9-
LL |
4+
LL | const K: ();
5+
| -----------
6+
| |
7+
| ambiguous `K` from `Parent0<u32>`
8+
| ambiguous `K` from `Parent0<i32>`
9+
...
1010
LL | fn take0(_: impl Trait0<K = { () }>) {}
1111
| ^^^^^^^^^^ ambiguous associated constant `K`
1212
|
@@ -17,13 +17,14 @@ LL | fn take0(_: impl Trait0<K = { () }>) {}
1717
T: Parent0<i32>::K = { () }
1818

1919
error[E0222]: ambiguous associated constant `C` in bounds of `Trait1`
20-
--> $DIR/assoc-const-eq-ambiguity.rs:16:25
20+
--> $DIR/assoc-const-eq-ambiguity.rs:26:25
2121
|
22-
LL | trait Parent1 { const C: i32; }
23-
| ------------ ambiguous `C` from `Parent1`
24-
LL | trait Parent2 { const C: &'static str; }
25-
| --------------------- ambiguous `C` from `Parent2`
26-
LL |
22+
LL | const C: i32;
23+
| ------------ ambiguous `C` from `Parent1`
24+
...
25+
LL | const C: &'static str;
26+
| --------------------- ambiguous `C` from `Parent2`
27+
...
2728
LL | fn take1(_: impl Trait1<C = "?">) {}
2829
| ^^^^^^^ ambiguous associated constant `C`
2930

tests/ui/associated-consts/assoc-const-eq-bound-var-in-ty-not-wf.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Check that we eventually catch types of assoc const bounds
22
// (containing late-bound vars) that are ill-formed.
3-
#![feature(associated_const_equality)]
3+
#![feature(associated_const_equality, min_generic_const_args)]
4+
#![allow(incomplete_features)]
45

56
trait Trait<T> {
7+
#[type_const]
68
const K: T;
79
}
810

tests/ui/associated-consts/assoc-const-eq-bound-var-in-ty-not-wf.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
error: higher-ranked subtype error
2-
--> $DIR/assoc-const-eq-bound-var-in-ty-not-wf.rs:12:13
2+
--> $DIR/assoc-const-eq-bound-var-in-ty-not-wf.rs:14:13
33
|
44
LL | K = { () }
55
| ^^^^^^
66

77
error: higher-ranked subtype error
8-
--> $DIR/assoc-const-eq-bound-var-in-ty-not-wf.rs:12:13
8+
--> $DIR/assoc-const-eq-bound-var-in-ty-not-wf.rs:14:13
99
|
1010
LL | K = { () }
1111
| ^^^^^^
1212
|
1313
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
1414

1515
error: implementation of `Project` is not general enough
16-
--> $DIR/assoc-const-eq-bound-var-in-ty-not-wf.rs:10:13
16+
--> $DIR/assoc-const-eq-bound-var-in-ty-not-wf.rs:12:13
1717
|
1818
LL | _: impl Trait<
1919
| _____________^

tests/ui/associated-consts/assoc-const-eq-bound-var-in-ty.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
//
44
//@ check-pass
55

6-
#![feature(associated_const_equality)]
6+
#![feature(associated_const_equality, min_generic_const_args)]
7+
#![allow(incomplete_features)]
78

89
trait Trait<T> {
10+
#[type_const]
911
const K: T;
1012
}
1113

tests/ui/associated-consts/assoc-const-eq-const_evaluatable_unchecked.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
//
55
// issue: <https://github.com/rust-lang/rust/issues/108220>
66
//@ check-pass
7-
#![feature(associated_const_equality)]
7+
#![feature(associated_const_equality, min_generic_const_args)]
8+
#![allow(incomplete_features)]
89

9-
pub trait TraitA<T> { const K: u8 = 0; }
10+
pub trait TraitA<T> {
11+
#[type_const]
12+
const K: u8 = 0;
13+
}
1014
pub trait TraitB<T> {}
1115

1216
impl<T> TraitA<T> for () {}

0 commit comments

Comments
 (0)