Skip to content

Commit

Permalink
fix ICE with -Wrust-2021-incompatible-closure-captures
Browse files Browse the repository at this point in the history
Fixes #93117
Fixes #96258
  • Loading branch information
matthiaskrgr committed Jun 29, 2022
1 parent 8308806 commit 7dc0489
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 2 deletions.
6 changes: 4 additions & 2 deletions compiler/rustc_typeck/src/check/upvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1707,12 +1707,14 @@ fn drop_location_span<'tcx>(tcx: TyCtxt<'tcx>, hir_id: hir::HirId) -> Span {
hir::Node::Item(item) => match item.kind {
hir::ItemKind::Fn(_, _, owner_id) => tcx.hir().span(owner_id.hir_id),
_ => {
bug!("Drop location span error: need to handle more ItemKind {:?}", item.kind);
bug!("Drop location span error: need to handle more ItemKind '{:?}'", item.kind);
}
},
hir::Node::Block(block) => tcx.hir().span(block.hir_id),
hir::Node::TraitItem(item) => tcx.hir().span(item.hir_id()),
hir::Node::ImplItem(item) => tcx.hir().span(item.hir_id()),
_ => {
bug!("Drop location span error: need to handle more Node {:?}", owner_node);
bug!("Drop location span error: need to handle more Node '{:?}'", owner_node);
}
};
tcx.sess.source_map().end_point(owner_span)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// compile-flags: -Wrust-2021-incompatible-closure-captures

pub struct A {}

impl A {
async fn create(path: impl AsRef<std::path::Path>) { //~ ERROR `async fn` is not permitted in Rust 2015
//~^ WARN changes to closure capture in Rust 2021 will affect drop order [rust_2021_incompatible_closure_captures]
;
crate(move || {} ).await //~ ERROR expected function, found module `crate`
}
}

trait C{async fn new(val: T) {} //~ ERROR `async fn` is not permitted in Rust 2015
//~^ ERROR functions in traits cannot be declared `async`
//~^^ ERROR cannot find type `T` in this scope
//~^^^ WARN changes to closure capture in Rust 2021 will affect drop order [rust_2021_incompatible_closure_captures]

//~ ERROR this file contains an unclosed delimiter
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
error: this file contains an unclosed delimiter
--> $DIR/drop-location-span-error-rust-2021-incompatible-closure-captures-93117.rs:18:53
|
LL | trait C{async fn new(val: T) {}
| - unclosed delimiter
...
LL |
| ^

error[E0670]: `async fn` is not permitted in Rust 2015
--> $DIR/drop-location-span-error-rust-2021-incompatible-closure-captures-93117.rs:6:5
|
LL | async fn create(path: impl AsRef<std::path::Path>) {
| ^^^^^ to use `async fn`, switch to Rust 2018 or later
|
= help: pass `--edition 2021` to `rustc`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide

error[E0670]: `async fn` is not permitted in Rust 2015
--> $DIR/drop-location-span-error-rust-2021-incompatible-closure-captures-93117.rs:13:9
|
LL | trait C{async fn new(val: T) {}
| ^^^^^ to use `async fn`, switch to Rust 2018 or later
|
= help: pass `--edition 2021` to `rustc`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide

error[E0706]: functions in traits cannot be declared `async`
--> $DIR/drop-location-span-error-rust-2021-incompatible-closure-captures-93117.rs:13:9
|
LL | trait C{async fn new(val: T) {}
| -----^^^^^^^^^^^^^^^^^^
| |
| `async` because of this
|
= note: `async` trait functions are not currently supported
= note: consider using the `async-trait` crate: https://crates.io/crates/async-trait

error[E0423]: expected function, found module `crate`
--> $DIR/drop-location-span-error-rust-2021-incompatible-closure-captures-93117.rs:9:5
|
LL | crate(move || {} ).await
| ^^^^^ not a function

error[E0412]: cannot find type `T` in this scope
--> $DIR/drop-location-span-error-rust-2021-incompatible-closure-captures-93117.rs:13:27
|
LL | pub struct A {}
| ------------ similarly named struct `A` defined here
...
LL | trait C{async fn new(val: T) {}
| ^ help: a struct with a similar name exists: `A`

warning: changes to closure capture in Rust 2021 will affect drop order
--> $DIR/drop-location-span-error-rust-2021-incompatible-closure-captures-93117.rs:6:57
|
LL | async fn create(path: impl AsRef<std::path::Path>) {
| _____________________----_____________________________-__^
| | | |
| | | in Rust 2018, `path` is dropped here along with the closure, but in Rust 2021 `path` is not part of the closure
| | in Rust 2018, this causes the closure to capture `path`, but in Rust 2021, it has no effect
LL | |
LL | | ;
LL | | crate(move || {} ).await
LL | | }
| |_____^
|
= note: requested on the command line with `-W rust-2021-incompatible-closure-captures`
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/disjoint-capture-in-closures.html>
help: add a dummy let to cause `path` to be fully captured
|
LL | async fn create(path: impl AsRef<std::path::Path>) { let _ = &path;
| ++++++++++++++

warning: changes to closure capture in Rust 2021 will affect drop order
--> $DIR/drop-location-span-error-rust-2021-incompatible-closure-captures-93117.rs:13:30
|
LL | trait C{async fn new(val: T) {}
| --- - ^^
| | |
| | in Rust 2018, `val` is dropped here along with the closure, but in Rust 2021 `val` is not part of the closure
| in Rust 2018, this causes the closure to capture `val`, but in Rust 2021, it has no effect
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/disjoint-capture-in-closures.html>
help: add a dummy let to cause `val` to be fully captured
|
LL | trait C{async fn new(val: T) { let _ = &val;}
| +++++++++++++

error: aborting due to 6 previous errors; 2 warnings emitted

Some errors have detailed explanations: E0412, E0423, E0670, E0706.
For more information about an error, try `rustc --explain E0412`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// compile-flags -Wrust-2021-incompatible-closure-captures

fn main() {}

pub(crate) struct Numberer {}

impl Numberer {
pub(crate) async fn new(
//~^ ERROR `async fn` is not permitted in Rust 2015
interval: Duration,
//~^ ERROR cannot find type `Duration` in this scope
) -> Numberer {
Numberer {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error[E0670]: `async fn` is not permitted in Rust 2015
--> $DIR/drop-location-span-error-rust-2021-incompatible-closure-captures-96258.rs:8:16
|
LL | pub(crate) async fn new(
| ^^^^^ to use `async fn`, switch to Rust 2018 or later
|
= help: pass `--edition 2021` to `rustc`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide

error[E0412]: cannot find type `Duration` in this scope
--> $DIR/drop-location-span-error-rust-2021-incompatible-closure-captures-96258.rs:10:19
|
LL | interval: Duration,
| ^^^^^^^^ not found in this scope
|
help: consider importing this struct
|
LL | use std::time::Duration;
|

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0412, E0670.
For more information about an error, try `rustc --explain E0412`.

0 comments on commit 7dc0489

Please sign in to comment.