-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add type_ascribe!
macro as placeholder syntax for type ascription
#104614
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,40 @@ | ||
error: comparison operators cannot be chained | ||
--> $DIR/issue-93835.rs:2:8 | ||
| | ||
LL | fn e() { | ||
| - while parsing this struct | ||
LL | p:a<p:p<e=6>> | ||
| ^ ^ | ||
| | ||
= help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments | ||
= help: or use `(...)` if you meant to specify fn arguments | ||
|
||
error[E0425]: cannot find value `p` in this scope | ||
--> $DIR/issue-93835.rs:2:5 | ||
| | ||
LL | p:a<p:p<e=6>> | ||
| ^ not found in this scope | ||
| | ||
help: you might have meant to write a `struct` literal | ||
| | ||
LL ~ fn e() { SomeStruct { | ||
LL | p:a<p:p<e=6>> | ||
... | ||
LL | | ||
LL ~ }} | ||
--> $DIR/issue-93835.rs:4:19 | ||
| | ||
help: maybe you meant to write a path separator here | ||
| | ||
LL | p::a<p:p<e=6>> | ||
| ~~ | ||
help: maybe you meant to write an assignment here | ||
| | ||
LL | let p:a<p:p<e=6>> | ||
| ~~~~~ | ||
LL | type_ascribe!(p, a<p:p<e=6>>); | ||
| ^ not found in this scope | ||
|
||
error[E0658]: associated const equality is incomplete | ||
--> $DIR/issue-93835.rs:2:13 | ||
error[E0412]: cannot find type `a` in this scope | ||
--> $DIR/issue-93835.rs:4:22 | ||
| | ||
LL | p:a<p:p<e=6>> | ||
| ^^^ | ||
LL | type_ascribe!(p, a<p:p<e=6>>); | ||
| ^ not found in this scope | ||
|
||
error[E0405]: cannot find trait `p` in this scope | ||
--> $DIR/issue-93835.rs:4:26 | ||
| | ||
= note: see issue #92827 <https://github.com/rust-lang/rust/issues/92827> for more information | ||
= help: add `#![feature(associated_const_equality)]` to the crate attributes to enable | ||
LL | type_ascribe!(p, a<p:p<e=6>>); | ||
| ^ not found in this scope | ||
|
||
error[E0658]: associated const equality is incomplete | ||
--> $DIR/issue-93835.rs:2:13 | ||
--> $DIR/issue-93835.rs:4:28 | ||
| | ||
LL | p:a<p:p<e=6>> | ||
| ^^^ | ||
LL | type_ascribe!(p, a<p:p<e=6>>); | ||
| ^^^ | ||
| | ||
= note: see issue #92827 <https://github.com/rust-lang/rust/issues/92827> for more information | ||
= help: add `#![feature(associated_const_equality)]` to the crate attributes to enable | ||
|
||
error[E0658]: associated type bounds are unstable | ||
--> $DIR/issue-93835.rs:2:9 | ||
--> $DIR/issue-93835.rs:4:24 | ||
| | ||
LL | p:a<p:p<e=6>> | ||
| ^^^^^^^^ | ||
LL | type_ascribe!(p, a<p:p<e=6>>); | ||
| ^^^^^^^^ | ||
| | ||
= note: see issue #52662 <https://github.com/rust-lang/rust/issues/52662> for more information | ||
= help: add `#![feature(associated_type_bounds)]` to the crate attributes to enable | ||
|
||
error: aborting due to 5 previous errors | ||
|
||
Some errors have detailed explanations: E0425, E0658. | ||
For more information about an error, try `rustc --explain E0425`. | ||
Some errors have detailed explanations: E0405, E0412, E0425, E0658. | ||
For more information about an error, try `rustc --explain E0405`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
#![feature(type_ascription)] | ||
|
||
fn main() { | ||
2: n([u8; || 1]) | ||
type_ascribe!(2, n([u8; || 1])) | ||
//~^ ERROR cannot find type `n` in this scope | ||
//~| ERROR mismatched types | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.