Skip to content

Commit 11b24c1

Browse files
authored
Rollup merge of rust-lang#143344 - JonathanBrouwer:path-parser, r=jdonszelmann
Port `#[path]` to the new attribute parsing infrastructure Ports `#[path]` to the new attribute parsing infrastructure for rust-lang#131229 (comment) This PR duplicates a change from rust-lang#143237 Draft until that one is merged
2 parents 00c67d1 + 244d64e commit 11b24c1

File tree

12 files changed

+123
-72
lines changed

12 files changed

+123
-72
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: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,42 @@ pub fn check_builtin_meta_item(
267267
deny_unsafety: bool,
268268
) {
269269
if !is_attr_template_compatible(&template, &meta.kind) {
270+
// attrs with new parsers are locally validated so excluded here
271+
if matches!(
272+
name,
273+
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::rustc_pass_by_value
282+
| sym::repr
283+
| sym::align
284+
| sym::deprecated
285+
| sym::optimize
286+
| sym::cold
287+
| sym::target_feature
288+
| sym::rustc_allow_const_fn_unstable
289+
| sym::naked
290+
| sym::no_mangle
291+
| sym::non_exhaustive
292+
| sym::path
293+
| sym::ignore
294+
| sym::must_use
295+
| sym::track_caller
296+
| sym::link_name
297+
| sym::export_name
298+
| sym::rustc_macro_transparency
299+
| sym::link_section
300+
| sym::rustc_layout_scalar_valid_range_start
301+
| sym::rustc_layout_scalar_valid_range_end
302+
| sym::no_implicit_prelude
303+
) {
304+
return;
305+
}
270306
emit_malformed_attribute(psess, style, meta.span, name, template);
271307
}
272308

@@ -282,42 +318,6 @@ fn emit_malformed_attribute(
282318
name: Symbol,
283319
template: AttributeTemplate,
284320
) {
285-
// attrs with new parsers are locally validated so excluded here
286-
if matches!(
287-
name,
288-
sym::inline
289-
| sym::may_dangle
290-
| sym::rustc_as_ptr
291-
| sym::rustc_pub_transparent
292-
| sym::rustc_const_stable_indirect
293-
| sym::rustc_force_inline
294-
| sym::rustc_confusables
295-
| sym::rustc_skip_during_method_dispatch
296-
| sym::rustc_pass_by_value
297-
| sym::repr
298-
| sym::align
299-
| sym::deprecated
300-
| sym::optimize
301-
| sym::cold
302-
| sym::target_feature
303-
| sym::rustc_allow_const_fn_unstable
304-
| sym::naked
305-
| sym::no_mangle
306-
| sym::non_exhaustive
307-
| sym::ignore
308-
| sym::must_use
309-
| sym::track_caller
310-
| sym::link_name
311-
| sym::export_name
312-
| sym::rustc_macro_transparency
313-
| sym::link_section
314-
| sym::rustc_layout_scalar_valid_range_start
315-
| sym::rustc_layout_scalar_valid_range_end
316-
| sym::no_implicit_prelude
317-
) {
318-
return;
319-
}
320-
321321
// Some of previously accepted forms were used in practice,
322322
// report them as warnings for now.
323323
let should_warn = |name| matches!(name, sym::doc | sym::link | sym::test | sym::bench);

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
}
@@ -2797,7 +2800,6 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
27972800
// resolution for the attribute macro error.
27982801
const ATTRS_TO_CHECK: &[Symbol] = &[
27992802
sym::macro_export,
2800-
sym::path,
28012803
sym::automatically_derived,
28022804
sym::rustc_main,
28032805
sym::derive,
@@ -2819,6 +2821,8 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
28192821
}) = attr
28202822
{
28212823
(*first_attr_span, sym::repr)
2824+
} else if let Attribute::Parsed(AttributeKind::Path(.., span)) = attr {
2825+
(*span, sym::path)
28222826
} else {
28232827
continue;
28242828
};

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)