Skip to content

Commit

Permalink
macro: ignore ; span
Browse files Browse the repository at this point in the history
  • Loading branch information
nurmohammed840 committed Nov 13, 2024
1 parent 642b01f commit 9b02d37
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 30 deletions.
5 changes: 5 additions & 0 deletions tests-build/tests/fail/macros_type_mismatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ async fn extra_semicolon() -> Result<(), ()> {
Ok(());
}

#[tokio::main]
async fn invalid_return_type() -> Option<()> {
()
}

// https://github.com/tokio-rs/tokio/issues/4635
#[allow(redundant_semicolons)]
#[rustfmt::skip]
Expand Down
39 changes: 27 additions & 12 deletions tests-build/tests/fail/macros_type_mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ error[E0308]: mismatched types
--> tests/fail/macros_type_mismatch.rs:10:5
|
10 | return Ok(());
| ^^^^^^^^^^^^^^ expected `()`, found `Result<(), _>`
| ^^^^^^^^^^^^^ expected `()`, found `Result<(), _>`
|
= note: expected unit type `()`
found enum `Result<(), _>`
Expand All @@ -29,30 +29,45 @@ help: a return type might be missing here
| ++++
help: consider using `Result::expect` to unwrap the `Result<(), _>` value, panicking if the value is a `Result::Err`
|
10 | return Ok(());.expect("REASON")
| +++++++++++++++++
10 | return Ok(()).expect("REASON");
| +++++++++++++++++

error[E0308]: mismatched types
--> tests/fail/macros_type_mismatch.rs:15:5
|
14 | async fn extra_semicolon() -> Result<(), ()> {
| -------------- expected `Result<(), ()>` because of return type
15 | Ok(());
| ^^^^^^^ expected `Result<(), ()>`, found `()`
| ^^^^^^ expected `Result<(), ()>`, found `()`
|
= note: expected enum `Result<(), ()>`
found unit type `()`
help: try wrapping the expression in a variant of `Result`
|
15 | Ok(Ok(());)
| +++ +
15 | Err(Ok(());)
| ++++ +
15 | Ok(Ok(()));
| +++ +
15 | Err(Ok(()));
| ++++ +

error[E0308]: mismatched types
--> tests/fail/macros_type_mismatch.rs:23:5
--> tests/fail/macros_type_mismatch.rs:20:5
|
22 | async fn issue_4635() {
19 | async fn invalid_return_type() -> Option<()> {
| ---------- expected `Option<()>` because of return type
20 | ()
| ^^ expected `Option<()>`, found `()`
|
= note: expected enum `Option<()>`
found unit type `()`
help: try wrapping the expression in `Some`
|
20 | Some(())
| +++++ +

error[E0308]: mismatched types
--> tests/fail/macros_type_mismatch.rs:28:5
|
27 | async fn issue_4635() {
| - help: try adding a return type: `-> i32`
23 | return 1;
| ^^^^^^^^^ expected `()`, found integer
28 | return 1;
| ^^^^^^^^ expected `()`, found integer
42 changes: 24 additions & 18 deletions tokio-macros/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,25 +402,31 @@ fn parse_knobs(mut input: ItemFn, is_test: bool, config: FinalConfig) -> TokenSt
let mut start = Span::call_site();
let mut end = Span::call_site();

let TokenTree::Group(group) = input.body.clone().into_iter().next().unwrap() else {
unreachable!()
};
let mut stream = group.stream().into_iter();
while let Some(tt) = stream.next() {
if matches!(&tt, TokenTree::Punct(p) if p.as_char() == ';') {
continue;
}

start = tt.span();
end = tt.span();

for tt in stream.by_ref() {
match tt {
TokenTree::Punct(p) if p.as_char() == ';' => {
end = p.span();
break;
if let Some(tt) = input.body.clone().into_iter().last() {
match tt {
TokenTree::Group(group) => {
let mut stream = group.stream().into_iter();
while let Some(tt) = stream.next() {
if matches!(&tt, TokenTree::Punct(p) if p.as_char() == ';') {
continue;
}
start = tt.span();
end = tt.span();

for tt in stream.by_ref() {
match tt {
TokenTree::Punct(p) if p.as_char() == ';' => {
// end = p.span();
break;
}
tt => end = tt.span(),
}
}
}
tt => end = tt.span(),
}
_ => {
start = tt.span();
end = tt.span();
}
}
}
Expand Down

0 comments on commit 9b02d37

Please sign in to comment.