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

+1-2
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

-4
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

-2
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

-99
This file was deleted.

compiler/rustc_parse/src/parser/expr.rs

+19-1
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

+1
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

+3-3
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

+8-8
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

+4-4
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

+11-3
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, }
+49-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,59 @@
1-
error: expected one of `!`, `(`, `+`, `,`, `::`, or `<`, found `<eof>`
2-
--> $DIR/offset-of-arg-count.rs:6:16
1+
error: unexpected end of macro invocation
2+
--> $DIR/offset-of-arg-count.rs:6:34
33
|
44
LL | offset_of!(NotEnoughArguments);
5-
| ^^^^^^^^^^^^^^^^^^ expected one of `!`, `(`, `+`, `,`, `::`, or `<`
5+
| ^ missing tokens in macro arguments
6+
|
7+
note: while trying to match `,`
8+
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
69

7-
error: expected 2 arguments
8-
--> $DIR/offset-of-arg-count.rs:7:5
10+
error: unexpected end of macro invocation
11+
--> $DIR/offset-of-arg-count.rs:7:45
912
|
1013
LL | offset_of!(NotEnoughArgumentsWithAComma, );
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
| ^ missing tokens in macro arguments
15+
|
16+
note: while trying to match meta-variable `$fields:tt`
17+
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
1218

13-
error: expected 2 arguments
14-
--> $DIR/offset-of-arg-count.rs:8:5
19+
error: no rules expected the token `too`
20+
--> $DIR/offset-of-arg-count.rs:8:34
1521
|
1622
LL | offset_of!(Container, field, too many arguments);
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
| ^^^ no rules expected this token in macro call
24+
|
25+
= note: while trying to match sequence end
26+
27+
error: unexpected end of macro invocation
28+
--> $DIR/offset-of-arg-count.rs:11:21
29+
|
30+
LL | offset_of!(S, f.);
31+
| ^ missing tokens in macro arguments
32+
|
33+
note: while trying to match meta-variable `$fields:tt`
34+
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
35+
36+
error: expected identifier, found `,`
37+
--> $DIR/offset-of-arg-count.rs:12:21
38+
|
39+
LL | offset_of!(S, f.,);
40+
| ^ expected identifier
41+
42+
error: no rules expected the token `..`
43+
--> $DIR/offset-of-arg-count.rs:13:20
44+
|
45+
LL | offset_of!(S, f..);
46+
| ^^ no rules expected this token in macro call
47+
|
48+
= note: while trying to match sequence start
49+
50+
error: no rules expected the token `..`
51+
--> $DIR/offset-of-arg-count.rs:14:20
52+
|
53+
LL | offset_of!(S, f..,);
54+
| ^^ no rules expected this token in macro call
55+
|
56+
= note: while trying to match sequence start
1857

19-
error: aborting due to 3 previous errors
58+
error: aborting due to 7 previous errors
2059

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// For the exposed macro we already test these errors in the other files,
2+
// but this test helps to make sure the builtin construct also errors.
3+
// This has the same examples as offset-of-arg-count.rs
4+
5+
6+
7+
fn main() {
8+
builtin # offset_of(NotEnoughArguments); //~ ERROR expected one of
9+
}
10+
fn t1() {
11+
// Already errored upon at the macro level. Yielding an error would require
12+
// extra effort.
13+
builtin # offset_of(NotEnoughArgumentsWithAComma, );
14+
}
15+
fn t2() {
16+
builtin # offset_of(Container, field, too many arguments); //~ ERROR expected identifier, found
17+
//~| ERROR found `,`
18+
//~| ERROR found `many`
19+
//~| ERROR found `arguments`
20+
}
21+
fn t3() {
22+
builtin # offset_of(S, f); // compiles fine
23+
}
24+
fn t4() {
25+
// Already errored upon at the macro level. Yielding an error would require
26+
// extra effort.
27+
builtin # offset_of(S, f);
28+
}
29+
fn t5() {
30+
builtin # offset_of(S, f.); //~ ERROR expected identifier
31+
}
32+
fn t6() {
33+
builtin # offset_of(S, f.,); //~ ERROR expected identifier
34+
}
35+
fn t7() {
36+
builtin # offset_of(S, f..); //~ ERROR expected one of
37+
}
38+
fn t8() {
39+
// Already errored upon at the macro level. Yielding an error would require
40+
// extra effort.
41+
builtin # offset_of(S, f..,);
42+
}
43+
44+
struct S { f: u8, }

0 commit comments

Comments
 (0)