-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Introduce #[diagnostic::on_move(message)] #150935
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
rperier
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
rperier:provide_diagnostic_on_move_for_smart_pointers
base: main
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.
+690
−18
Open
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| use rustc_feature::{AttributeTemplate, template}; | ||
| use rustc_hir::attrs::{AttributeKind, OnMoveAttrArg, OnMoveAttribute}; | ||
| use rustc_hir::lints::AttributeLintKind; | ||
| use rustc_parse_format::{ParseMode, Parser, Piece, Position}; | ||
| use rustc_session::lint::builtin::{ | ||
| MALFORMED_DIAGNOSTIC_ATTRIBUTES, MALFORMED_DIAGNOSTIC_FORMAT_LITERALS, | ||
| }; | ||
| use rustc_span::{InnerSpan, Span, Symbol, kw, sym}; | ||
| use thin_vec::ThinVec; | ||
|
|
||
| use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser}; | ||
| use crate::context::{AcceptContext, Stage}; | ||
| use crate::parser::ArgParser; | ||
| use crate::target_checking::{ALL_TARGETS, AllowedTargets}; | ||
|
|
||
| pub(crate) struct OnMoveParser; | ||
|
|
||
| impl<S: Stage> SingleAttributeParser<S> for OnMoveParser { | ||
| const PATH: &[Symbol] = &[sym::diagnostic, sym::on_move]; | ||
| const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost; | ||
| const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn; | ||
| const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); // Check in check_attr. | ||
| const TEMPLATE: AttributeTemplate = template!(List: &["message", "label"]); | ||
|
|
||
| fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> { | ||
| let get_format_ranges = |cx: &mut AcceptContext<'_, '_, S>, | ||
| symbol: &Symbol, | ||
| span: Span| | ||
| -> ThinVec<(usize, usize)> { | ||
| let mut parser = Parser::new(symbol.as_str(), None, None, false, ParseMode::Diagnostic); | ||
| let pieces: Vec<_> = parser.by_ref().collect(); | ||
| let mut spans = ThinVec::new(); | ||
|
|
||
| for piece in pieces { | ||
| match piece { | ||
| Piece::NextArgument(arg) => match arg.position { | ||
| Position::ArgumentNamed(name) if name == kw::SelfUpper.as_str() => { | ||
| spans.push((arg.position_span.start - 2, arg.position_span.end)); | ||
| } | ||
| Position::ArgumentNamed(name) => { | ||
| let span = span.from_inner(InnerSpan { | ||
| start: arg.position_span.start, | ||
| end: arg.position_span.end, | ||
| }); | ||
| cx.emit_lint( | ||
| MALFORMED_DIAGNOSTIC_FORMAT_LITERALS, | ||
| AttributeLintKind::OnMoveMalformedFormatLiterals { | ||
| name: Symbol::intern(name), | ||
| }, | ||
| span, | ||
| ) | ||
| } | ||
| _ => {} | ||
| }, | ||
| _ => continue, | ||
| } | ||
| } | ||
| spans | ||
| }; | ||
| let Some(list) = args.list() else { | ||
| cx.expected_list(cx.attr_span, args); | ||
| return None; | ||
| }; | ||
| let mut message = None; | ||
| let mut label = None; | ||
|
|
||
| for item in list.mixed() { | ||
| let Some(item) = item.meta_item() else { | ||
| cx.expected_specific_argument(item.span(), &[sym::message, sym::label]); | ||
| return None; | ||
| }; | ||
|
|
||
| let Some(name_value) = item.args().name_value() else { | ||
| cx.expected_name_value(cx.attr_span, item.path().word_sym()); | ||
| return None; | ||
| }; | ||
|
|
||
| let Some(value) = name_value.value_as_str() else { | ||
| cx.expected_string_literal(name_value.value_span, None); | ||
| return None; | ||
| }; | ||
|
|
||
| let value_span = name_value.value_span; | ||
| if item.path().word_is(sym::message) && message.is_none() { | ||
| let spans = get_format_ranges(cx, &value, value_span); | ||
| message = Some(OnMoveAttrArg::new(value, spans)); | ||
| continue; | ||
| } else if item.path().word_is(sym::label) && label.is_none() { | ||
| let spans = get_format_ranges(cx, &value, value_span); | ||
| label = Some(OnMoveAttrArg::new(value, spans)); | ||
| continue; | ||
| } | ||
|
|
||
| cx.emit_lint( | ||
| MALFORMED_DIAGNOSTIC_ATTRIBUTES, | ||
| AttributeLintKind::OnMoveMalformedAttr, | ||
| item.span(), | ||
| ) | ||
| } | ||
| Some(AttributeKind::OnMove(Box::new(OnMoveAttribute { | ||
| span: cx.attr_span, | ||
| message, | ||
| label, | ||
| }))) | ||
| } | ||
| } |
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
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
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.
I plan on moving a fair bit of the format and string stuff that on_unimplemented uses into rustc_hir; you should be able to reuse some of that in this PR :)
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.
Sure, I will do