-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Port #[custom_mir(..)]
to the new attribute system
#145206
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
scrabsha
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
scrabsha:push-uxovoqzrxnlx
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.
+313
−90
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
136 changes: 136 additions & 0 deletions
136
compiler/rustc_attr_parsing/src/attributes/prototype.rs
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,136 @@ | ||
//! Attributes that are only used on function prototypes. | ||
|
||
use rustc_feature::{AttributeTemplate, template}; | ||
use rustc_hir::attrs::{AttributeKind, MirDialect, MirPhase}; | ||
use rustc_span::{Span, Symbol, sym}; | ||
|
||
use super::{AttributeOrder, OnDuplicate}; | ||
use crate::attributes::SingleAttributeParser; | ||
use crate::context::{AcceptContext, Stage}; | ||
use crate::parser::ArgParser; | ||
|
||
pub(crate) struct CustomMirParser; | ||
|
||
impl<S: Stage> SingleAttributeParser<S> for CustomMirParser { | ||
const PATH: &[rustc_span::Symbol] = &[sym::custom_mir]; | ||
|
||
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost; | ||
|
||
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error; | ||
|
||
const TEMPLATE: AttributeTemplate = template!(List: &[r#"dialect = "...", phase = "...""#]); | ||
|
||
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> { | ||
let Some(list) = args.list() else { | ||
cx.expected_list(cx.attr_span); | ||
return None; | ||
}; | ||
|
||
let mut dialect = None; | ||
let mut phase = None; | ||
let mut failed = false; | ||
|
||
for item in list.mixed() { | ||
let Some(meta_item) = item.meta_item() else { | ||
cx.expected_name_value(item.span(), None); | ||
failed = true; | ||
break; | ||
}; | ||
|
||
if let Some(arg) = meta_item.word_is(sym::dialect) { | ||
extract_value(cx, sym::dialect, arg, meta_item.span(), &mut dialect, &mut failed); | ||
} else if let Some(arg) = meta_item.word_is(sym::phase) { | ||
extract_value(cx, sym::phase, arg, meta_item.span(), &mut phase, &mut failed); | ||
} else if let Some(word) = meta_item.path().word() { | ||
let word = word.to_string(); | ||
cx.unknown_key(meta_item.span(), word, &["dialect", "phase"]); | ||
failed = true; | ||
} else { | ||
cx.expected_name_value(meta_item.span(), None); | ||
failed = true; | ||
}; | ||
} | ||
|
||
let dialect = parse_dialect(cx, dialect, &mut failed); | ||
let phase = parse_phase(cx, phase, &mut failed); | ||
|
||
if failed { | ||
return None; | ||
} | ||
|
||
Some(AttributeKind::CustomMir(dialect, phase, cx.attr_span)) | ||
} | ||
} | ||
|
||
fn extract_value<S: Stage>( | ||
cx: &mut AcceptContext<'_, '_, S>, | ||
key: Symbol, | ||
arg: &ArgParser<'_>, | ||
span: Span, | ||
out_val: &mut Option<(Symbol, Span)>, | ||
failed: &mut bool, | ||
) { | ||
if out_val.is_some() { | ||
cx.duplicate_key(span, key); | ||
*failed = true; | ||
return; | ||
} | ||
|
||
let Some(val) = arg.name_value() else { | ||
cx.expected_single_argument(arg.span().unwrap_or(span)); | ||
*failed = true; | ||
return; | ||
}; | ||
|
||
let Some(value_sym) = val.value_as_str() else { | ||
cx.expected_string_literal(val.value_span, Some(val.value_as_lit())); | ||
*failed = true; | ||
return; | ||
}; | ||
|
||
*out_val = Some((value_sym, val.value_span)); | ||
} | ||
|
||
fn parse_dialect<S: Stage>( | ||
cx: &mut AcceptContext<'_, '_, S>, | ||
dialect: Option<(Symbol, Span)>, | ||
failed: &mut bool, | ||
) -> Option<(MirDialect, Span)> { | ||
let (dialect, span) = dialect?; | ||
|
||
let dialect = match dialect { | ||
sym::analysis => MirDialect::Analysis, | ||
sym::built => MirDialect::Built, | ||
sym::runtime => MirDialect::Runtime, | ||
|
||
_ => { | ||
cx.expected_specific_argument(span, vec!["analysis", "built", "runtime"]); | ||
*failed = true; | ||
return None; | ||
} | ||
}; | ||
|
||
Some((dialect, span)) | ||
} | ||
|
||
fn parse_phase<S: Stage>( | ||
cx: &mut AcceptContext<'_, '_, S>, | ||
phase: Option<(Symbol, Span)>, | ||
failed: &mut bool, | ||
) -> Option<(MirPhase, Span)> { | ||
let (phase, span) = phase?; | ||
|
||
let phase = match phase { | ||
sym::initial => MirPhase::Initial, | ||
sym::post_cleanup => MirPhase::PostCleanup, | ||
sym::optimized => MirPhase::Optimized, | ||
|
||
_ => { | ||
cx.expected_specific_argument(span, vec!["initial", "post-cleanup", "optimized"]); | ||
*failed = true; | ||
return None; | ||
} | ||
}; | ||
|
||
Some((phase, span)) | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.