Skip to content

Commit 5eb29c7

Browse files
committed
Migrate offset_of from a macro to builtin # syntax
1 parent 59ecbd2 commit 5eb29c7

File tree

15 files changed

+213
-136
lines changed

15 files changed

+213
-136
lines changed

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,7 @@ impl<'a> State<'a> {
556556
self.pclose();
557557
}
558558
ast::ExprKind::OffsetOf(container, fields) => {
559-
// FIXME: This should have its own syntax, distinct from a macro invocation.
560-
self.word("offset_of!");
559+
self.word("builtin # offset_of");
561560
self.popen();
562561
self.rbox(0, Inconsistent);
563562
self.print_type(container);

compiler/rustc_builtin_macros/messages.ftl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,6 @@ builtin_macros_format_pos_mismatch = {$n} positional {$n ->
150150
*[more] arguments
151151
} in format string, but {$desc}
152152
153-
builtin_macros_offset_of_expected_field = expected field
154-
155-
builtin_macros_offset_of_expected_two_args = expected 2 arguments
156-
157153
builtin_macros_test_case_non_item = `#[test_case]` attribute is only allowed on items
158154
159155
builtin_macros_test_bad_fn = {$kind} functions cannot be used for tests

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ mod format;
4444
mod format_foreign;
4545
mod global_allocator;
4646
mod log_syntax;
47-
mod offset_of;
4847
mod source_util;
4948
mod test;
5049
mod trace_macros;
@@ -92,7 +91,6 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
9291
line: source_util::expand_line,
9392
log_syntax: log_syntax::expand_log_syntax,
9493
module_path: source_util::expand_mod,
95-
offset_of: offset_of::expand_offset_of,
9694
option_env: env::expand_option_env,
9795
core_panic: edition_panic::expand_panic,
9896
std_panic: edition_panic::expand_panic,

compiler/rustc_builtin_macros/src/offset_of.rs

Lines changed: 0 additions & 99 deletions
This file was deleted.

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1759,7 +1759,11 @@ impl<'a> Parser<'a> {
17591759

17601760
/// Parse `builtin # ident(args,*)`.
17611761
fn parse_expr_builtin(&mut self) -> PResult<'a, P<Expr>> {
1762-
self.parse_builtin(|_this, _lo, _ident| {
1762+
self.parse_builtin(|this, lo, ident| {
1763+
if ident.name == sym::offset_of {
1764+
return Ok(Some(this.parse_expr_offset_of(lo)?));
1765+
}
1766+
17631767
Ok(None)
17641768
})
17651769
}
@@ -1793,6 +1797,20 @@ impl<'a> Parser<'a> {
17931797
ret
17941798
}
17951799

1800+
pub(crate) fn parse_expr_offset_of(&mut self, lo: Span) -> PResult<'a, P<Expr>> {
1801+
let container = self.parse_ty()?;
1802+
self.expect(&TokenKind::Comma)?;
1803+
1804+
let seq_sep = SeqSep { sep: Some(token::Dot), trailing_sep_allowed: false };
1805+
let (fields, _trailing, _recovered) = self.parse_seq_to_before_end(
1806+
&TokenKind::CloseDelim(Delimiter::Parenthesis),
1807+
seq_sep,
1808+
Parser::parse_field_name,
1809+
)?;
1810+
let span = lo.to(self.token.span);
1811+
Ok(self.mk_expr(span, ExprKind::OffsetOf(container, fields.to_vec().into())))
1812+
}
1813+
17961814
/// Returns a string literal if the next token is a string literal.
17971815
/// In case of error returns `Some(lit)` if the next token is a literal with a wrong kind,
17981816
/// and returns `None` if the next token is not literal at all.

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ symbols! {
441441
breakpoint,
442442
bridge,
443443
bswap,
444+
builtin_syntax,
444445
c_str,
445446
c_str_literals,
446447
c_unwind,

library/core/src/mem/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,9 +1315,9 @@ impl<T> SizedTypeProperties for T {}
13151315
///
13161316
/// assert_eq!(mem::offset_of!(NestedA, b.0), 0);
13171317
/// ```
1318-
#[unstable(feature = "offset_of", issue = "106655")]
1319-
#[rustc_builtin_macro]
13201318
#[cfg(not(bootstrap))]
1319+
#[unstable(feature = "offset_of", issue = "106655")]
1320+
#[allow_internal_unstable(builtin_syntax)]
13211321
pub macro offset_of($Container:ty, $($fields:tt).+ $(,)?) {
1322-
/* compiler built-in */
1322+
builtin # offset_of($Container, $($fields).+)
13231323
}

tests/mir-opt/const_prop/offset_of.concrete.ConstProp.diff

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@
2222

2323
bb0: {
2424
StorageLive(_1); // scope 0 at $DIR/offset_of.rs:+1:9: +1:10
25-
- _1 = OffsetOf(Alpha, [0]); // scope 0 at $DIR/offset_of.rs:+1:13: +1:33
26-
+ _1 = const 4_usize; // scope 0 at $DIR/offset_of.rs:+1:13: +1:33
25+
- _1 = OffsetOf(Alpha, [0]); // scope 0 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
26+
+ _1 = const 4_usize; // scope 0 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
2727
StorageLive(_2); // scope 1 at $DIR/offset_of.rs:+2:9: +2:10
28-
- _2 = OffsetOf(Alpha, [1]); // scope 1 at $DIR/offset_of.rs:+2:13: +2:33
29-
+ _2 = const 0_usize; // scope 1 at $DIR/offset_of.rs:+2:13: +2:33
28+
- _2 = OffsetOf(Alpha, [1]); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
29+
+ _2 = const 0_usize; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
3030
StorageLive(_3); // scope 2 at $DIR/offset_of.rs:+3:9: +3:11
31-
- _3 = OffsetOf(Alpha, [2, 0]); // scope 2 at $DIR/offset_of.rs:+3:14: +3:36
32-
+ _3 = const 2_usize; // scope 2 at $DIR/offset_of.rs:+3:14: +3:36
31+
- _3 = OffsetOf(Alpha, [2, 0]); // scope 2 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
32+
+ _3 = const 2_usize; // scope 2 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
3333
StorageLive(_4); // scope 3 at $DIR/offset_of.rs:+4:9: +4:11
34-
- _4 = OffsetOf(Alpha, [2, 1]); // scope 3 at $DIR/offset_of.rs:+4:14: +4:36
35-
+ _4 = const 3_usize; // scope 3 at $DIR/offset_of.rs:+4:14: +4:36
34+
- _4 = OffsetOf(Alpha, [2, 1]); // scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
35+
+ _4 = const 3_usize; // scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
3636
_0 = const (); // scope 0 at $DIR/offset_of.rs:+0:15: +5:2
3737
StorageDead(_4); // scope 3 at $DIR/offset_of.rs:+5:1: +5:2
3838
StorageDead(_3); // scope 2 at $DIR/offset_of.rs:+5:1: +5:2

tests/mir-opt/const_prop/offset_of.generic.ConstProp.diff

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222

2323
bb0: {
2424
StorageLive(_1); // scope 0 at $DIR/offset_of.rs:+1:9: +1:11
25-
_1 = OffsetOf(Gamma<T>, [0]); // scope 0 at $DIR/offset_of.rs:+1:14: +1:37
25+
_1 = OffsetOf(Gamma<T>, [0]); // scope 0 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
2626
StorageLive(_2); // scope 1 at $DIR/offset_of.rs:+2:9: +2:11
27-
_2 = OffsetOf(Gamma<T>, [1]); // scope 1 at $DIR/offset_of.rs:+2:14: +2:37
27+
_2 = OffsetOf(Gamma<T>, [1]); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
2828
StorageLive(_3); // scope 2 at $DIR/offset_of.rs:+3:9: +3:11
29-
_3 = OffsetOf(Delta<T>, [1]); // scope 2 at $DIR/offset_of.rs:+3:14: +3:37
29+
_3 = OffsetOf(Delta<T>, [1]); // scope 2 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
3030
StorageLive(_4); // scope 3 at $DIR/offset_of.rs:+4:9: +4:11
31-
_4 = OffsetOf(Delta<T>, [2]); // scope 3 at $DIR/offset_of.rs:+4:14: +4:37
31+
_4 = OffsetOf(Delta<T>, [2]); // scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
3232
_0 = const (); // scope 0 at $DIR/offset_of.rs:+0:17: +5:2
3333
StorageDead(_4); // scope 3 at $DIR/offset_of.rs:+5:1: +5:2
3434
StorageDead(_3); // scope 2 at $DIR/offset_of.rs:+5:1: +5:2

tests/ui/offset-of/offset-of-arg-count.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
use std::mem::offset_of;
44

55
fn main() {
6-
offset_of!(NotEnoughArguments); //~ ERROR expected one of
7-
offset_of!(NotEnoughArgumentsWithAComma, ); //~ ERROR expected 2 arguments
8-
offset_of!(Container, field, too many arguments); //~ ERROR expected 2 arguments
6+
offset_of!(NotEnoughArguments); //~ ERROR unexpected end of macro invocation
7+
offset_of!(NotEnoughArgumentsWithAComma, ); //~ ERROR unexpected end of macro invocation
8+
offset_of!(Container, field, too many arguments); //~ ERROR no rules expected the token `too`
9+
offset_of!(S, f); // compiles fine
10+
offset_of!(S, f,); // also compiles fine
11+
offset_of!(S, f.); //~ ERROR unexpected end of macro invocation
12+
offset_of!(S, f.,); //~ ERROR expected identifier
13+
offset_of!(S, f..); //~ ERROR no rules expected the token
14+
offset_of!(S, f..,); //~ ERROR no rules expected the token
915
}
16+
17+
struct S { f: u8, }

0 commit comments

Comments
 (0)