Port #[path] to the new attribute parsing infrastructure

Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
This commit is contained in:
Jonathan Brouwer 2025-07-06 22:16:42 +02:00
parent ef3d7741e5
commit 244d64e60b
No known key found for this signature in database
GPG Key ID: 13619B051B673C52
12 changed files with 88 additions and 36 deletions

View File

@ -298,6 +298,9 @@ pub enum AttributeKind {
/// Represents `#[rustc_pass_by_value]` (used by the `rustc_pass_by_value` lint).
PassByValue(Span),
/// Represents `#[path]`
Path(Symbol, Span),
/// Represents `#[rustc_pub_transparent]` (used by the `repr_transparent_external_private_fields` lint).
PubTransparent(Span),

View File

@ -40,6 +40,7 @@ impl AttributeKind {
NonExhaustive(..) => Yes,
Optimize(..) => No,
PassByValue(..) => Yes,
Path(..) => No,
PubTransparent(..) => Yes,
Repr { .. } => No,
RustcLayoutScalarValidRangeEnd(..) => Yes,

View File

@ -37,6 +37,7 @@ pub(crate) mod loop_match;
pub(crate) mod must_use;
pub(crate) mod no_implicit_prelude;
pub(crate) mod non_exhaustive;
pub(crate) mod path;
pub(crate) mod repr;
pub(crate) mod rustc_internal;
pub(crate) mod semantics;

View File

@ -0,0 +1,29 @@
use rustc_attr_data_structures::AttributeKind;
use rustc_feature::{AttributeTemplate, template};
use rustc_span::{Symbol, sym};
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
use crate::context::{AcceptContext, Stage};
use crate::parser::ArgParser;
pub(crate) struct PathParser;
impl<S: Stage> SingleAttributeParser<S> for PathParser {
const PATH: &[Symbol] = &[sym::path];
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "file");
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
let Some(nv) = args.name_value() else {
cx.expected_name_value(cx.attr_span, None);
return None;
};
let Some(path) = nv.value_as_str() else {
cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
return None;
};
Some(AttributeKind::Path(path, cx.attr_span))
}
}

View File

@ -28,6 +28,7 @@ use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
use crate::attributes::must_use::MustUseParser;
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
use crate::attributes::non_exhaustive::NonExhaustiveParser;
use crate::attributes::path::PathParser as PathAttributeParser;
use crate::attributes::repr::{AlignParser, ReprParser};
use crate::attributes::rustc_internal::{
RustcLayoutScalarValidRangeEnd, RustcLayoutScalarValidRangeStart,
@ -133,6 +134,7 @@ attribute_parsers!(
Single<LinkSectionParser>,
Single<MustUseParser>,
Single<OptimizeParser>,
Single<PathAttributeParser>,
Single<RustcForceInlineParser>,
Single<RustcLayoutScalarValidRangeEnd>,
Single<RustcLayoutScalarValidRangeStart>,

View File

@ -289,6 +289,7 @@ pub fn check_builtin_meta_item(
| sym::naked
| sym::no_mangle
| sym::non_exhaustive
| sym::path
| sym::ignore
| sym::must_use
| sym::track_caller

View File

@ -191,6 +191,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
target,
Target::Mod,
),
Attribute::Parsed(AttributeKind::Path(_, attr_span)) => {
self.check_generic_attr(hir_id, sym::path, *attr_span, target, Target::Mod)
}
Attribute::Parsed(AttributeKind::TrackCaller(attr_span)) => {
self.check_track_caller(hir_id, *attr_span, attrs, span, target)
}
@ -2800,7 +2803,6 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
// resolution for the attribute macro error.
const ATTRS_TO_CHECK: &[Symbol] = &[
sym::macro_export,
sym::path,
sym::automatically_derived,
sym::rustc_main,
sym::derive,
@ -2822,6 +2824,8 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
}) = attr
{
(*first_attr_span, sym::repr)
} else if let Attribute::Parsed(AttributeKind::Path(.., span)) = attr {
(*span, sym::path)
} else {
continue;
};

View File

@ -121,21 +121,6 @@ LL - #![rustc_main]
LL + #[rustc_main]
|
error: `path` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:21:1
|
LL | #![path = "3800"]
| ^^^^^^^^^^^^^^^^^
...
LL | mod inline {
| ------ the inner attribute doesn't annotate this module
|
help: perhaps you meant to use an outer attribute
|
LL - #![path = "3800"]
LL + #[path = "3800"]
|
error: `automatically_derived` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:23:1
|
@ -166,6 +151,21 @@ LL - #![repr()]
LL + #[repr()]
|
error: `path` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:21:1
|
LL | #![path = "3800"]
| ^^^^^^^^^^^^^^^^^
...
LL | mod inline {
| ------ the inner attribute doesn't annotate this module
|
help: perhaps you meant to use an outer attribute
|
LL - #![path = "3800"]
LL + #[path = "3800"]
|
error[E0518]: attribute should be applied to function or closure
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:42:17
|

View File

@ -27,19 +27,6 @@ note: attribute also specified here
LL | #[macro_use]
| ^^^^^^^^^^^^
error: unused attribute
--> $DIR/unused-attr-duplicate.rs:47:1
|
LL | #[path = "bar.rs"]
| ^^^^^^^^^^^^^^^^^^ help: remove this attribute
|
note: attribute also specified here
--> $DIR/unused-attr-duplicate.rs:46:1
|
LL | #[path = "auxiliary/lint_unused_extern_crate.rs"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
error: unused attribute
--> $DIR/unused-attr-duplicate.rs:55:1
|
@ -153,6 +140,19 @@ note: attribute also specified here
LL | #[macro_export]
| ^^^^^^^^^^^^^^^
error: unused attribute
--> $DIR/unused-attr-duplicate.rs:47:1
|
LL | #[path = "bar.rs"]
| ^^^^^^^^^^^^^^^^^^ help: remove this attribute
|
note: attribute also specified here
--> $DIR/unused-attr-duplicate.rs:46:1
|
LL | #[path = "auxiliary/lint_unused_extern_crate.rs"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
error: unused attribute
--> $DIR/unused-attr-duplicate.rs:53:1
|

View File

@ -10,17 +10,17 @@ note: the lint level is defined here
LL | #![deny(unused_attributes)]
| ^^^^^^^^^^^^^^^^^
error: `#[path]` only has an effect on modules
--> $DIR/unused-attr-macro-rules.rs:8:1
|
LL | #[path="foo"]
| ^^^^^^^^^^^^^
error: crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]`
--> $DIR/unused-attr-macro-rules.rs:9:1
|
LL | #[recursion_limit="1"]
| ^^^^^^^^^^^^^^^^^^^^^^
error: `#[path]` only has an effect on modules
--> $DIR/unused-attr-macro-rules.rs:8:1
|
LL | #[path="foo"]
| ^^^^^^^^^^^^^
error: aborting due to 3 previous errors

View File

@ -5,5 +5,6 @@ fn main() {
const {
#![path = foo!()]
//~^ ERROR: cannot find macro `foo` in this scope
//~| ERROR malformed `path` attribute input
}
}

View File

@ -4,5 +4,15 @@ error: cannot find macro `foo` in this scope
LL | #![path = foo!()]
| ^^^
error: aborting due to 1 previous error
error[E0539]: malformed `path` attribute input
--> $DIR/path-attr-in-const-block.rs:6:9
|
LL | #![path = foo!()]
| ^^^^^^^^^^------^
| | |
| | expected a string literal here
| help: must be of the form: `#[path = "file"]`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0539`.