Skip to content

Commit 34b16b4

Browse files
committed
Port #[type_const] to the new attribute system
1 parent a24b1c4 commit 34b16b4

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
@@ -327,6 +327,9 @@ pub enum AttributeKind {
327327
/// Represents `#[track_caller]`
328328
TrackCaller(Span),
329329

330+
/// Represents `#[type_const]`.
331+
TypeConst(Span),
332+
330333
/// Represents `#[used]`
331334
Used { used_by: UsedBy, span: Span },
332335
// 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
@@ -49,6 +49,7 @@ impl AttributeKind {
4949
Stability { .. } => Yes,
5050
TargetFeature(..) => No,
5151
TrackCaller(..) => Yes,
52+
TypeConst(..) => Yes,
5253
Used { .. } => No,
5354
// tidy-alphabetical-end
5455
}

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
@@ -37,7 +37,7 @@ use crate::attributes::stability::{
3737
};
3838
use crate::attributes::traits::{
3939
CoinductiveParser, ConstTraitParser, DenyExplicitImplParser, DoNotImplementViaObjectParser,
40-
SkipDuringMethodDispatchParser,
40+
SkipDuringMethodDispatchParser, TypeConstParser,
4141
};
4242
use crate::attributes::transparency::TransparencyParser;
4343
use crate::attributes::{AttributeParser as _, Combine, Single, WithoutArgs};
@@ -151,6 +151,7 @@ attribute_parsers!(
151151
Single<WithoutArgs<NoMangleParser>>,
152152
Single<WithoutArgs<PubTransparentParser>>,
153153
Single<WithoutArgs<TrackCallerParser>>,
154+
Single<WithoutArgs<TypeConstParser>>,
154155
// tidy-alphabetical-end
155156
];
156157
);

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
@@ -297,6 +297,7 @@ fn emit_malformed_attribute(
297297
| sym::rustc_do_not_implement_via_object
298298
| sym::rustc_coinductive
299299
| sym::const_trait
300+
| sym::type_const
300301
| sym::repr
301302
| sym::align
302303
| 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
}
@@ -315,9 +318,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
315318
[sym::coroutine, ..] => {
316319
self.check_coroutine(attr, target);
317320
}
318-
[sym::type_const, ..] => {
319-
self.check_type_const(hir_id,attr, target);
320-
}
321321
[sym::linkage, ..] => self.check_linkage(attr, span, target),
322322
[
323323
// ok
@@ -2512,7 +2512,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
25122512
}
25132513
}
25142514

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

tests/ui/attributes/malformed-attrs.stderr

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

131-
error: malformed `type_const` attribute input
132-
--> $DIR/malformed-attrs.rs:142:5
133-
|
134-
LL | #[type_const = 1]
135-
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[type_const]`
136-
137131
error: malformed `marker` attribute input
138132
--> $DIR/malformed-attrs.rs:154:1
139133
|
@@ -549,6 +543,15 @@ LL | #[rustc_layout_scalar_valid_range_end]
549543
| expected this to be a list
550544
| help: must be of the form: `#[rustc_layout_scalar_valid_range_end(end)]`
551545

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

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)