-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add offset_of! macro (RFC 3308) #106934
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
Merged
Merged
Add offset_of! macro (RFC 3308) #106934
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
511e457
offset_of
beepster4096 61f23e0
intern offsetof fields
beepster4096 b95852b
test improvements
beepster4096 b92c2f7
fix incorrect param env in dead code lint
beepster4096 631ea7c
use P<[Ident]> instead of Vec<Ident>
beepster4096 2bcb018
fmt
beepster4096 f92294f
bless
beepster4096 3206960
minor tweaks
beepster4096 a642563
major test improvements
beepster4096 99abe44
rustfmt fmt
beepster4096 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use rustc_ast as ast; | ||
use rustc_ast::ptr::P; | ||
use rustc_ast::token; | ||
use rustc_ast::tokenstream::TokenStream; | ||
use rustc_errors::PResult; | ||
use rustc_expand::base::{self, *}; | ||
use rustc_macros::Diagnostic; | ||
use rustc_parse::parser::Parser; | ||
use rustc_span::{symbol::Ident, Span}; | ||
|
||
#[derive(Diagnostic)] | ||
#[diag(builtin_macros_offset_of_expected_field)] | ||
struct ExpectedField { | ||
#[primary_span] | ||
span: Span, | ||
} | ||
|
||
#[derive(Diagnostic)] | ||
#[diag(builtin_macros_offset_of_expected_two_args)] | ||
struct ExpectedTwoArgs { | ||
#[primary_span] | ||
span: Span, | ||
} | ||
|
||
fn parse_field<'a>(cx: &ExtCtxt<'a>, p: &mut Parser<'a>) -> PResult<'a, Ident> { | ||
let token = p.token.uninterpolate(); | ||
let field = match token.kind { | ||
token::Ident(name, _) => Ident::new(name, token.span), | ||
token::Literal(token::Lit { kind: token::Integer, symbol, suffix: None }) => { | ||
Ident::new(symbol, token.span) | ||
} | ||
_ => return Err(cx.create_err(ExpectedField { span: p.token.span })), | ||
}; | ||
|
||
p.bump(); | ||
|
||
Ok(field) | ||
} | ||
|
||
fn parse_args<'a>( | ||
cx: &mut ExtCtxt<'a>, | ||
sp: Span, | ||
tts: TokenStream, | ||
) -> PResult<'a, (P<ast::Ty>, P<[Ident]>)> { | ||
let mut p = cx.new_parser_from_tts(tts); | ||
|
||
let container = p.parse_ty()?; | ||
|
||
p.expect(&token::Comma)?; | ||
|
||
if p.eat(&token::Eof) { | ||
return Err(cx.create_err(ExpectedTwoArgs { span: sp })); | ||
} | ||
|
||
let mut fields = Vec::new(); | ||
|
||
loop { | ||
let field = parse_field(cx, &mut p)?; | ||
fields.push(field); | ||
|
||
if p.eat(&token::Dot) { | ||
continue; | ||
} | ||
|
||
p.eat(&token::Comma); | ||
|
||
if !p.eat(&token::Eof) { | ||
return Err(cx.create_err(ExpectedTwoArgs { span: sp })); | ||
} | ||
|
||
break; | ||
} | ||
|
||
Ok((container, fields.into())) | ||
} | ||
|
||
pub fn expand_offset_of<'cx>( | ||
cx: &'cx mut ExtCtxt<'_>, | ||
sp: Span, | ||
tts: TokenStream, | ||
) -> Box<dyn base::MacResult + 'cx> { | ||
match parse_args(cx, sp, tts) { | ||
Ok((container, fields)) => { | ||
let expr = P(ast::Expr { | ||
id: ast::DUMMY_NODE_ID, | ||
kind: ast::ExprKind::OffsetOf(container, fields), | ||
span: sp, | ||
attrs: ast::AttrVec::new(), | ||
tokens: None, | ||
}); | ||
|
||
MacEager::expr(expr) | ||
} | ||
Err(mut err) => { | ||
err.emit(); | ||
DummyResult::any(sp) | ||
} | ||
} | ||
} |
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.
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.