Skip to content

Commit 813ec60

Browse files
committed
Port #[type_const] to the new attribute system
1 parent 6f8e92d commit 813ec60

File tree

9 files changed

+42
-21
lines changed

9 files changed

+42
-21
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ pub enum AttributeKind {
359359
/// Represents `#[track_caller]`
360360
TrackCaller(Span),
361361

362+
/// Represents `#[type_const]`.
363+
TypeConst(Span),
364+
362365
/// Represents `#[used]`
363366
Used { used_by: UsedBy, span: Span },
364367
// tidy-alphabetical-end

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ impl AttributeKind {
5959
StdInternalSymbol(..) => No,
6060
TargetFeature(..) => No,
6161
TrackCaller(..) => Yes,
62+
TypeConst(..) => Yes,
6263
Used { .. } => No,
6364
// tidy-alphabetical-end
6465
}

compiler/rustc_attr_parsing/src/attributes/traits.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,10 @@ impl<S: Stage> NoArgsAttributeParser<S> for CoinductiveParser {
8282
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
8383
const CREATE: fn(Span) -> AttributeKind = AttributeKind::Coinductive;
8484
}
85+
86+
pub(crate) struct TypeConstParser;
87+
impl<S: Stage> NoArgsAttributeParser<S> for TypeConstParser {
88+
const PATH: &[Symbol] = &[sym::type_const];
89+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
90+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::TypeConst;
91+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::attributes::stability::{
4545
use crate::attributes::test_attrs::IgnoreParser;
4646
use crate::attributes::traits::{
4747
CoinductiveParser, ConstTraitParser, DenyExplicitImplParser, DoNotImplementViaObjectParser,
48-
SkipDuringMethodDispatchParser,
48+
SkipDuringMethodDispatchParser, TypeConstParser,
4949
};
5050
use crate::attributes::transparency::TransparencyParser;
5151
use crate::attributes::{AttributeParser as _, Combine, Single, WithoutArgs};
@@ -169,6 +169,7 @@ attribute_parsers!(
169169
Single<WithoutArgs<PubTransparentParser>>,
170170
Single<WithoutArgs<StdInternalSymbolParser>>,
171171
Single<WithoutArgs<TrackCallerParser>>,
172+
Single<WithoutArgs<TypeConstParser>>,
172173
// tidy-alphabetical-end
173174
];
174175
);

compiler/rustc_middle/src/ty/assoc.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use rustc_attr_data_structures::{AttributeKind, find_attr};
12
use rustc_data_structures::sorted_map::SortedIndexMultiMap;
23
use rustc_hir as hir;
34
use rustc_hir::def::{DefKind, Namespace};
45
use rustc_hir::def_id::DefId;
56
use rustc_macros::{Decodable, Encodable, HashStable};
6-
use rustc_span::{Ident, Symbol, sym};
7+
use rustc_span::{Ident, Symbol};
78

89
use super::{TyCtxt, Visibility};
910
use crate::ty;
@@ -160,7 +161,7 @@ impl AssocItem {
160161
// Inherent impl but this attr is only applied to trait assoc items.
161162
(AssocItemContainer::Impl, None) => return true,
162163
};
163-
tcx.has_attr(def_id, sym::type_const)
164+
find_attr!(tcx.get_all_attrs(def_id), AttributeKind::TypeConst(_))
164165
}
165166
}
166167

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ pub fn check_builtin_meta_item(
287287
| sym::rustc_do_not_implement_via_object
288288
| sym::rustc_coinductive
289289
| sym::const_trait
290+
| sym::type_const
290291
| sym::repr
291292
| sym::align
292293
| sym::deprecated

compiler/rustc_passes/src/check_attr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
129129
) => {
130130
self.check_must_be_applied_to_trait(*attr_span, span, target);
131131
}
132+
&Attribute::Parsed(AttributeKind::TypeConst(attr_span)) => {
133+
self.check_type_const(hir_id, attr_span, target)
134+
}
132135
Attribute::Parsed(AttributeKind::Confusables { first_span, .. }) => {
133136
self.check_confusables(*first_span, target);
134137
}
@@ -338,9 +341,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
338341
[sym::coroutine, ..] => {
339342
self.check_coroutine(attr, target);
340343
}
341-
[sym::type_const, ..] => {
342-
self.check_type_const(hir_id,attr, target);
343-
}
344344
[sym::linkage, ..] => self.check_linkage(attr, span, target),
345345
[
346346
// ok
@@ -2513,7 +2513,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
25132513
}
25142514
}
25152515

2516-
fn check_type_const(&self, hir_id: HirId, attr: &Attribute, target: Target) {
2516+
fn check_type_const(&self, hir_id: HirId, attr_span: Span, target: Target) {
25172517
let tcx = self.tcx;
25182518
if target == Target::AssocConst
25192519
&& let parent = tcx.parent(hir_id.expect_owner().to_def_id())
@@ -2523,7 +2523,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
25232523
} else {
25242524
self.dcx()
25252525
.struct_span_err(
2526-
attr.span(),
2526+
attr_span,
25272527
"`#[type_const]` must only be applied to trait associated constants",
25282528
)
25292529
.emit();

tests/ui/attributes/malformed-attrs.stderr

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,6 @@ error: malformed `cfi_encoding` attribute input
116116
LL | #[cfi_encoding]
117117
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[cfi_encoding = "encoding"]`
118118

119-
error: malformed `type_const` attribute input
120-
--> $DIR/malformed-attrs.rs:143:5
121-
|
122-
LL | #[type_const = 1]
123-
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[type_const]`
124-
125119
error: malformed `marker` attribute input
126120
--> $DIR/malformed-attrs.rs:155:1
127121
|
@@ -555,6 +549,15 @@ LL | #[non_exhaustive = 1]
555549
| | didn't expect any arguments here
556550
| help: must be of the form: `#[non_exhaustive]`
557551

552+
error[E0565]: malformed `type_const` attribute input
553+
--> $DIR/malformed-attrs.rs:143:5
554+
|
555+
LL | #[type_const = 1]
556+
| ^^^^^^^^^^^^^---^
557+
| | |
558+
| | didn't expect any arguments here
559+
| help: must be of the form: `#[type_const]`
560+
558561
error: attribute should be applied to `const fn`
559562
--> $DIR/malformed-attrs.rs:34:1
560563
|

tests/ui/const-generics/mgca/bad-type_const-syntax.stderr

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
error: malformed `type_const` attribute input
2-
--> $DIR/bad-type_const-syntax.rs:2:5
3-
|
4-
LL | #[type_const()]
5-
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[type_const]`
6-
71
error[E0658]: the `#[type_const]` attribute is an experimental feature
82
--> $DIR/bad-type_const-syntax.rs:2:5
93
|
@@ -24,6 +18,15 @@ LL | #[type_const]
2418
= help: add `#![feature(min_generic_const_args)]` to the crate attributes to enable
2519
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2620

21+
error[E0565]: malformed `type_const` attribute input
22+
--> $DIR/bad-type_const-syntax.rs:2:5
23+
|
24+
LL | #[type_const()]
25+
| ^^^^^^^^^^^^--^
26+
| | |
27+
| | didn't expect any arguments here
28+
| help: must be of the form: `#[type_const]`
29+
2730
error: `#[type_const]` must only be applied to trait associated constants
2831
--> $DIR/bad-type_const-syntax.rs:11:5
2932
|
@@ -32,4 +35,5 @@ LL | #[type_const]
3235

3336
error: aborting due to 4 previous errors
3437

35-
For more information about this error, try `rustc --explain E0658`.
38+
Some errors have detailed explanations: E0565, E0658.
39+
For more information about an error, try `rustc --explain E0565`.

0 commit comments

Comments
 (0)