Skip to content

Commit 486e5db

Browse files
committed
Port #[type_const] to the new attribute system
1 parent 5f9005b commit 486e5db

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
@@ -333,6 +333,9 @@ pub enum AttributeKind {
333333
/// Represents `#[track_caller]`
334334
TrackCaller(Span),
335335

336+
/// Represents `#[type_const]`.
337+
TypeConst(Span),
338+
336339
/// Represents `#[used]`
337340
Used { used_by: UsedBy, span: Span },
338341
// 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
@@ -51,6 +51,7 @@ impl AttributeKind {
5151
Stability { .. } => Yes,
5252
TargetFeature(..) => No,
5353
TrackCaller(..) => Yes,
54+
TypeConst(..) => Yes,
5455
Used { .. } => No,
5556
// tidy-alphabetical-end
5657
}

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
@@ -38,7 +38,7 @@ use crate::attributes::stability::{
3838
};
3939
use crate::attributes::traits::{
4040
CoinductiveParser, ConstTraitParser, DenyExplicitImplParser, DoNotImplementViaObjectParser,
41-
SkipDuringMethodDispatchParser,
41+
SkipDuringMethodDispatchParser, TypeConstParser,
4242
};
4343
use crate::attributes::transparency::TransparencyParser;
4444
use crate::attributes::{AttributeParser as _, Combine, Single, WithoutArgs};
@@ -154,6 +154,7 @@ attribute_parsers!(
154154
Single<WithoutArgs<PassByValueParser>>,
155155
Single<WithoutArgs<PubTransparentParser>>,
156156
Single<WithoutArgs<TrackCallerParser>>,
157+
Single<WithoutArgs<TypeConstParser>>,
157158
// tidy-alphabetical-end
158159
];
159160
);

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
}
@@ -322,9 +325,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
322325
[sym::coroutine, ..] => {
323326
self.check_coroutine(attr, target);
324327
}
325-
[sym::type_const, ..] => {
326-
self.check_type_const(hir_id,attr, target);
327-
}
328328
[sym::linkage, ..] => self.check_linkage(attr, span, target),
329329
[
330330
// 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
|
@@ -552,6 +546,15 @@ LL | #[rustc_layout_scalar_valid_range_end]
552546
| expected this to be a list
553547
| help: must be of the form: `#[rustc_layout_scalar_valid_range_end(end)]`
554548

549+
error[E0565]: malformed `type_const` attribute input
550+
--> $DIR/malformed-attrs.rs:142:5
551+
|
552+
LL | #[type_const = 1]
553+
| ^^^^^^^^^^^^^---^
554+
| | |
555+
| | didn't expect any arguments here
556+
| help: must be of the form: `#[type_const]`
557+
555558
error: attribute should be applied to `const fn`
556559
--> $DIR/malformed-attrs.rs:34:1
557560
|

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)