Rollup merge of #104614 - Nilstrieb:type-ascribe!, r=TaKO8Ki
Add `type_ascribe!` macro as placeholder syntax for type ascription This makes it still possible to test the internal semantics of type ascription even once the `:`-syntax is removed from the parser. The macro now gets used in a bunch of UI tests that test the semantics and not syntax of type ascription. I might have forgotten a few tests but this should hopefully be most of them. The remaining ones will certainly be found once type ascription is removed from the parser altogether. Part of #101728
This commit is contained in:
commit
4fdc3eb176
32 changed files with 262 additions and 221 deletions
|
@ -45,6 +45,7 @@ mod log_syntax;
|
|||
mod source_util;
|
||||
mod test;
|
||||
mod trace_macros;
|
||||
mod type_ascribe;
|
||||
mod util;
|
||||
|
||||
pub mod asm;
|
||||
|
@ -92,6 +93,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
|
|||
unreachable: edition_panic::expand_unreachable,
|
||||
stringify: source_util::expand_stringify,
|
||||
trace_macros: trace_macros::expand_trace_macros,
|
||||
type_ascribe: type_ascribe::expand_type_ascribe,
|
||||
}
|
||||
|
||||
register_attr! {
|
||||
|
|
35
compiler/rustc_builtin_macros/src/type_ascribe.rs
Normal file
35
compiler/rustc_builtin_macros/src/type_ascribe.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
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::{self, DummyResult, ExtCtxt, MacEager};
|
||||
use rustc_span::Span;
|
||||
|
||||
pub fn expand_type_ascribe(
|
||||
cx: &mut ExtCtxt<'_>,
|
||||
span: Span,
|
||||
tts: TokenStream,
|
||||
) -> Box<dyn base::MacResult + 'static> {
|
||||
let (expr, ty) = match parse_ascribe(cx, tts) {
|
||||
Ok(parsed) => parsed,
|
||||
Err(mut err) => {
|
||||
err.emit();
|
||||
return DummyResult::any(span);
|
||||
}
|
||||
};
|
||||
|
||||
let asc_expr = cx.expr(span, ExprKind::Type(expr, ty));
|
||||
|
||||
return 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))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue