Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dbd090c

Browse files
authoredMay 9, 2023
Rollup merge of rust-lang#110694 - est31:builtin, r=petrochenkov
Implement builtin # syntax and use it for offset_of!(...) Add `builtin #` syntax to the parser, as well as a generic infrastructure to support both item and expression position builtin syntaxes. The PR also uses this infrastructure for the implementation of the `offset_of!` macro, added by rust-lang#106934. cc `@petrochenkov` `@DrMeepster` cc rust-lang#110680 `builtin #` tracking issue cc rust-lang#106655 `offset_of!` tracking issue
2 parents ff30b8c + 83b4df4 commit dbd090c

File tree

25 files changed

+333
-136
lines changed

25 files changed

+333
-136
lines changed
 

‎compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
603603
gate_all!(yeet_expr, "`do yeet` expression is experimental");
604604
gate_all!(dyn_star, "`dyn*` trait objects are experimental");
605605
gate_all!(const_closures, "const closures are experimental");
606+
gate_all!(builtin_syntax, "`builtin #` syntax is unstable");
606607

607608
if !visitor.features.negative_bounds {
608609
for &span in spans.get(&sym::negative_bounds).iter().copied().flatten() {

‎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_feature/src/active.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ declare_features! (
313313
(active, async_closure, "1.37.0", Some(62290), None),
314314
/// Allows async functions to be declared, implemented, and used in traits.
315315
(active, async_fn_in_trait, "1.66.0", Some(91611), None),
316+
/// Allows builtin # foo() syntax
317+
(active, builtin_syntax, "CURRENT_RUSTC_VERSION", Some(110680), None),
316318
/// Allows `c"foo"` literals.
317319
(active, c_str_literals, "CURRENT_RUSTC_VERSION", Some(105723), None),
318320
/// Treat `extern "C"` function as nounwind.

‎compiler/rustc_parse/messages.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ parse_invalid_literal_suffix_on_tuple_index = suffixes on a tuple index are inva
257257
.tuple_exception_line_2 = on proc macros, you'll want to use `syn::Index::from` or `proc_macro::Literal::*_unsuffixed` for code that will desugar to tuple field access
258258
.tuple_exception_line_3 = see issue #60210 <https://github.com/rust-lang/rust/issues/60210> for more information
259259
260+
parse_expected_builtin_ident = expected identifier after `builtin #`
261+
262+
parse_unknown_builtin_construct = unknown `builtin #` construct `{$name}`
263+
260264
parse_non_string_abi_literal = non-string ABI literal
261265
.suggestion = specify the ABI with a string literal
262266

‎compiler/rustc_parse/src/errors.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2644,3 +2644,18 @@ pub(crate) struct MalformedCfgAttr {
26442644
pub span: Span,
26452645
pub sugg: &'static str,
26462646
}
2647+
2648+
#[derive(Diagnostic)]
2649+
#[diag(parse_unknown_builtin_construct)]
2650+
pub(crate) struct UnknownBuiltinConstruct {
2651+
#[primary_span]
2652+
pub span: Span,
2653+
pub name: Symbol,
2654+
}
2655+
2656+
#[derive(Diagnostic)]
2657+
#[diag(parse_expected_builtin_ident)]
2658+
pub(crate) struct ExpectedBuiltinIdent {
2659+
#[primary_span]
2660+
pub span: Span,
2661+
}

‎compiler/rustc_parse/src/parser/expr.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,8 @@ impl<'a> Parser<'a> {
13001300
})
13011301
} else if self.check(&token::OpenDelim(Delimiter::Bracket)) {
13021302
self.parse_expr_array_or_repeat(Delimiter::Bracket)
1303+
} else if self.is_builtin() {
1304+
self.parse_expr_builtin()
13031305
} else if self.check_path() {
13041306
self.parse_expr_path_start()
13051307
} else if self.check_keyword(kw::Move)
@@ -1766,6 +1768,61 @@ impl<'a> Parser<'a> {
17661768
self.maybe_recover_from_bad_qpath(expr)
17671769
}
17681770

1771+
/// Parse `builtin # ident(args,*)`.
1772+
fn parse_expr_builtin(&mut self) -> PResult<'a, P<Expr>> {
1773+
self.parse_builtin(|this, lo, ident| {
1774+
if ident.name == sym::offset_of {
1775+
return Ok(Some(this.parse_expr_offset_of(lo)?));
1776+
}
1777+
1778+
Ok(None)
1779+
})
1780+
}
1781+
1782+
pub(crate) fn parse_builtin<T>(
1783+
&mut self,
1784+
parse: impl FnOnce(&mut Parser<'a>, Span, Ident) -> PResult<'a, Option<T>>,
1785+
) -> PResult<'a, T> {
1786+
let lo = self.token.span;
1787+
1788+
self.bump(); // `builtin`
1789+
self.bump(); // `#`
1790+
1791+
let Some((ident, false)) = self.token.ident() else {
1792+
let err = errors::ExpectedBuiltinIdent { span: self.token.span }
1793+
.into_diagnostic(&self.sess.span_diagnostic);
1794+
return Err(err);
1795+
};
1796+
self.sess.gated_spans.gate(sym::builtin_syntax, ident.span);
1797+
self.bump();
1798+
1799+
self.expect(&TokenKind::OpenDelim(Delimiter::Parenthesis))?;
1800+
let ret = if let Some(res) = parse(self, lo, ident)? {
1801+
Ok(res)
1802+
} else {
1803+
let err = errors::UnknownBuiltinConstruct { span: lo.to(ident.span), name: ident.name }
1804+
.into_diagnostic(&self.sess.span_diagnostic);
1805+
return Err(err);
1806+
};
1807+
self.expect(&TokenKind::CloseDelim(Delimiter::Parenthesis))?;
1808+
1809+
ret
1810+
}
1811+
1812+
pub(crate) fn parse_expr_offset_of(&mut self, lo: Span) -> PResult<'a, P<Expr>> {
1813+
let container = self.parse_ty()?;
1814+
self.expect(&TokenKind::Comma)?;
1815+
1816+
let seq_sep = SeqSep { sep: Some(token::Dot), trailing_sep_allowed: false };
1817+
let (fields, _trailing, _recovered) = self.parse_seq_to_before_end(
1818+
&TokenKind::CloseDelim(Delimiter::Parenthesis),
1819+
seq_sep,
1820+
Parser::parse_field_name,
1821+
)?;
1822+
let span = lo.to(self.token.span);
1823+
Ok(self.mk_expr(span, ExprKind::OffsetOf(container, fields.to_vec().into())))
1824+
}
1825+
17691826
/// Returns a string literal if the next token is a string literal.
17701827
/// In case of error returns `Some(lit)` if the next token is a literal with a wrong kind,
17711828
/// and returns `None` if the next token is not literal at all.
@@ -2835,6 +2892,10 @@ impl<'a> Parser<'a> {
28352892
})
28362893
}
28372894

2895+
pub(crate) fn is_builtin(&self) -> bool {
2896+
self.token.is_keyword(kw::Builtin) && self.look_ahead(1, |t| *t == token::Pound)
2897+
}
2898+
28382899
/// Parses a `try {...}` expression (`try` token already eaten).
28392900
fn parse_try_block(&mut self, span_lo: Span) -> PResult<'a, P<Expr>> {
28402901
let (attrs, body) = self.parse_inner_attrs_and_block()?;

‎compiler/rustc_parse/src/parser/item.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ impl<'a> Parser<'a> {
265265
// UNION ITEM
266266
self.bump(); // `union`
267267
self.parse_item_union()?
268+
} else if self.is_builtin() {
269+
// BUILTIN# ITEM
270+
return self.parse_item_builtin();
268271
} else if self.eat_keyword(kw::Macro) {
269272
// MACROS 2.0 ITEM
270273
self.parse_item_decl_macro(lo)?
@@ -434,6 +437,11 @@ impl<'a> Parser<'a> {
434437
}
435438
}
436439

440+
fn parse_item_builtin(&mut self) -> PResult<'a, Option<ItemInfo>> {
441+
// To be expanded
442+
return Ok(None);
443+
}
444+
437445
/// Parses an item macro, e.g., `item!();`.
438446
fn parse_item_macro(&mut self, vis: &Visibility) -> PResult<'a, MacCall> {
439447
let path = self.parse_path(PathStyle::Mod)?; // `foo::bar`

‎compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ impl<'a> Parser<'a> {
9090
attrs,
9191
errors::InvalidVariableDeclarationSub::UseLetNotVar,
9292
)?
93-
} else if self.check_path() && !self.token.is_qpath_start() && !self.is_path_start_item() {
93+
} else if self.check_path()
94+
&& !self.token.is_qpath_start()
95+
&& !self.is_path_start_item()
96+
&& !self.is_builtin()
97+
{
9498
// We have avoided contextual keywords like `union`, items with `crate` visibility,
9599
// or `auto trait` items. We aim to parse an arbitrary path `a::b` but not something
96100
// that starts like a path (1 token), but it fact not a path.

‎compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ symbols! {
9595

9696
// Weak keywords, have special meaning only in specific contexts.
9797
Auto: "auto",
98+
Builtin: "builtin",
9899
Catch: "catch",
99100
Default: "default",
100101
MacroRules: "macro_rules",
@@ -440,6 +441,7 @@ symbols! {
440441
breakpoint,
441442
bridge,
442443
bswap,
444+
builtin_syntax,
443445
c_str,
444446
c_str_literals,
445447
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
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct Foo {
2+
v: u8,
3+
w: u8,
4+
}
5+
fn main() {
6+
builtin # offset_of(Foo, v); //~ ERROR `builtin #` syntax is unstable
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: `builtin #` syntax is unstable
2+
--> $DIR/feature-gate-builtin_syntax.rs:6:15
3+
|
4+
LL | builtin # offset_of(Foo, v);
5+
| ^^^^^^^^^
6+
|
7+
= note: see issue #110680 <https://github.com/rust-lang/rust/issues/110680> for more information
8+
= help: add `#![feature(builtin_syntax)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.

‎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, }
Lines changed: 49 additions & 10 deletions
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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#![feature(builtin_syntax)]
2+
3+
// For the exposed macro we already test these errors in the other files,
4+
// but this test helps to make sure the builtin construct also errors.
5+
// This has the same examples as offset-of-arg-count.rs
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, }
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
error: expected one of `!`, `(`, `+`, `,`, `::`, or `<`, found `)`
2+
--> $DIR/offset-of-builtin.rs:8:43
3+
|
4+
LL | builtin # offset_of(NotEnoughArguments);
5+
| ^ expected one of `!`, `(`, `+`, `,`, `::`, or `<`
6+
7+
error: expected identifier, found `,`
8+
--> $DIR/offset-of-builtin.rs:16:41
9+
|
10+
LL | builtin # offset_of(Container, field, too many arguments);
11+
| ^
12+
| |
13+
| expected identifier
14+
| help: remove this comma
15+
16+
error: expected one of `)` or `.`, found `,`
17+
--> $DIR/offset-of-builtin.rs:16:41
18+
|
19+
LL | builtin # offset_of(Container, field, too many arguments);
20+
| ^
21+
| |
22+
| expected one of `)` or `.`
23+
| help: missing `.`
24+
25+
error: expected one of `)` or `.`, found `many`
26+
--> $DIR/offset-of-builtin.rs:16:47
27+
|
28+
LL | builtin # offset_of(Container, field, too many arguments);
29+
| -^^^^ expected one of `)` or `.`
30+
| |
31+
| help: missing `.`
32+
33+
error: expected one of `)` or `.`, found `arguments`
34+
--> $DIR/offset-of-builtin.rs:16:52
35+
|
36+
LL | builtin # offset_of(Container, field, too many arguments);
37+
| -^^^^^^^^^ expected one of `)` or `.`
38+
| |
39+
| help: missing `.`
40+
41+
error: expected identifier, found `)`
42+
--> $DIR/offset-of-builtin.rs:30:30
43+
|
44+
LL | builtin # offset_of(S, f.);
45+
| ^ expected identifier
46+
47+
error: expected identifier, found `,`
48+
--> $DIR/offset-of-builtin.rs:33:30
49+
|
50+
LL | builtin # offset_of(S, f.,);
51+
| ^ expected identifier
52+
53+
error: expected one of `)` or `.`, found `..`
54+
--> $DIR/offset-of-builtin.rs:36:29
55+
|
56+
LL | builtin # offset_of(S, f..);
57+
| ^^ expected one of `)` or `.`
58+
|
59+
help: if you meant to bind the contents of the rest of the array pattern into `f`, use `@`
60+
|
61+
LL | builtin # offset_of(S, f @ ..);
62+
| +
63+
64+
error: aborting due to 8 previous errors
65+

‎tests/ui/offset-of/offset-of-dst-field.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | offset_of!(Alpha, z);
55
| ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `[u8]`
8+
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
89

910
error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
1011
--> $DIR/offset-of-dst-field.rs:31:5
@@ -13,6 +14,7 @@ LL | offset_of!(Beta, z);
1314
| ^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
1415
|
1516
= help: the trait `Sized` is not implemented for `(dyn Trait + 'static)`
17+
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
1618

1719
error[E0277]: the size for values of type `Extern` cannot be known at compilation time
1820
--> $DIR/offset-of-dst-field.rs:32:5
@@ -21,6 +23,7 @@ LL | offset_of!(Gamma, z);
2123
| ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
2224
|
2325
= help: the trait `Sized` is not implemented for `Extern`
26+
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
2427

2528
error: aborting due to 3 previous errors
2629

‎tests/ui/offset-of/offset-of-unstable.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ LL | | );
3333
| |_____^
3434
|
3535
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
36+
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
3637

3738
error[E0658]: use of unstable library feature 'unstable_test_feature'
3839
--> $DIR/offset-of-unstable.rs:18:5
@@ -41,6 +42,7 @@ LL | offset_of!(StableWithUnstableField, unstable);
4142
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4243
|
4344
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
45+
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
4446

4547
error[E0658]: use of unstable library feature 'unstable_test_feature'
4648
--> $DIR/offset-of-unstable.rs:20:5
@@ -49,6 +51,7 @@ LL | offset_of!(StableWithUnstableFieldType, stable.unstable);
4951
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5052
|
5153
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
54+
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
5255

5356
error[E0658]: use of unstable library feature 'unstable_test_feature'
5457
--> $DIR/offset-of-unstable.rs:21:5
@@ -61,6 +64,7 @@ LL | | );
6164
| |_____^
6265
|
6366
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
67+
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
6468

6569
error[E0658]: use of unstable library feature 'unstable_test_feature'
6670
--> $DIR/offset-of-unstable.rs:26:5
@@ -73,6 +77,7 @@ LL | | );
7377
| |_____^
7478
|
7579
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
80+
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
7681

7782
error: aborting due to 8 previous errors
7883

‎tests/ui/parser/builtin-syntax.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(builtin_syntax)]
2+
3+
fn main() {
4+
builtin # foobar(); //~ ERROR unknown `builtin #` construct
5+
}
6+
7+
fn not_identifier() {
8+
builtin # {}(); //~ ERROR expected identifier after
9+
}

‎tests/ui/parser/builtin-syntax.stderr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: unknown `builtin #` construct `foobar`
2+
--> $DIR/builtin-syntax.rs:4:5
3+
|
4+
LL | builtin # foobar();
5+
| ^^^^^^^^^^^^^^^^
6+
7+
error: expected identifier after `builtin #`
8+
--> $DIR/builtin-syntax.rs:8:15
9+
|
10+
LL | builtin # {}();
11+
| ^
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)
Please sign in to comment.