Skip to content

Commit fd93990

Browse files
committed
Improve use of spans in quote_spanned
1 parent a5093fe commit fd93990

File tree

4 files changed

+28
-45
lines changed

4 files changed

+28
-45
lines changed

src/expand.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,18 +404,18 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) {
404404
ReplaceSelf.visit_block_mut(block);
405405
}
406406

407-
let stmts = &block.stmts;
408407
let let_ret = match &mut sig.output {
409-
ReturnType::Default => quote_spanned! {block.brace_token.span=>
408+
ReturnType::Default => quote! {
410409
#(#decls)*
411-
let () = { #(#stmts)* };
410+
let _: () = #block;
412411
},
413412
ReturnType::Type(_, ret) => {
414413
if contains_associated_type_impl_trait(context, ret) {
415414
if decls.is_empty() {
415+
let stmts = &block.stmts;
416416
quote!(#(#stmts)*)
417417
} else {
418-
quote!(#(#decls)* { #(#stmts)* })
418+
quote!(#(#decls)* #block)
419419
}
420420
} else {
421421
let mut ret = ret.clone();
@@ -426,14 +426,14 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) {
426426
return __ret;
427427
}
428428
#(#decls)*
429-
let __ret: #ret = { #(#stmts)* };
429+
let __ret: #ret = #block;
430430
#[allow(unreachable_code)]
431431
__ret
432432
}
433433
}
434434
}
435435
};
436-
let box_pin = quote_spanned!(block.brace_token.span=>
436+
let box_pin = quote_spanned!(sig.asyncness.unwrap().span =>
437437
Box::pin(async move { #let_ret })
438438
);
439439
block.stmts = parse_quote!(#box_pin);

tests/ui/consider-restricting.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
error: future cannot be sent between threads safely
2-
--> tests/ui/consider-restricting.rs:16:50
2+
--> tests/ui/consider-restricting.rs:16:5
33
|
44
16 | async fn publish<T: IntoUrl>(&self, _url: T) {}
5-
| ^^ future created by async block is not `Send`
5+
| ^^^^^ future created by async block is not `Send`
66
|
77
note: captured value is not `Send`
88
--> tests/ui/consider-restricting.rs:16:41
99
|
1010
16 | async fn publish<T: IntoUrl>(&self, _url: T) {}
1111
| ^^^^ has type `T` which is not `Send`
12-
= note: required for the cast from `Pin<Box<{async block@$DIR/tests/ui/consider-restricting.rs:16:50: 16:52}>>` to `Pin<Box<dyn Future<Output = ()> + Send>>`
12+
= note: required for the cast from `Pin<Box<{async block@$DIR/tests/ui/consider-restricting.rs:16:5: 16:10}>>` to `Pin<Box<dyn Future<Output = ()> + Send>>`
1313
help: consider further restricting type parameter `T` with trait `Send`
1414
|
1515
16 | async fn publish<T: IntoUrl + std::marker::Send>(&self, _url: T) {}
1616
| +++++++++++++++++++
1717

1818
error: future cannot be sent between threads safely
19-
--> tests/ui/consider-restricting.rs:23:41
19+
--> tests/ui/consider-restricting.rs:23:5
2020
|
2121
23 | async fn publish<T>(&self, _url: T) {}
22-
| ^^ future created by async block is not `Send`
22+
| ^^^^^ future created by async block is not `Send`
2323
|
2424
note: captured value is not `Send`
2525
--> tests/ui/consider-restricting.rs:23:32
2626
|
2727
23 | async fn publish<T>(&self, _url: T) {}
2828
| ^^^^ has type `T` which is not `Send`
29-
= note: required for the cast from `Pin<Box<{async block@$DIR/tests/ui/consider-restricting.rs:23:41: 23:43}>>` to `Pin<Box<dyn Future<Output = ()> + Send>>`
29+
= note: required for the cast from `Pin<Box<{async block@$DIR/tests/ui/consider-restricting.rs:23:5: 23:10}>>` to `Pin<Box<dyn Future<Output = ()> + Send>>`
3030
help: consider further restricting type parameter `T` with trait `Send`
3131
|
3232
23 | async fn publish<T + std::marker::Send>(&self, _url: T) {}

tests/ui/send-not-implemented.stderr

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,31 @@
11
error: future cannot be sent between threads safely
2-
--> tests/ui/send-not-implemented.rs:8:26
2+
--> tests/ui/send-not-implemented.rs:8:5
33
|
4-
8 | async fn test(&self) {
5-
| __________________________^
6-
9 | | let mutex = Mutex::new(());
7-
10 | | let _guard = mutex.lock().unwrap();
8-
11 | | f().await;
9-
12 | | }
10-
| |_____^ future created by async block is not `Send`
4+
8 | async fn test(&self) {
5+
| ^^^^^ future created by async block is not `Send`
116
|
12-
= help: within `{async block@$DIR/tests/ui/send-not-implemented.rs:8:26: 12:6}`, the trait `Send` is not implemented for `std::sync::MutexGuard<'_, ()>`
7+
= help: within `{async block@$DIR/tests/ui/send-not-implemented.rs:8:5: 8:10}`, the trait `Send` is not implemented for `std::sync::MutexGuard<'_, ()>`
138
note: future is not `Send` as this value is used across an await
149
--> tests/ui/send-not-implemented.rs:11:13
1510
|
1611
10 | let _guard = mutex.lock().unwrap();
1712
| ------ has type `std::sync::MutexGuard<'_, ()>` which is not `Send`
1813
11 | f().await;
1914
| ^^^^^ await occurs here, with `_guard` maybe used later
20-
= note: required for the cast from `Pin<Box<{async block@$DIR/tests/ui/send-not-implemented.rs:8:26: 12:6}>>` to `Pin<Box<dyn Future<Output = ()> + Send>>`
15+
= note: required for the cast from `Pin<Box<{async block@$DIR/tests/ui/send-not-implemented.rs:8:5: 8:10}>>` to `Pin<Box<dyn Future<Output = ()> + Send>>`
2116

2217
error: future cannot be sent between threads safely
23-
--> tests/ui/send-not-implemented.rs:14:38
18+
--> tests/ui/send-not-implemented.rs:14:5
2419
|
25-
14 | async fn test_ret(&self) -> bool {
26-
| ______________________________________^
27-
15 | | let mutex = Mutex::new(());
28-
16 | | let _guard = mutex.lock().unwrap();
29-
17 | | f().await;
30-
18 | | true
31-
19 | | }
32-
| |_____^ future created by async block is not `Send`
20+
14 | async fn test_ret(&self) -> bool {
21+
| ^^^^^ future created by async block is not `Send`
3322
|
34-
= help: within `{async block@$DIR/tests/ui/send-not-implemented.rs:14:38: 19:6}`, the trait `Send` is not implemented for `std::sync::MutexGuard<'_, ()>`
23+
= help: within `{async block@$DIR/tests/ui/send-not-implemented.rs:14:5: 14:10}`, the trait `Send` is not implemented for `std::sync::MutexGuard<'_, ()>`
3524
note: future is not `Send` as this value is used across an await
3625
--> tests/ui/send-not-implemented.rs:17:13
3726
|
3827
16 | let _guard = mutex.lock().unwrap();
3928
| ------ has type `std::sync::MutexGuard<'_, ()>` which is not `Send`
4029
17 | f().await;
4130
| ^^^^^ await occurs here, with `_guard` maybe used later
42-
= note: required for the cast from `Pin<Box<{async block@$DIR/tests/ui/send-not-implemented.rs:14:38: 19:6}>>` to `Pin<Box<dyn Future<Output = bool> + Send>>`
31+
= note: required for the cast from `Pin<Box<{async block@$DIR/tests/ui/send-not-implemented.rs:14:5: 14:10}>>` to `Pin<Box<dyn Future<Output = bool> + Send>>`

tests/ui/type-mismatch.stderr

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
error[E0308]: mismatched types
2-
--> tests/ui/type-mismatch.rs:13:23
2+
--> tests/ui/type-mismatch.rs:14:9
33
|
4-
13 | async fn f(&self) {
5-
| _______________________^
6-
14 | | 0
7-
15 | | }
8-
| |_____^ expected integer, found `()`
4+
14 | 0
5+
| ^ expected `()`, found integer
96

107
error[E0308]: mismatched types
11-
--> tests/ui/type-mismatch.rs:16:23
8+
--> tests/ui/type-mismatch.rs:17:9
129
|
13-
16 | async fn g(&self) {
14-
| _______________________^
15-
17 | | 0
16-
18 | | }
17-
| |_____^ expected integer, found `()`
10+
17 | 0
11+
| ^ expected `()`, found integer

0 commit comments

Comments
 (0)