Skip to content

Commit 62e414d

Browse files
authored
Rollup merge of #122806 - compiler-errors:type-ascribe, r=fmease
Make `type_ascribe!` not a built-in The only weird thing is the macro expansion note. I wonder if we should suppress these 🤔 r? ````@fmease```` since you told me about builtin# lol
2 parents 3331d0d + a015b90 commit 62e414d

File tree

7 files changed

+28
-44
lines changed

7 files changed

+28
-44
lines changed

compiler/rustc_builtin_macros/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ mod log_syntax;
4949
mod source_util;
5050
mod test;
5151
mod trace_macros;
52-
mod type_ascribe;
5352
mod util;
5453

5554
pub mod asm;
@@ -99,7 +98,6 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
9998
std_panic: edition_panic::expand_panic,
10099
stringify: source_util::expand_stringify,
101100
trace_macros: trace_macros::expand_trace_macros,
102-
type_ascribe: type_ascribe::expand_type_ascribe,
103101
unreachable: edition_panic::expand_unreachable,
104102
// tidy-alphabetical-end
105103
}

compiler/rustc_builtin_macros/src/type_ascribe.rs

-35
This file was deleted.

compiler/rustc_parse/src/parser/expr.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -1939,11 +1939,11 @@ impl<'a> Parser<'a> {
19391939
/// Parse `builtin # ident(args,*)`.
19401940
fn parse_expr_builtin(&mut self) -> PResult<'a, P<Expr>> {
19411941
self.parse_builtin(|this, lo, ident| {
1942-
if ident.name == sym::offset_of {
1943-
return Ok(Some(this.parse_expr_offset_of(lo)?));
1944-
}
1945-
1946-
Ok(None)
1942+
Ok(match ident.name {
1943+
sym::offset_of => Some(this.parse_expr_offset_of(lo)?),
1944+
sym::type_ascribe => Some(this.parse_expr_type_ascribe(lo)?),
1945+
_ => None,
1946+
})
19471947
})
19481948
}
19491949

@@ -1978,6 +1978,7 @@ impl<'a> Parser<'a> {
19781978
ret
19791979
}
19801980

1981+
/// Built-in macro for `offset_of!` expressions.
19811982
pub(crate) fn parse_expr_offset_of(&mut self, lo: Span) -> PResult<'a, P<Expr>> {
19821983
let container = self.parse_ty()?;
19831984
self.expect(&TokenKind::Comma)?;
@@ -2007,6 +2008,15 @@ impl<'a> Parser<'a> {
20072008
Ok(self.mk_expr(span, ExprKind::OffsetOf(container, fields)))
20082009
}
20092010

2011+
/// Built-in macro for type ascription expressions.
2012+
pub(crate) fn parse_expr_type_ascribe(&mut self, lo: Span) -> PResult<'a, P<Expr>> {
2013+
let expr = self.parse_expr()?;
2014+
self.expect(&token::Comma)?;
2015+
let ty = self.parse_ty()?;
2016+
let span = lo.to(self.token.span);
2017+
Ok(self.mk_expr(span, ExprKind::Type(expr, ty)))
2018+
}
2019+
20102020
/// Returns a string literal if the next token is a string literal.
20112021
/// In case of error returns `Some(lit)` if the next token is a literal with a wrong kind,
20122022
/// and returns `None` if the next token is not literal at all.

library/core/src/macros/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1704,14 +1704,14 @@ pub(crate) mod builtin {
17041704
}
17051705

17061706
/// Unstable placeholder for type ascription.
1707-
#[rustc_builtin_macro]
1707+
#[allow_internal_unstable(builtin_syntax)]
17081708
#[unstable(
17091709
feature = "type_ascription",
17101710
issue = "23416",
17111711
reason = "placeholder syntax for type ascription"
17121712
)]
17131713
pub macro type_ascribe($expr:expr, $ty:ty) {
1714-
/* compiler built-in */
1714+
builtin # type_ascribe($expr, $ty)
17151715
}
17161716

17171717
/// Unstable implementation detail of the `rustc` compiler, do not use.

tests/ui/raw-ref-op/raw-ref-temp.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,32 @@ error[E0745]: cannot take address of a temporary
7575
|
7676
LL | let ref_ascribe = &raw const type_ascribe!(2, i32);
7777
| ^^^^^^^^^^^^^^^^^^^^^ temporary value
78+
|
79+
= note: this error originates in the macro `type_ascribe` (in Nightly builds, run with -Z macro-backtrace for more info)
7880

7981
error[E0745]: cannot take address of a temporary
8082
--> $DIR/raw-ref-temp.rs:27:36
8183
|
8284
LL | let mut_ref_ascribe = &raw mut type_ascribe!(3, i32);
8385
| ^^^^^^^^^^^^^^^^^^^^^ temporary value
86+
|
87+
= note: this error originates in the macro `type_ascribe` (in Nightly builds, run with -Z macro-backtrace for more info)
8488

8589
error[E0745]: cannot take address of a temporary
8690
--> $DIR/raw-ref-temp.rs:29:40
8791
|
8892
LL | let ascribe_field_ref = &raw const type_ascribe!(PAIR.0, i32);
8993
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value
94+
|
95+
= note: this error originates in the macro `type_ascribe` (in Nightly builds, run with -Z macro-backtrace for more info)
9096

9197
error[E0745]: cannot take address of a temporary
9298
--> $DIR/raw-ref-temp.rs:30:38
9399
|
94100
LL | let ascribe_index_ref = &raw mut type_ascribe!(ARRAY[0], i32);
95101
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value
102+
|
103+
= note: this error originates in the macro `type_ascribe` (in Nightly builds, run with -Z macro-backtrace for more info)
96104

97105
error: aborting due to 16 previous errors
98106

tests/ui/reachable/expr_type.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ note: the lint level is defined here
1212
|
1313
LL | #![deny(unreachable_code)]
1414
| ^^^^^^^^^^^^^^^^
15+
= note: this error originates in the macro `type_ascribe` (in Nightly builds, run with -Z macro-backtrace for more info)
1516

1617
error: aborting due to 1 previous error
1718

tests/ui/typeck/issue-91267.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ LL | fn main() {
1717
| - expected `()` because of default return type
1818
LL | type_ascribe!(0, u8<e<5>=e>)
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `u8`
20+
|
21+
= note: this error originates in the macro `type_ascribe` (in Nightly builds, run with -Z macro-backtrace for more info)
2022

2123
error: aborting due to 3 previous errors
2224

0 commit comments

Comments
 (0)