Skip to content

Commit 7daa8d2

Browse files
committed
Port #[type_const] to the new attribute system
1 parent b411784 commit 7daa8d2

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
@@ -336,6 +336,9 @@ pub enum AttributeKind {
336336
/// Represents `#[track_caller]`
337337
TrackCaller(Span),
338338

339+
/// Represents `#[type_const]`.
340+
TypeConst(Span),
341+
339342
/// Represents `#[used]`
340343
Used { used_by: UsedBy, span: Span },
341344
// 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
@@ -52,6 +52,7 @@ impl AttributeKind {
5252
Stability { .. } => Yes,
5353
TargetFeature(..) => No,
5454
TrackCaller(..) => Yes,
55+
TypeConst(..) => Yes,
5556
Used { .. } => No,
5657
// tidy-alphabetical-end
5758
}

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
@@ -39,7 +39,7 @@ use crate::attributes::stability::{
3939
};
4040
use crate::attributes::traits::{
4141
CoinductiveParser, ConstTraitParser, DenyExplicitImplParser, DoNotImplementViaObjectParser,
42-
SkipDuringMethodDispatchParser,
42+
SkipDuringMethodDispatchParser, TypeConstParser,
4343
};
4444
use crate::attributes::transparency::TransparencyParser;
4545
use crate::attributes::{AttributeParser as _, Combine, Single, WithoutArgs};
@@ -156,6 +156,7 @@ attribute_parsers!(
156156
Single<WithoutArgs<PassByValueParser>>,
157157
Single<WithoutArgs<PubTransparentParser>>,
158158
Single<WithoutArgs<TrackCallerParser>>,
159+
Single<WithoutArgs<TypeConstParser>>,
159160
// tidy-alphabetical-end
160161
];
161162
);

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
@@ -298,6 +298,7 @@ fn emit_malformed_attribute(
298298
| sym::rustc_do_not_implement_via_object
299299
| sym::rustc_coinductive
300300
| sym::const_trait
301+
| sym::type_const
301302
| sym::repr
302303
| sym::align
303304
| 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
}
@@ -324,9 +327,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
324327
[sym::coroutine, ..] => {
325328
self.check_coroutine(attr, target);
326329
}
327-
[sym::type_const, ..] => {
328-
self.check_type_const(hir_id,attr, target);
329-
}
330330
[sym::linkage, ..] => self.check_linkage(attr, span, target),
331331
[
332332
// ok
@@ -2541,7 +2541,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
25412541
}
25422542
}
25432543

2544-
fn check_type_const(&self, hir_id: HirId, attr: &Attribute, target: Target) {
2544+
fn check_type_const(&self, hir_id: HirId, attr_span: Span, target: Target) {
25452545
let tcx = self.tcx;
25462546
if target == Target::AssocConst
25472547
&& let parent = tcx.parent(hir_id.expect_owner().to_def_id())
@@ -2551,7 +2551,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
25512551
} else {
25522552
self.dcx()
25532553
.struct_span_err(
2554-
attr.span(),
2554+
attr_span,
25552555
"`#[type_const]` must only be applied to trait associated constants",
25562556
)
25572557
.emit();

tests/ui/attributes/malformed-attrs.stderr

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

125-
error: malformed `type_const` attribute input
126-
--> $DIR/malformed-attrs.rs:142:5
127-
|
128-
LL | #[type_const = 1]
129-
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[type_const]`
130-
131125
error: malformed `marker` attribute input
132126
--> $DIR/malformed-attrs.rs:154:1
133127
|
@@ -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:142: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)