Skip to content

Commit 244d64e

Browse files
Port #[path] to the new attribute parsing infrastructure
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent ef3d774 commit 244d64e

File tree

12 files changed

+88
-36
lines changed

12 files changed

+88
-36
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ pub enum AttributeKind {
298298
/// Represents `#[rustc_pass_by_value]` (used by the `rustc_pass_by_value` lint).
299299
PassByValue(Span),
300300

301+
/// Represents `#[path]`
302+
Path(Symbol, Span),
303+
301304
/// Represents `#[rustc_pub_transparent]` (used by the `repr_transparent_external_private_fields` lint).
302305
PubTransparent(Span),
303306

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl AttributeKind {
4040
NonExhaustive(..) => Yes,
4141
Optimize(..) => No,
4242
PassByValue(..) => Yes,
43+
Path(..) => No,
4344
PubTransparent(..) => Yes,
4445
Repr { .. } => No,
4546
RustcLayoutScalarValidRangeEnd(..) => Yes,

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub(crate) mod loop_match;
3737
pub(crate) mod must_use;
3838
pub(crate) mod no_implicit_prelude;
3939
pub(crate) mod non_exhaustive;
40+
pub(crate) mod path;
4041
pub(crate) mod repr;
4142
pub(crate) mod rustc_internal;
4243
pub(crate) mod semantics;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use rustc_attr_data_structures::AttributeKind;
2+
use rustc_feature::{AttributeTemplate, template};
3+
use rustc_span::{Symbol, sym};
4+
5+
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
6+
use crate::context::{AcceptContext, Stage};
7+
use crate::parser::ArgParser;
8+
9+
pub(crate) struct PathParser;
10+
11+
impl<S: Stage> SingleAttributeParser<S> for PathParser {
12+
const PATH: &[Symbol] = &[sym::path];
13+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
14+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
15+
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "file");
16+
17+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
18+
let Some(nv) = args.name_value() else {
19+
cx.expected_name_value(cx.attr_span, None);
20+
return None;
21+
};
22+
let Some(path) = nv.value_as_str() else {
23+
cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
24+
return None;
25+
};
26+
27+
Some(AttributeKind::Path(path, cx.attr_span))
28+
}
29+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
2828
use crate::attributes::must_use::MustUseParser;
2929
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
3030
use crate::attributes::non_exhaustive::NonExhaustiveParser;
31+
use crate::attributes::path::PathParser as PathAttributeParser;
3132
use crate::attributes::repr::{AlignParser, ReprParser};
3233
use crate::attributes::rustc_internal::{
3334
RustcLayoutScalarValidRangeEnd, RustcLayoutScalarValidRangeStart,
@@ -133,6 +134,7 @@ attribute_parsers!(
133134
Single<LinkSectionParser>,
134135
Single<MustUseParser>,
135136
Single<OptimizeParser>,
137+
Single<PathAttributeParser>,
136138
Single<RustcForceInlineParser>,
137139
Single<RustcLayoutScalarValidRangeEnd>,
138140
Single<RustcLayoutScalarValidRangeStart>,

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ pub fn check_builtin_meta_item(
289289
| sym::naked
290290
| sym::no_mangle
291291
| sym::non_exhaustive
292+
| sym::path
292293
| sym::ignore
293294
| sym::must_use
294295
| sym::track_caller

compiler/rustc_passes/src/check_attr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
191191
target,
192192
Target::Mod,
193193
),
194+
Attribute::Parsed(AttributeKind::Path(_, attr_span)) => {
195+
self.check_generic_attr(hir_id, sym::path, *attr_span, target, Target::Mod)
196+
}
194197
Attribute::Parsed(AttributeKind::TrackCaller(attr_span)) => {
195198
self.check_track_caller(hir_id, *attr_span, attrs, span, target)
196199
}
@@ -2800,7 +2803,6 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
28002803
// resolution for the attribute macro error.
28012804
const ATTRS_TO_CHECK: &[Symbol] = &[
28022805
sym::macro_export,
2803-
sym::path,
28042806
sym::automatically_derived,
28052807
sym::rustc_main,
28062808
sym::derive,
@@ -2822,6 +2824,8 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
28222824
}) = attr
28232825
{
28242826
(*first_attr_span, sym::repr)
2827+
} else if let Attribute::Parsed(AttributeKind::Path(.., span)) = attr {
2828+
(*span, sym::path)
28252829
} else {
28262830
continue;
28272831
};

tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.stderr

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,6 @@ LL - #![rustc_main]
121121
LL + #[rustc_main]
122122
|
123123

124-
error: `path` attribute cannot be used at crate level
125-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:21:1
126-
|
127-
LL | #![path = "3800"]
128-
| ^^^^^^^^^^^^^^^^^
129-
...
130-
LL | mod inline {
131-
| ------ the inner attribute doesn't annotate this module
132-
|
133-
help: perhaps you meant to use an outer attribute
134-
|
135-
LL - #![path = "3800"]
136-
LL + #[path = "3800"]
137-
|
138-
139124
error: `automatically_derived` attribute cannot be used at crate level
140125
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:23:1
141126
|
@@ -166,6 +151,21 @@ LL - #![repr()]
166151
LL + #[repr()]
167152
|
168153

154+
error: `path` attribute cannot be used at crate level
155+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:21:1
156+
|
157+
LL | #![path = "3800"]
158+
| ^^^^^^^^^^^^^^^^^
159+
...
160+
LL | mod inline {
161+
| ------ the inner attribute doesn't annotate this module
162+
|
163+
help: perhaps you meant to use an outer attribute
164+
|
165+
LL - #![path = "3800"]
166+
LL + #[path = "3800"]
167+
|
168+
169169
error[E0518]: attribute should be applied to function or closure
170170
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:42:17
171171
|

tests/ui/lint/unused/unused-attr-duplicate.stderr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,6 @@ note: attribute also specified here
2727
LL | #[macro_use]
2828
| ^^^^^^^^^^^^
2929

30-
error: unused attribute
31-
--> $DIR/unused-attr-duplicate.rs:47:1
32-
|
33-
LL | #[path = "bar.rs"]
34-
| ^^^^^^^^^^^^^^^^^^ help: remove this attribute
35-
|
36-
note: attribute also specified here
37-
--> $DIR/unused-attr-duplicate.rs:46:1
38-
|
39-
LL | #[path = "auxiliary/lint_unused_extern_crate.rs"]
40-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
42-
4330
error: unused attribute
4431
--> $DIR/unused-attr-duplicate.rs:55:1
4532
|
@@ -153,6 +140,19 @@ note: attribute also specified here
153140
LL | #[macro_export]
154141
| ^^^^^^^^^^^^^^^
155142

143+
error: unused attribute
144+
--> $DIR/unused-attr-duplicate.rs:47:1
145+
|
146+
LL | #[path = "bar.rs"]
147+
| ^^^^^^^^^^^^^^^^^^ help: remove this attribute
148+
|
149+
note: attribute also specified here
150+
--> $DIR/unused-attr-duplicate.rs:46:1
151+
|
152+
LL | #[path = "auxiliary/lint_unused_extern_crate.rs"]
153+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
154+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
155+
156156
error: unused attribute
157157
--> $DIR/unused-attr-duplicate.rs:53:1
158158
|

tests/ui/lint/unused/unused-attr-macro-rules.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ note: the lint level is defined here
1010
LL | #![deny(unused_attributes)]
1111
| ^^^^^^^^^^^^^^^^^
1212

13-
error: `#[path]` only has an effect on modules
14-
--> $DIR/unused-attr-macro-rules.rs:8:1
15-
|
16-
LL | #[path="foo"]
17-
| ^^^^^^^^^^^^^
18-
1913
error: crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]`
2014
--> $DIR/unused-attr-macro-rules.rs:9:1
2115
|
2216
LL | #[recursion_limit="1"]
2317
| ^^^^^^^^^^^^^^^^^^^^^^
2418

19+
error: `#[path]` only has an effect on modules
20+
--> $DIR/unused-attr-macro-rules.rs:8:1
21+
|
22+
LL | #[path="foo"]
23+
| ^^^^^^^^^^^^^
24+
2525
error: aborting due to 3 previous errors
2626

tests/ui/resolve/path-attr-in-const-block.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ fn main() {
55
const {
66
#![path = foo!()]
77
//~^ ERROR: cannot find macro `foo` in this scope
8+
//~| ERROR malformed `path` attribute input
89
}
910
}

tests/ui/resolve/path-attr-in-const-block.stderr

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,15 @@ error: cannot find macro `foo` in this scope
44
LL | #![path = foo!()]
55
| ^^^
66

7-
error: aborting due to 1 previous error
7+
error[E0539]: malformed `path` attribute input
8+
--> $DIR/path-attr-in-const-block.rs:6:9
9+
|
10+
LL | #![path = foo!()]
11+
| ^^^^^^^^^^------^
12+
| | |
13+
| | expected a string literal here
14+
| help: must be of the form: `#[path = "file"]`
15+
16+
error: aborting due to 2 previous errors
817

18+
For more information about this error, try `rustc --explain E0539`.

0 commit comments

Comments
 (0)