rustc_attr_parsing/attributes/
path.rs1use rustc_feature::{AttributeTemplate, template};
2use rustc_hir::Target;
3use rustc_hir::attrs::AttributeKind;
4use rustc_span::{Symbol, sym};
5
6use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
7use crate::context::MaybeWarn::{Allow, Error};
8use crate::context::{AcceptContext, AllowedTargets, Stage};
9use crate::parser::ArgParser;
10pub(crate) struct PathParser;
11
12impl<S: Stage> SingleAttributeParser<S> for PathParser {
13 const PATH: &[Symbol] = &[sym::path];
14 const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
15 const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
16 const ALLOWED_TARGETS: AllowedTargets =
17 AllowedTargets::AllowListWarnRest(&[Allow(Target::Mod), Error(Target::Crate)]);
18 const TEMPLATE: AttributeTemplate = template!(
19 NameValueStr: "file",
20 "https://doc.rust-lang.org/reference/items/modules.html#the-path-attribute"
21 );
22
23 fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
24 let Some(nv) = args.name_value() else {
25 cx.expected_name_value(cx.attr_span, None);
26 return None;
27 };
28 let Some(path) = nv.value_as_str() else {
29 cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
30 return None;
31 };
32
33 Some(AttributeKind::Path(path, cx.attr_span))
34 }
35}