Skip to content

Commit 4fdc3eb

Browse files
authored
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
2 parents 11663b1 + efea79c commit 4fdc3eb

32 files changed

+265
-224
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
@@ -1488,6 +1488,7 @@ symbols! {
14881488
ty,
14891489
type_alias_enum_variants,
14901490
type_alias_impl_trait,
1491+
type_ascribe,
14911492
type_ascription,
14921493
type_changing_struct_update,
14931494
type_id,

library/core/src/macros/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,18 @@ 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+
15491561
/// Unstable implementation detail of the `rustc` compiler, do not use.
15501562
#[rustc_builtin_macro]
15511563
#[stable(feature = "rust1", since = "1.0.0")]

library/core/src/prelude/v1.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,11 @@ 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+
#[cfg(not(bootstrap))]
108+
pub use crate::macros::builtin::type_ascribe;

library/std/src/prelude/v1.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ 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+
#[cfg(not(bootstrap))]
95+
pub use core::prelude::v1::type_ascribe;
96+
8897
// The file so far is equivalent to src/libcore/prelude/v1.rs,
8998
// and below to src/liballoc/prelude.rs.
9099
// Those files are duplicated rather than using glob imports

src/test/ui/associated-consts/issue-93835.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
#![feature(type_ascription)]
2+
13
fn e() {
2-
p:a<p:p<e=6>>
3-
//~^ ERROR comparison operators
4+
type_ascribe!(p, a<p:p<e=6>>);
5+
//~^ ERROR cannot find type `a` in this scope
46
//~| ERROR cannot find value
57
//~| ERROR associated const equality
6-
//~| ERROR associated const equality
8+
//~| ERROR cannot find trait `p` in this scope
79
//~| ERROR associated type bounds
810
}
911

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,40 @@
1-
error: comparison operators cannot be chained
2-
--> $DIR/issue-93835.rs:2:8
3-
|
4-
LL | fn e() {
5-
| - while parsing this struct
6-
LL | p:a<p:p<e=6>>
7-
| ^ ^
8-
|
9-
= help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
10-
= help: or use `(...)` if you meant to specify fn arguments
11-
121
error[E0425]: cannot find value `p` in this scope
13-
--> $DIR/issue-93835.rs:2:5
14-
|
15-
LL | p:a<p:p<e=6>>
16-
| ^ not found in this scope
17-
|
18-
help: you might have meant to write a `struct` literal
19-
|
20-
LL ~ fn e() { SomeStruct {
21-
LL | p:a<p:p<e=6>>
22-
...
23-
LL |
24-
LL ~ }}
2+
--> $DIR/issue-93835.rs:4:19
253
|
26-
help: maybe you meant to write a path separator here
27-
|
28-
LL | p::a<p:p<e=6>>
29-
| ~~
30-
help: maybe you meant to write an assignment here
31-
|
32-
LL | let p:a<p:p<e=6>>
33-
| ~~~~~
4+
LL | type_ascribe!(p, a<p:p<e=6>>);
5+
| ^ not found in this scope
346

35-
error[E0658]: associated const equality is incomplete
36-
--> $DIR/issue-93835.rs:2:13
7+
error[E0412]: cannot find type `a` in this scope
8+
--> $DIR/issue-93835.rs:4:22
379
|
38-
LL | p:a<p:p<e=6>>
39-
| ^^^
10+
LL | type_ascribe!(p, a<p:p<e=6>>);
11+
| ^ not found in this scope
12+
13+
error[E0405]: cannot find trait `p` in this scope
14+
--> $DIR/issue-93835.rs:4:26
4015
|
41-
= note: see issue #92827 <https://github.com/rust-lang/rust/issues/92827> for more information
42-
= help: add `#![feature(associated_const_equality)]` to the crate attributes to enable
16+
LL | type_ascribe!(p, a<p:p<e=6>>);
17+
| ^ not found in this scope
4318

4419
error[E0658]: associated const equality is incomplete
45-
--> $DIR/issue-93835.rs:2:13
20+
--> $DIR/issue-93835.rs:4:28
4621
|
47-
LL | p:a<p:p<e=6>>
48-
| ^^^
22+
LL | type_ascribe!(p, a<p:p<e=6>>);
23+
| ^^^
4924
|
5025
= note: see issue #92827 <https://github.com/rust-lang/rust/issues/92827> for more information
5126
= help: add `#![feature(associated_const_equality)]` to the crate attributes to enable
5227

5328
error[E0658]: associated type bounds are unstable
54-
--> $DIR/issue-93835.rs:2:9
29+
--> $DIR/issue-93835.rs:4:24
5530
|
56-
LL | p:a<p:p<e=6>>
57-
| ^^^^^^^^
31+
LL | type_ascribe!(p, a<p:p<e=6>>);
32+
| ^^^^^^^^
5833
|
5934
= note: see issue #52662 <https://github.com/rust-lang/rust/issues/52662> for more information
6035
= help: add `#![feature(associated_type_bounds)]` to the crate attributes to enable
6136

6237
error: aborting due to 5 previous errors
6338

64-
Some errors have detailed explanations: E0425, E0658.
65-
For more information about an error, try `rustc --explain E0425`.
39+
Some errors have detailed explanations: E0405, E0412, E0425, E0658.
40+
For more information about an error, try `rustc --explain E0405`.

src/test/ui/closures/issue-90871.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
#![feature(type_ascription)]
2+
13
fn main() {
2-
2: n([u8; || 1])
4+
type_ascribe!(2, n([u8; || 1]))
35
//~^ ERROR cannot find type `n` in this scope
46
//~| ERROR mismatched types
57
}

src/test/ui/closures/issue-90871.stderr

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
error[E0412]: cannot find type `n` in this scope
2-
--> $DIR/issue-90871.rs:2:8
2+
--> $DIR/issue-90871.rs:4:22
33
|
4-
LL | 2: n([u8; || 1])
5-
| ^ expecting a type here because of type ascription
4+
LL | type_ascribe!(2, n([u8; || 1]))
5+
| ^ help: a trait with a similar name exists: `Fn`
6+
|
7+
::: $SRC_DIR/core/src/ops/function.rs:LL:COL
8+
|
9+
LL | pub trait Fn<Args: Tuple>: FnMut<Args> {
10+
| -------------------------------------- similarly named trait `Fn` defined here
611

712
error[E0308]: mismatched types
8-
--> $DIR/issue-90871.rs:2:15
13+
--> $DIR/issue-90871.rs:4:29
914
|
10-
LL | 2: n([u8; || 1])
11-
| ^^^^ expected `usize`, found closure
15+
LL | type_ascribe!(2, n([u8; || 1]))
16+
| ^^^^ expected `usize`, found closure
1217
|
1318
= note: expected type `usize`
14-
found closure `[closure@$DIR/issue-90871.rs:2:15: 2:17]`
19+
found closure `[closure@$DIR/issue-90871.rs:4:29: 4:31]`
1520
help: use parentheses to call this closure
1621
|
17-
LL | 2: n([u8; (|| 1)()])
18-
| + +++
22+
LL | type_ascribe!(2, n([u8; (|| 1)()]))
23+
| + +++
1924

2025
error: aborting due to 2 previous errors
2126

src/test/ui/coercion/coerce-expect-unsized-ascribed.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@
66
use std::fmt::Debug;
77

88
pub fn main() {
9-
let _ = box { [1, 2, 3] }: Box<[i32]>; //~ ERROR mismatched types
10-
let _ = box if true { [1, 2, 3] } else { [1, 3, 4] }: Box<[i32]>; //~ ERROR mismatched types
11-
let _ = box match true { true => [1, 2, 3], false => [1, 3, 4] }: Box<[i32]>;
9+
let _ = type_ascribe!(box { [1, 2, 3] }, Box<[i32]>); //~ ERROR mismatched types
10+
let _ = type_ascribe!(box if true { [1, 2, 3] } else { [1, 3, 4] }, Box<[i32]>); //~ ERROR mismatched types
11+
let _ = type_ascribe!(box match true { true => [1, 2, 3], false => [1, 3, 4] }, Box<[i32]>);
1212
//~^ ERROR mismatched types
13-
let _ = box { |x| (x as u8) }: Box<dyn Fn(i32) -> _>; //~ ERROR mismatched types
14-
let _ = box if true { false } else { true }: Box<dyn Debug>; //~ ERROR mismatched types
15-
let _ = box match true { true => 'a', false => 'b' }: Box<dyn Debug>; //~ ERROR mismatched types
13+
let _ = type_ascribe!(box { |x| (x as u8) }, Box<dyn Fn(i32) -> _>); //~ ERROR mismatched types
14+
let _ = type_ascribe!(box if true { false } else { true }, Box<dyn Debug>); //~ ERROR mismatched types
15+
let _ = type_ascribe!(box match true { true => 'a', false => 'b' }, Box<dyn Debug>); //~ ERROR mismatched types
1616

17-
let _ = &{ [1, 2, 3] }: &[i32]; //~ ERROR mismatched types
18-
let _ = &if true { [1, 2, 3] } else { [1, 3, 4] }: &[i32]; //~ ERROR mismatched types
19-
let _ = &match true { true => [1, 2, 3], false => [1, 3, 4] }: &[i32];
17+
let _ = type_ascribe!(&{ [1, 2, 3] }, &[i32]); //~ ERROR mismatched types
18+
let _ = type_ascribe!(&if true { [1, 2, 3] } else { [1, 3, 4] }, &[i32]); //~ ERROR mismatched types
19+
let _ = type_ascribe!(&match true { true => [1, 2, 3], false => [1, 3, 4] }, &[i32]);
2020
//~^ ERROR mismatched types
21-
let _ = &{ |x| (x as u8) }: &dyn Fn(i32) -> _; //~ ERROR mismatched types
22-
let _ = &if true { false } else { true }: &dyn Debug; //~ ERROR mismatched types
23-
let _ = &match true { true => 'a', false => 'b' }: &dyn Debug; //~ ERROR mismatched types
21+
let _ = type_ascribe!(&{ |x| (x as u8) }, &dyn Fn(i32) -> _); //~ ERROR mismatched types
22+
let _ = type_ascribe!(&if true { false } else { true }, &dyn Debug); //~ ERROR mismatched types
23+
let _ = type_ascribe!(&match true { true => 'a', false => 'b' }, &dyn Debug); //~ ERROR mismatched types
2424

25-
let _ = Box::new([1, 2, 3]): Box<[i32]>; //~ ERROR mismatched types
26-
let _ = Box::new(|x| (x as u8)): Box<dyn Fn(i32) -> _>; //~ ERROR mismatched types
25+
let _ = type_ascribe!(Box::new([1, 2, 3]), Box<[i32]>); //~ ERROR mismatched types
26+
let _ = type_ascribe!(Box::new(|x| (x as u8)), Box<dyn Fn(i32) -> _>); //~ ERROR mismatched types
2727

28-
let _ = vec![
28+
let _ = type_ascribe!(vec![
2929
Box::new(|x| (x as u8)),
3030
box |x| (x as i16 as u8),
31-
]: Vec<Box<dyn Fn(i32) -> _>>;
31+
], Vec<Box<dyn Fn(i32) -> _>>);
3232
}

0 commit comments

Comments
 (0)