Skip to content

Commit f55c37c

Browse files
committed
Port #[type_const] to the new attribute system
1 parent 808577a commit f55c37c

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
@@ -344,6 +344,9 @@ pub enum AttributeKind {
344344
/// Represents `#[track_caller]`
345345
TrackCaller(Span),
346346

347+
/// Represents `#[type_const]`.
348+
TypeConst(Span),
349+
347350
/// Represents `#[used]`
348351
Used { used_by: UsedBy, span: Span },
349352
// 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
@@ -54,6 +54,7 @@ impl AttributeKind {
5454
Stability { .. } => Yes,
5555
TargetFeature(..) => No,
5656
TrackCaller(..) => Yes,
57+
TypeConst(..) => Yes,
5758
Used { .. } => No,
5859
// tidy-alphabetical-end
5960
}

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
@@ -41,7 +41,7 @@ use crate::attributes::stability::{
4141
use crate::attributes::test_attrs::IgnoreParser;
4242
use crate::attributes::traits::{
4343
CoinductiveParser, ConstTraitParser, DenyExplicitImplParser, DoNotImplementViaObjectParser,
44-
SkipDuringMethodDispatchParser,
44+
SkipDuringMethodDispatchParser, TypeConstParser,
4545
};
4646
use crate::attributes::transparency::TransparencyParser;
4747
use crate::attributes::{AttributeParser as _, Combine, Single, WithoutArgs};
@@ -160,6 +160,7 @@ attribute_parsers!(
160160
Single<WithoutArgs<PassByValueParser>>,
161161
Single<WithoutArgs<PubTransparentParser>>,
162162
Single<WithoutArgs<TrackCallerParser>>,
163+
Single<WithoutArgs<TypeConstParser>>,
163164
// tidy-alphabetical-end
164165
];
165166
);

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
@@ -283,6 +283,7 @@ pub fn check_builtin_meta_item(
283283
| sym::rustc_do_not_implement_via_object
284284
| sym::rustc_coinductive
285285
| sym::const_trait
286+
| sym::type_const
286287
| sym::repr
287288
| sym::align
288289
| 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
}
@@ -330,9 +333,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
330333
[sym::coroutine, ..] => {
331334
self.check_coroutine(attr, target);
332335
}
333-
[sym::type_const, ..] => {
334-
self.check_type_const(hir_id,attr, target);
335-
}
336336
[sym::linkage, ..] => self.check_linkage(attr, span, target),
337337
[
338338
// ok
@@ -2508,7 +2508,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
25082508
}
25092509
}
25102510

2511-
fn check_type_const(&self, hir_id: HirId, attr: &Attribute, target: Target) {
2511+
fn check_type_const(&self, hir_id: HirId, attr_span: Span, target: Target) {
25122512
let tcx = self.tcx;
25132513
if target == Target::AssocConst
25142514
&& let parent = tcx.parent(hir_id.expect_owner().to_def_id())
@@ -2518,7 +2518,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
25182518
} else {
25192519
self.dcx()
25202520
.struct_span_err(
2521-
attr.span(),
2521+
attr_span,
25222522
"`#[type_const]` must only be applied to trait associated constants",
25232523
)
25242524
.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:143: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:155:1
133127
|
@@ -546,6 +540,15 @@ LL | #[non_exhaustive = 1]
546540
| | didn't expect any arguments here
547541
| help: must be of the form: `#[non_exhaustive]`
548542

543+
error[E0565]: malformed `type_const` attribute input
544+
--> $DIR/malformed-attrs.rs:143:5
545+
|
546+
LL | #[type_const = 1]
547+
| ^^^^^^^^^^^^^---^
548+
| | |
549+
| | didn't expect any arguments here
550+
| help: must be of the form: `#[type_const]`
551+
549552
error: attribute should be applied to `const fn`
550553
--> $DIR/malformed-attrs.rs:34:1
551554
|

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)