Skip to content

Commit 3b4d221

Browse files
BoxyUwUcamelid
authored andcommitted
fix associated_const_equality tests
1 parent 96d97f7 commit 3b4d221

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,
@@ -578,7 +579,32 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
578579
term,
579580
})
580581
});
581-
bounds.push((bound.upcast(tcx), constraint.span));
582+
583+
if let ty::AssocTag::Const = assoc_tag
584+
&& !find_attr!(
585+
self.tcx().get_all_attrs(assoc_item.def_id),
586+
AttributeKind::TypeConst(_)
587+
)
588+
{
589+
if tcx.features().min_generic_const_args()
590+
|| tcx.features().associated_const_equality()
591+
{
592+
let mut err = self.dcx().struct_span_err(
593+
constraint.span,
594+
"use of trait associated const without `#[type_const]`",
595+
);
596+
err.note("the declaration in the trait must be marked with `#[type_const]`");
597+
return Err(err.emit());
598+
} else {
599+
let err = self.dcx().span_delayed_bug(
600+
constraint.span,
601+
"use of trait associated const without `#[type_const]`",
602+
);
603+
return Err(err);
604+
}
605+
} else {
606+
bounds.push((bound.upcast(tcx), constraint.span));
607+
}
582608
}
583609
// SelfTraitThatDefines is only interested in trait predicates.
584610
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};
@@ -1244,7 +1245,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
12441245
LowerTypeRelativePathMode::Const,
12451246
)? {
12461247
TypeRelativePath::AssocItem(def_id, args) => {
1247-
if !tcx.associated_item(def_id).is_type_const_capable(tcx) {
1248+
if !find_attr!(self.tcx().get_all_attrs(def_id), AttributeKind::TypeConst(_)) {
12481249
let mut err = self.dcx().struct_span_err(
12491250
span,
12501251
"use of trait associated const without `#[type_const]`",
@@ -1685,6 +1686,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
16851686
ty::AssocTag::Const,
16861687
) {
16871688
Ok((item_def_id, item_args)) => {
1689+
if !find_attr!(self.tcx().get_all_attrs(item_def_id), AttributeKind::TypeConst(_)) {
1690+
let mut err = self.dcx().struct_span_err(
1691+
span,
1692+
"use of `const` in the type system without `#[type_const]`",
1693+
);
1694+
err.note("the declaration must be marked with `#[type_const]`");
1695+
return Const::new_error(self.tcx(), err.emit());
1696+
}
1697+
16881698
let uv = ty::UnevaluatedConst::new(item_def_id, item_args);
16891699
Const::new_unevaluated(self.tcx(), uv)
16901700
}

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
@@ -2091,19 +2091,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
20912091
}
20922092
}
20932093

2094-
fn check_type_const(&self, hir_id: HirId, attr_span: Span, target: Target) {
2095-
let tcx = self.tcx;
2096-
if target == Target::AssocConst
2097-
&& let parent = tcx.parent(hir_id.expect_owner().to_def_id())
2098-
&& self.tcx.def_kind(parent) == DefKind::Trait
2099-
{
2094+
fn check_type_const(&self, _hir_id: HirId, attr_span: Span, target: Target) {
2095+
if matches!(target, Target::AssocConst | Target::Const) {
21002096
return;
21012097
} else {
21022098
self.dcx()
2103-
.struct_span_err(
2104-
attr_span,
2105-
"`#[type_const]` must only be applied to trait associated constants",
2106-
)
2099+
.struct_span_err(attr_span, "`#[type_const]` must only be applied to const items")
21072100
.emit();
21082101
}
21092102
}

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)