Skip to content
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

Implement #[must_not_suspend] #88865

Merged
merged 9 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions compiler/rustc_typeck/src/check/generator_interior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct InteriorVisitor<'a, 'tcx> {
/// that they may succeed the said yield point in the post-order.
guard_bindings: SmallVec<[SmallVec<[HirId; 4]>; 1]>,
guard_bindings_set: HirIdSet,
linted_values: HirIdSet,
}

impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
Expand Down Expand Up @@ -122,18 +123,21 @@ impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
// Insert the type into the ordered set.
let scope_span = scope.map(|s| s.span(self.fcx.tcx, self.region_scope_tree));

check_must_not_suspend_ty(
self.fcx,
ty,
hir_id,
SuspendCheckData {
expr,
source_span,
yield_span: yield_data.span,
plural_len: 1,
..Default::default()
},
);
if !self.linted_values.contains(&hir_id) {
check_must_not_suspend_ty(
self.fcx,
ty,
hir_id,
SuspendCheckData {
expr,
source_span,
yield_span: yield_data.span,
plural_len: 1,
..Default::default()
},
);
self.linted_values.insert(hir_id);
}

self.types.insert(ty::GeneratorInteriorTypeCause {
span: source_span,
Expand Down Expand Up @@ -181,6 +185,7 @@ pub fn resolve_interior<'a, 'tcx>(
prev_unresolved_span: None,
guard_bindings: <_>::default(),
guard_bindings_set: <_>::default(),
linted_values: <_>::default(),
};
intravisit::walk_body(&mut visitor, body);

Expand Down
20 changes: 20 additions & 0 deletions src/test/ui/lint/must_not_suspend/dedup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// edition:2018
#![feature(must_not_suspend)]
#![deny(must_not_suspend)]

#[must_not_suspend]
struct No {}

async fn shushspend() {}

async fn wheeee<T>(t: T) {
shushspend().await;
drop(t);
}

async fn yes() {
wheeee(No {}).await; //~ ERROR `No` held across
}

fn main() {
}
19 changes: 19 additions & 0 deletions src/test/ui/lint/must_not_suspend/dedup.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error: `No` held across a suspend point, but should not be
--> $DIR/dedup.rs:16:12
|
LL | wheeee(No {}).await;
| -------^^^^^------- the value is held across this suspend point
|
note: the lint level is defined here
--> $DIR/dedup.rs:3:9
|
LL | #![deny(must_not_suspend)]
| ^^^^^^^^^^^^^^^^
help: consider using a block (`{ ... }`) to shrink the value's scope, ending before the suspend point
--> $DIR/dedup.rs:16:12
|
LL | wheeee(No {}).await;
| ^^^^^
Comment on lines +12 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this only makes sense where the value is held locally. If it was found in anther generator, then we should probably not emit this help

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is triggering on the No { } value being held across the whee yield point, I think? I believe that @eholk is improving this analysis


error: aborting due to previous error

9 changes: 4 additions & 5 deletions src/test/ui/lint/must_not_suspend/generic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// edition:2018
// run-pass
//
// this test shows a case where the lint doesn't fire in generic code
#![feature(must_not_suspend)]
#![deny(must_not_suspend)]

Expand All @@ -12,10 +15,6 @@ async fn wheeee<T>(t: T) {
drop(t);
}

async fn yes() {
wheeee(No {}).await; //~ ERROR `No` held across
//~^ ERROR `No` held across
}

fn main() {
let _fut = wheeee(No {});
}
31 changes: 0 additions & 31 deletions src/test/ui/lint/must_not_suspend/generic.stderr

This file was deleted.

1 change: 0 additions & 1 deletion src/test/ui/lint/must_not_suspend/ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ async fn other() {}
impl Bar {
async fn uhoh(&mut self) {
let guard = &mut self.u; //~ ERROR `Umm` held across
//~^ ERROR `Umm` held across

other().await;

Expand Down
24 changes: 2 additions & 22 deletions src/test/ui/lint/must_not_suspend/ref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ error: `Umm` held across a suspend point, but should not be
|
LL | let guard = &mut self.u;
| ^^^^^^
...
LL |
LL | other().await;
| ------------- the value is held across this suspend point
|
Expand All @@ -23,25 +23,5 @@ help: consider using a block (`{ ... }`) to shrink the value's scope, ending bef
LL | let guard = &mut self.u;
| ^^^^^^

error: `Umm` held across a suspend point, but should not be
--> $DIR/ref.rs:18:26
|
LL | let guard = &mut self.u;
| ^^^^^^
...
LL | other().await;
| ------------- the value is held across this suspend point
|
note: You gotta use Umm's, ya know?
--> $DIR/ref.rs:18:26
|
LL | let guard = &mut self.u;
| ^^^^^^
help: consider using a block (`{ ... }`) to shrink the value's scope, ending before the suspend point
--> $DIR/ref.rs:18:26
|
LL | let guard = &mut self.u;
| ^^^^^^

error: aborting due to 2 previous errors
error: aborting due to previous error