-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add test to reproduce #137687 and fix it by converting #[crate_name]
to a new-style attribute parser
#137729
base: master
Are you sure you want to change the base?
add test to reproduce #137687 and fix it by converting #[crate_name]
to a new-style attribute parser
#137729
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use rustc_attr_data_structures::AttributeKind; | ||
use rustc_span::{Span, Symbol, sym}; | ||
|
||
use super::SingleAttributeParser; | ||
use crate::context::AcceptContext; | ||
use crate::parser::ArgParser; | ||
use crate::session_diagnostics::{ExpectedNameValue, IncorrectMetaItem, UnusedMultiple}; | ||
|
||
pub(crate) struct CratenameParser; | ||
|
||
impl SingleAttributeParser for CratenameParser { | ||
const PATH: &'static [Symbol] = &[sym::crate_name]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: You generally no longer need to explicitly state |
||
|
||
fn on_duplicate(cx: &AcceptContext<'_>, first_span: Span) { | ||
// FIXME(jdonszelmann): better duplicate reporting (WIP) | ||
cx.emit_err(UnusedMultiple { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicates are warnings on master, right? So this is a breaking change? |
||
this: cx.attr_span, | ||
other: first_span, | ||
name: sym::crate_name, | ||
}); | ||
} | ||
|
||
fn convert(cx: &AcceptContext<'_>, args: &ArgParser<'_>) -> Option<AttributeKind> { | ||
if let ArgParser::NameValue(n) = args { | ||
if let Some(name) = n.value_as_str() { | ||
Some(AttributeKind::CrateName { | ||
name, | ||
name_span: n.value_span, | ||
style: cx.attr_style, | ||
}) | ||
} else { | ||
cx.emit_err(IncorrectMetaItem { span: cx.attr_span, suggestion: None }); | ||
|
||
None | ||
} | ||
} else { | ||
cx.emit_err(ExpectedNameValue { span: cx.attr_span, name: sym::crate_name }); | ||
|
||
None | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// issue: rust-lang/rust#122001 | ||
// Ensure we reject macro calls inside `#![crate_name]` as their result wouldn't get honored anyway. | ||
|
||
#![crate_name = concat!("my", "crate")] //~ ERROR malformed `crate_name` attribute input | ||
#![crate_name = concat!("my", "crate")] //~ ERROR expected a quoted string literal | ||
|
||
fn main() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could theoretically use
Spanned
but it doesn't necessarily lead to more readable code (and I don't remember if it's compatible with translatable diagnostics).