Skip to content

Commit 6ee0dd9

Browse files
committed
Add unstable type_ascribe macro
This macro serves as a placeholder for future type ascription syntax to make sure that the semantic implementation keeps working.
1 parent 2a43428 commit 6ee0dd9

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ mod log_syntax;
4545
mod source_util;
4646
mod test;
4747
mod trace_macros;
48+
mod type_ascribe;
4849
mod util;
4950

5051
pub mod asm;
@@ -92,6 +93,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
9293
unreachable: edition_panic::expand_unreachable,
9394
stringify: source_util::expand_stringify,
9495
trace_macros: trace_macros::expand_trace_macros,
96+
type_ascribe: type_ascribe::expand_type_ascribe,
9597
}
9698

9799
register_attr! {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use rustc_ast::ptr::P;
2+
use rustc_ast::tokenstream::TokenStream;
3+
use rustc_ast::{token, Expr, ExprKind, Ty};
4+
use rustc_errors::PResult;
5+
use rustc_expand::base::{self, DummyResult, ExtCtxt, MacEager};
6+
use rustc_span::Span;
7+
8+
pub fn expand_type_ascribe(
9+
cx: &mut ExtCtxt<'_>,
10+
span: Span,
11+
tts: TokenStream,
12+
) -> Box<dyn base::MacResult + 'static> {
13+
let (expr, ty) = match parse_ascribe(cx, tts) {
14+
Ok(parsed) => parsed,
15+
Err(mut err) => {
16+
err.emit();
17+
return DummyResult::any(span);
18+
}
19+
};
20+
21+
let asc_expr = cx.expr(span, ExprKind::Type(expr, ty));
22+
23+
return MacEager::expr(asc_expr);
24+
}
25+
26+
fn parse_ascribe<'a>(cx: &mut ExtCtxt<'a>, stream: TokenStream) -> PResult<'a, (P<Expr>, P<Ty>)> {
27+
let mut parser = cx.new_parser_from_tts(stream);
28+
29+
let expr = parser.parse_expr()?;
30+
parser.expect(&token::Comma)?;
31+
32+
let ty = parser.parse_ty()?;
33+
34+
Ok((expr, ty))
35+
}

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,7 @@ symbols! {
14851485
ty,
14861486
type_alias_enum_variants,
14871487
type_alias_impl_trait,
1488+
type_ascribe,
14881489
type_ascription,
14891490
type_changing_struct_update,
14901491
type_id,

library/core/src/macros/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,29 @@ pub(crate) mod builtin {
15461546
/* compiler built-in */
15471547
}
15481548

1549+
/// Unstable placeholder for type ascription.
1550+
#[rustc_builtin_macro]
1551+
#[unstable(
1552+
feature = "type_ascription",
1553+
issue = "23416",
1554+
reason = "placeholder syntax for type ascription"
1555+
)]
1556+
#[cfg(not(bootstrap))]
1557+
pub macro type_ascribe($expr:expr, $ty:ty) {
1558+
/* compiler built-in */
1559+
}
1560+
1561+
/// Unstable placeholder for type ascription.
1562+
#[unstable(
1563+
feature = "type_ascription",
1564+
issue = "23416",
1565+
reason = "placeholder syntax for type ascription"
1566+
)]
1567+
#[cfg(bootstrap)]
1568+
pub macro type_ascribe($expr:expr, $ty:ty) {
1569+
$expr: $ty
1570+
}
1571+
15491572
/// Unstable implementation detail of the `rustc` compiler, do not use.
15501573
#[rustc_builtin_macro]
15511574
#[stable(feature = "rust1", since = "1.0.0")]

library/core/src/prelude/v1.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,10 @@ pub use crate::macros::builtin::cfg_accessible;
9898
reason = "`cfg_eval` is a recently implemented feature"
9999
)]
100100
pub use crate::macros::builtin::cfg_eval;
101+
102+
#[unstable(
103+
feature = "type_ascription",
104+
issue = "23416",
105+
reason = "placeholder syntax for type ascription"
106+
)]
107+
pub use crate::macros::builtin::type_ascribe;

library/std/src/prelude/v1.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ pub use core::prelude::v1::cfg_accessible;
8585
)]
8686
pub use core::prelude::v1::cfg_eval;
8787

88+
// Do not `doc(no_inline)` either.
89+
#[unstable(
90+
feature = "type_ascription",
91+
issue = "23416",
92+
reason = "placeholder syntax for type ascription"
93+
)]
94+
pub use core::prelude::v1::type_ascribe;
95+
8896
// The file so far is equivalent to src/libcore/prelude/v1.rs,
8997
// and below to src/liballoc/prelude.rs.
9098
// Those files are duplicated rather than using glob imports

0 commit comments

Comments
 (0)