Skip to content

Commit 02fb831

Browse files
Port #[path] to the new attribute parsing infrastructure
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent 45a5b89 commit 02fb831

File tree

12 files changed

+114
-61
lines changed

12 files changed

+114
-61
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ pub enum AttributeKind {
287287
/// Represents `#[optimize(size|speed)]`
288288
Optimize(OptimizeAttr, Span),
289289

290+
/// Represents `#[path]`
291+
Path(Symbol, Span),
292+
290293
/// Represents `#[rustc_pub_transparent]` (used by the `repr_transparent_external_private_fields` lint).
291294
PubTransparent(Span),
292295

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ impl AttributeKind {
3838
NoImplicitPrelude(..) => No,
3939
NoMangle(..) => No,
4040
Optimize(..) => No,
41+
Path(..) => No,
4142
PubTransparent(..) => Yes,
4243
RustcLayoutScalarValidRangeEnd(..) => Yes,
4344
RustcLayoutScalarValidRangeStart(..) => Yes,

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub(crate) mod lint_helpers;
3636
pub(crate) mod loop_match;
3737
pub(crate) mod must_use;
3838
pub(crate) mod no_implicit_prelude;
39+
pub(crate) mod path;
3940
pub(crate) mod repr;
4041
pub(crate) mod rustc_internal;
4142
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
@@ -27,6 +27,7 @@ use crate::attributes::lint_helpers::{AsPtrParser, PubTransparentParser};
2727
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
2828
use crate::attributes::must_use::MustUseParser;
2929
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
30+
use crate::attributes::path::PathParser as PathAttributeParser;
3031
use crate::attributes::repr::{AlignParser, ReprParser};
3132
use crate::attributes::rustc_internal::{
3233
RustcLayoutScalarValidRangeEnd, RustcLayoutScalarValidRangeStart,
@@ -130,6 +131,7 @@ attribute_parsers!(
130131
Single<LinkSectionParser>,
131132
Single<MustUseParser>,
132133
Single<OptimizeParser>,
134+
Single<PathAttributeParser>,
133135
Single<RustcForceInlineParser>,
134136
Single<RustcLayoutScalarValidRangeEnd>,
135137
Single<RustcLayoutScalarValidRangeStart>,

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -271,31 +271,33 @@ pub fn check_builtin_meta_item(
271271
if matches!(
272272
name,
273273
sym::inline
274-
| sym::may_dangle
275-
| sym::rustc_as_ptr
276-
| sym::rustc_pub_transparent
277-
| sym::rustc_const_stable_indirect
278-
| sym::rustc_force_inline
279-
| sym::rustc_confusables
280-
| sym::rustc_skip_during_method_dispatch
281-
| sym::repr
282-
| sym::align
283-
| sym::deprecated
284-
| sym::optimize
285-
| sym::cold
286-
| sym::target_feature
287-
| sym::rustc_allow_const_fn_unstable
288-
| sym::naked
289-
| sym::no_mangle
290-
| sym::must_use
291-
| sym::track_caller
292-
| sym::link_name
293-
| sym::export_name
294-
| sym::rustc_macro_transparency
295-
| sym::link_section
296-
| sym::rustc_layout_scalar_valid_range_start
297-
| sym::rustc_layout_scalar_valid_range_end
298-
| sym::no_implicit_prelude) {
274+
| sym::may_dangle
275+
| sym::rustc_as_ptr
276+
| sym::rustc_pub_transparent
277+
| sym::rustc_const_stable_indirect
278+
| sym::rustc_force_inline
279+
| sym::rustc_confusables
280+
| sym::rustc_skip_during_method_dispatch
281+
| sym::repr
282+
| sym::align
283+
| sym::deprecated
284+
| sym::optimize
285+
| sym::cold
286+
| sym::target_feature
287+
| sym::rustc_allow_const_fn_unstable
288+
| sym::naked
289+
| sym::no_mangle
290+
| sym::must_use
291+
| sym::track_caller
292+
| sym::link_name
293+
| sym::export_name
294+
| sym::rustc_macro_transparency
295+
| sym::link_section
296+
| sym::rustc_layout_scalar_valid_range_start
297+
| sym::rustc_layout_scalar_valid_range_end
298+
| sym::path
299+
| sym::no_implicit_prelude
300+
) {
299301
return;
300302
}
301303
emit_malformed_attribute(psess, style, meta.span, name, template);

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
}
@@ -2831,7 +2834,6 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
28312834
// resolution for the attribute macro error.
28322835
const ATTRS_TO_CHECK: &[Symbol] = &[
28332836
sym::macro_export,
2834-
sym::path,
28352837
sym::automatically_derived,
28362838
sym::rustc_main,
28372839
sym::derive,
@@ -2849,6 +2851,8 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
28492851
(attr.span(), *a)
28502852
} else if let Attribute::Parsed(AttributeKind::Repr(r)) = attr {
28512853
(r.first().unwrap().1, sym::repr)
2854+
} else if let Attribute::Parsed(AttributeKind::Path(.., span)) = attr {
2855+
(*span, sym::path)
28522856
} else {
28532857
continue;
28542858
};

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:53:1
4532
|
@@ -177,6 +164,19 @@ note: attribute also specified here
177164
LL | #[macro_export]
178165
| ^^^^^^^^^^^^^^^
179166

167+
error: unused attribute
168+
--> $DIR/unused-attr-duplicate.rs:47:1
169+
|
170+
LL | #[path = "bar.rs"]
171+
| ^^^^^^^^^^^^^^^^^^ help: remove this attribute
172+
|
173+
note: attribute also specified here
174+
--> $DIR/unused-attr-duplicate.rs:46:1
175+
|
176+
LL | #[path = "auxiliary/lint_unused_extern_crate.rs"]
177+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
178+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
179+
180180
error: unused attribute
181181
--> $DIR/unused-attr-duplicate.rs:60:1
182182
|

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)