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
This commit is contained in:
Matthias Krüger 2024-03-21 12:05:09 +01:00 committed by GitHub
commit 62e414d3af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 28 additions and 44 deletions

View File

@ -49,7 +49,6 @@ mod log_syntax;
mod source_util;
mod test;
mod trace_macros;
mod type_ascribe;
mod util;
pub mod asm;
@ -99,7 +98,6 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
std_panic: edition_panic::expand_panic,
stringify: source_util::expand_stringify,
trace_macros: trace_macros::expand_trace_macros,
type_ascribe: type_ascribe::expand_type_ascribe,
unreachable: edition_panic::expand_unreachable,
// tidy-alphabetical-end
}

View File

@ -1,35 +0,0 @@
use rustc_ast::ptr::P;
use rustc_ast::tokenstream::TokenStream;
use rustc_ast::{token, Expr, ExprKind, Ty};
use rustc_errors::PResult;
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult};
use rustc_span::Span;
pub fn expand_type_ascribe(
cx: &mut ExtCtxt<'_>,
span: Span,
tts: TokenStream,
) -> MacroExpanderResult<'static> {
let (expr, ty) = match parse_ascribe(cx, tts) {
Ok(parsed) => parsed,
Err(err) => {
let guar = err.emit();
return ExpandResult::Ready(DummyResult::any(span, guar));
}
};
let asc_expr = cx.expr(span, ExprKind::Type(expr, ty));
ExpandResult::Ready(MacEager::expr(asc_expr))
}
fn parse_ascribe<'a>(cx: &mut ExtCtxt<'a>, stream: TokenStream) -> PResult<'a, (P<Expr>, P<Ty>)> {
let mut parser = cx.new_parser_from_tts(stream);
let expr = parser.parse_expr()?;
parser.expect(&token::Comma)?;
let ty = parser.parse_ty()?;
Ok((expr, ty))
}

View File

@ -1939,11 +1939,11 @@ impl<'a> Parser<'a> {
/// Parse `builtin # ident(args,*)`.
fn parse_expr_builtin(&mut self) -> PResult<'a, P<Expr>> {
self.parse_builtin(|this, lo, ident| {
if ident.name == sym::offset_of {
return Ok(Some(this.parse_expr_offset_of(lo)?));
}
Ok(None)
Ok(match ident.name {
sym::offset_of => Some(this.parse_expr_offset_of(lo)?),
sym::type_ascribe => Some(this.parse_expr_type_ascribe(lo)?),
_ => None,
})
})
}
@ -1978,6 +1978,7 @@ impl<'a> Parser<'a> {
ret
}
/// Built-in macro for `offset_of!` expressions.
pub(crate) fn parse_expr_offset_of(&mut self, lo: Span) -> PResult<'a, P<Expr>> {
let container = self.parse_ty()?;
self.expect(&TokenKind::Comma)?;
@ -2007,6 +2008,15 @@ impl<'a> Parser<'a> {
Ok(self.mk_expr(span, ExprKind::OffsetOf(container, fields)))
}
/// Built-in macro for type ascription expressions.
pub(crate) fn parse_expr_type_ascribe(&mut self, lo: Span) -> PResult<'a, P<Expr>> {
let expr = self.parse_expr()?;
self.expect(&token::Comma)?;
let ty = self.parse_ty()?;
let span = lo.to(self.token.span);
Ok(self.mk_expr(span, ExprKind::Type(expr, ty)))
}
/// Returns a string literal if the next token is a string literal.
/// In case of error returns `Some(lit)` if the next token is a literal with a wrong kind,
/// and returns `None` if the next token is not literal at all.

View File

@ -1704,14 +1704,14 @@ pub(crate) mod builtin {
}
/// Unstable placeholder for type ascription.
#[rustc_builtin_macro]
#[allow_internal_unstable(builtin_syntax)]
#[unstable(
feature = "type_ascription",
issue = "23416",
reason = "placeholder syntax for type ascription"
)]
pub macro type_ascribe($expr:expr, $ty:ty) {
/* compiler built-in */
builtin # type_ascribe($expr, $ty)
}
/// Unstable implementation detail of the `rustc` compiler, do not use.

View File

@ -75,24 +75,32 @@ error[E0745]: cannot take address of a temporary
|
LL | let ref_ascribe = &raw const type_ascribe!(2, i32);
| ^^^^^^^^^^^^^^^^^^^^^ temporary value
|
= note: this error originates in the macro `type_ascribe` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:27:36
|
LL | let mut_ref_ascribe = &raw mut type_ascribe!(3, i32);
| ^^^^^^^^^^^^^^^^^^^^^ temporary value
|
= note: this error originates in the macro `type_ascribe` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:29:40
|
LL | let ascribe_field_ref = &raw const type_ascribe!(PAIR.0, i32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value
|
= note: this error originates in the macro `type_ascribe` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:30:38
|
LL | let ascribe_index_ref = &raw mut type_ascribe!(ARRAY[0], i32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value
|
= note: this error originates in the macro `type_ascribe` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 16 previous errors

View File

@ -12,6 +12,7 @@ note: the lint level is defined here
|
LL | #![deny(unreachable_code)]
| ^^^^^^^^^^^^^^^^
= note: this error originates in the macro `type_ascribe` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 1 previous error

View File

@ -17,6 +17,8 @@ LL | fn main() {
| - expected `()` because of default return type
LL | type_ascribe!(0, u8<e<5>=e>)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `u8`
|
= note: this error originates in the macro `type_ascribe` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 3 previous errors