Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make type_ascribe! not a built-in #122806

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions compiler/rustc_builtin_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ mod log_syntax;
mod source_util;
mod test;
mod trace_macros;
mod type_ascribe;
mod util;

pub mod asm;
Expand Down Expand Up @@ -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
}
Expand Down
35 changes: 0 additions & 35 deletions compiler/rustc_builtin_macros/src/type_ascribe.rs

This file was deleted.

20 changes: 15 additions & 5 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
})
}

Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/raw-ref-op/raw-ref-temp.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions tests/ui/reachable/expr_type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 2 additions & 0 deletions tests/ui/typeck/issue-91267.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading