-
Notifications
You must be signed in to change notification settings - Fork 13.6k
add test to reproduce #137687 and fix it by converting #[crate_name]
to a new-style attribute parser
#137729
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
Open
jdonszelmann
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
jdonszelmann:fix-137687
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+405
−206
Open
add test to reproduce #137687 and fix it by converting #[crate_name]
to a new-style attribute parser
#137729
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use rustc_feature::{AttributeTemplate, template}; | ||
use rustc_hir::attrs::AttributeKind; | ||
use rustc_span::{Symbol, sym}; | ||
|
||
use super::{AttributeOrder, OnDuplicate, SingleAttributeParser}; | ||
use crate::context::{AcceptContext, Stage}; | ||
use crate::parser::ArgParser; | ||
|
||
pub(crate) struct CrateNameParser; | ||
|
||
impl<S: Stage> SingleAttributeParser<S> for CrateNameParser { | ||
const PATH: &[Symbol] = &[sym::crate_name]; | ||
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost; | ||
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError; | ||
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name"); | ||
|
||
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> { | ||
let ArgParser::NameValue(n) = args else { | ||
cx.expected_name_value(cx.attr_span, None); | ||
return None; | ||
}; | ||
|
||
let Some(name) = n.value_as_str() else { | ||
cx.expected_string_literal(n.value_span, Some(n.value_as_lit())); | ||
return None; | ||
}; | ||
|
||
Some(AttributeKind::CrateName { name, name_span: n.value_span, style: cx.attr_style }) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,46 @@ | ||
use rustc_errors::{DiagArgValue, LintEmitter}; | ||
use rustc_errors::{DiagArgValue, LintEmitter, SimpleLintEmitter}; | ||
use rustc_hir::HirId; | ||
use rustc_hir::lints::{AttributeLint, AttributeLintKind}; | ||
use rustc_session::lint::Lint; | ||
|
||
use crate::session_diagnostics; | ||
|
||
pub fn emit_attribute_lint<L: LintEmitter>(lint: &AttributeLint<HirId>, lint_emitter: L) { | ||
let AttributeLint { id, span, kind } = lint; | ||
pub(crate) fn lint_name(kind: &AttributeLintKind) -> &'static Lint { | ||
use rustc_session::lint::builtin::*; | ||
match kind { | ||
AttributeLintKind::UnusedDuplicate { .. } => UNUSED_ATTRIBUTES, | ||
AttributeLintKind::IllFormedAttributeInput { .. } => ILL_FORMED_ATTRIBUTE_INPUT, | ||
AttributeLintKind::EmptyAttribute { .. } => UNUSED_ATTRIBUTES, | ||
} | ||
} | ||
|
||
pub fn emit_attribute_lint_kind<L: SimpleLintEmitter>(kind: &AttributeLintKind, lint_emitter: L) { | ||
match kind { | ||
&AttributeLintKind::UnusedDuplicate { this, other, warning } => lint_emitter | ||
.emit_node_span_lint( | ||
rustc_session::lint::builtin::UNUSED_ATTRIBUTES, | ||
*id, | ||
*span, | ||
session_diagnostics::UnusedDuplicate { this, other, warning }, | ||
), | ||
&AttributeLintKind::UnusedDuplicate { this, other, warning } => { | ||
lint_emitter.emit(session_diagnostics::UnusedDuplicate { this, other, warning }) | ||
} | ||
AttributeLintKind::IllFormedAttributeInput { suggestions } => { | ||
lint_emitter.emit_node_span_lint( | ||
rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT, | ||
*id, | ||
*span, | ||
session_diagnostics::IllFormedAttributeInput { | ||
num_suggestions: suggestions.len(), | ||
suggestions: DiagArgValue::StrListSepByAnd( | ||
suggestions.into_iter().map(|s| format!("`{s}`").into()).collect(), | ||
), | ||
}, | ||
); | ||
lint_emitter.emit(session_diagnostics::IllFormedAttributeInput { | ||
num_suggestions: suggestions.len(), | ||
suggestions: DiagArgValue::StrListSepByAnd( | ||
suggestions.into_iter().map(|s| format!("`{s}`").into()).collect(), | ||
), | ||
}); | ||
} | ||
AttributeLintKind::EmptyAttribute { first_span } => { | ||
lint_emitter.emit(session_diagnostics::EmptyAttributeList { attr_span: *first_span }) | ||
} | ||
AttributeLintKind::EmptyAttribute { first_span } => lint_emitter.emit_node_span_lint( | ||
rustc_session::lint::builtin::UNUSED_ATTRIBUTES, | ||
*id, | ||
*first_span, | ||
session_diagnostics::EmptyAttributeList { attr_span: *first_span }, | ||
), | ||
} | ||
} | ||
|
||
pub fn emit_attribute_lint<L: LintEmitter<ID = HirId>>( | ||
lint: &AttributeLint<HirId>, | ||
lint_emitter: L, | ||
) { | ||
let AttributeLint { id, span, kind } = lint; | ||
|
||
let lint_name = lint_name(&lint.kind); | ||
|
||
let emit = lint_emitter.prepare_node_span_lint(lint_name, *id, *span); | ||
emit_attribute_lint_kind(kind, emit); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
(Since I lack a bigger picture of the new attr parsing APIs) Is it desirable that
parse_limited{,_should_emit}
default to lint buffering by default? Shouldn't the caller decide that (via a callback)?For example, what happens if someone calls one of these functions in a later compiler stage when all buffered lints "have been emitted already"? Can that even happen or are buffered lints only emitted when the session is destroyed (I don't know much about this logic of
buffer_lint
)?