Skip to content

Commit 2687f47

Browse files
committed
Auto merge of rust-lang#111971 - cuviper:beta-next, r=cuviper
[beta] backport - Dont check `must_use` on nested `impl Future` from fn rust-lang#111491 - fix recursion depth handling after confirmation rust-lang#111754 r? cuviper
2 parents 16ec1c0 + f2e37f7 commit 2687f47

File tree

10 files changed

+130
-18
lines changed

10 files changed

+130
-18
lines changed

compiler/rustc_lint/src/unused.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
103103
&& let ty = cx.typeck_results().expr_ty(&await_expr)
104104
&& let ty::Alias(ty::Opaque, ty::AliasTy { def_id: future_def_id, .. }) = ty.kind()
105105
&& cx.tcx.ty_is_opaque_future(ty)
106-
// FIXME: This also includes non-async fns that return `impl Future`.
107106
&& let async_fn_def_id = cx.tcx.parent(*future_def_id)
107+
&& matches!(cx.tcx.def_kind(async_fn_def_id), DefKind::Fn | DefKind::AssocFn)
108+
// Check that this `impl Future` actually comes from an `async fn`
109+
&& cx.tcx.asyncness(async_fn_def_id).is_async()
108110
&& check_must_use_def(
109111
cx,
110112
async_fn_def_id,

compiler/rustc_middle/src/traits/mod.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -697,9 +697,9 @@ impl<'tcx, N> ImplSource<'tcx, N> {
697697
}
698698

699699
pub fn borrow_nested_obligations(&self) -> &[N] {
700-
match &self {
701-
ImplSource::UserDefined(i) => &i.nested[..],
702-
ImplSource::Param(n, _) => &n,
700+
match self {
701+
ImplSource::UserDefined(i) => &i.nested,
702+
ImplSource::Param(n, _) => n,
703703
ImplSource::Builtin(i) => &i.nested,
704704
ImplSource::AutoImpl(d) => &d.nested,
705705
ImplSource::Closure(c) => &c.nested,
@@ -713,6 +713,23 @@ impl<'tcx, N> ImplSource<'tcx, N> {
713713
}
714714
}
715715

716+
pub fn borrow_nested_obligations_mut(&mut self) -> &mut [N] {
717+
match self {
718+
ImplSource::UserDefined(i) => &mut i.nested,
719+
ImplSource::Param(n, _) => n,
720+
ImplSource::Builtin(i) => &mut i.nested,
721+
ImplSource::AutoImpl(d) => &mut d.nested,
722+
ImplSource::Closure(c) => &mut c.nested,
723+
ImplSource::Generator(c) => &mut c.nested,
724+
ImplSource::Future(c) => &mut c.nested,
725+
ImplSource::Object(d) => &mut d.nested,
726+
ImplSource::FnPointer(d) => &mut d.nested,
727+
ImplSource::TraitAlias(d) => &mut d.nested,
728+
ImplSource::TraitUpcasting(d) => &mut d.nested,
729+
ImplSource::ConstDestruct(i) => &mut i.nested,
730+
}
731+
}
732+
716733
pub fn map<M, F>(self, f: F) -> ImplSource<'tcx, M>
717734
where
718735
F: FnMut(N) -> M,

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+6
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
131131
}
132132
};
133133

134+
// The obligations returned by confirmation are recursively evaluated
135+
// so we need to make sure they have the correct depth.
136+
for subobligation in impl_src.borrow_nested_obligations_mut() {
137+
subobligation.set_depth_from_parent(obligation.recursion_depth);
138+
}
139+
134140
if !obligation.predicate.is_const_if_const() {
135141
// normalize nested predicates according to parent predicate's constness.
136142
impl_src = impl_src.map(|mut o| {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// edition:2021
2+
3+
use std::future::Future;
4+
5+
pub struct Manager;
6+
7+
impl Manager {
8+
#[must_use]
9+
pub async fn new() -> (Self, impl Future<Output = ()>) {
10+
(Manager, async {})
11+
}
12+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// edition:2021
2+
// aux-build:must-use-foreign.rs
3+
// check-pass
4+
5+
extern crate must_use_foreign;
6+
7+
use must_use_foreign::Manager;
8+
9+
async fn async_main() {
10+
Manager::new().await.1.await;
11+
}
12+
13+
fn main() {
14+
let _ = async_main();
15+
}

tests/ui/lint/unused/unused-async.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async fn test() {
3333
foo().await; //~ ERROR unused output of future returned by `foo` that must be used
3434
bar(); //~ ERROR unused return value of `bar` that must be used
3535
//~^ ERROR unused implementer of `Future` that must be used
36-
bar().await; //~ ERROR unused output of future returned by `bar` that must be used
36+
bar().await; // ok, it's not an async fn
3737
baz(); //~ ERROR unused implementer of `Future` that must be used
3838
baz().await; // ok
3939
}

tests/ui/lint/unused/unused-async.stderr

+1-12
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,6 @@ help: use `let _ = ...` to ignore the resulting value
5252
LL | let _ = bar();
5353
| +++++++
5454

55-
error: unused output of future returned by `bar` that must be used
56-
--> $DIR/unused-async.rs:36:5
57-
|
58-
LL | bar().await;
59-
| ^^^^^^^^^^^
60-
|
61-
help: use `let _ = ...` to ignore the resulting value
62-
|
63-
LL | let _ = bar().await;
64-
| +++++++
65-
6655
error: unused implementer of `Future` that must be used
6756
--> $DIR/unused-async.rs:37:5
6857
|
@@ -71,5 +60,5 @@ LL | baz();
7160
|
7261
= note: futures do nothing unless you `.await` or poll them
7362

74-
error: aborting due to 7 previous errors
63+
error: aborting due to 6 previous errors
7564

tests/ui/traits/cycle-cache-err-60010.stderr

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
error[E0275]: overflow evaluating the requirement `RootDatabase: RefUnwindSafe`
1+
error[E0275]: overflow evaluating the requirement `SalsaStorage: RefUnwindSafe`
22
--> $DIR/cycle-cache-err-60010.rs:27:13
33
|
44
LL | _parse: <ParseQuery as Query<RootDatabase>>::Data,
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7+
= note: required because it appears within the type `PhantomData<SalsaStorage>`
8+
= note: required because it appears within the type `Unique<SalsaStorage>`
9+
= note: required because it appears within the type `Box<SalsaStorage>`
10+
note: required because it appears within the type `Runtime<RootDatabase>`
11+
--> $DIR/cycle-cache-err-60010.rs:23:8
12+
|
13+
LL | struct Runtime<DB: Database> {
14+
| ^^^^^^^
15+
note: required because it appears within the type `RootDatabase`
16+
--> $DIR/cycle-cache-err-60010.rs:20:8
17+
|
18+
LL | struct RootDatabase {
19+
| ^^^^^^^^^^^^
720
note: required for `RootDatabase` to implement `SourceDatabase`
821
--> $DIR/cycle-cache-err-60010.rs:44:9
922
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//~ ERROR overflow
2+
// A regression test for #111729 checking that we correctly
3+
// track recursion depth for obligations returned by confirmation.
4+
use std::panic::RefUnwindSafe;
5+
6+
trait Database {
7+
type Storage;
8+
}
9+
trait Query<DB> {
10+
type Data;
11+
}
12+
struct ParseQuery;
13+
struct RootDatabase {
14+
_runtime: Runtime<RootDatabase>,
15+
}
16+
17+
impl<T: RefUnwindSafe> Database for T {
18+
type Storage = SalsaStorage;
19+
}
20+
impl Database for RootDatabase {
21+
type Storage = SalsaStorage;
22+
}
23+
24+
struct Runtime<DB: Database> {
25+
_storage: Box<DB::Storage>,
26+
}
27+
struct SalsaStorage {
28+
_parse: <ParseQuery as Query<RootDatabase>>::Data,
29+
}
30+
31+
impl<DB: Database> Query<DB> for ParseQuery {
32+
type Data = RootDatabase;
33+
}
34+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0275]: overflow evaluating the requirement `Runtime<RootDatabase>: RefUnwindSafe`
2+
|
3+
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`cycle_via_builtin_auto_trait_impl`)
4+
note: required because it appears within the type `RootDatabase`
5+
--> $DIR/cycle-via-builtin-auto-trait-impl.rs:13:8
6+
|
7+
LL | struct RootDatabase {
8+
| ^^^^^^^^^^^^
9+
note: required for `RootDatabase` to implement `Database`
10+
--> $DIR/cycle-via-builtin-auto-trait-impl.rs:17:24
11+
|
12+
LL | impl<T: RefUnwindSafe> Database for T {
13+
| ------------- ^^^^^^^^ ^
14+
| |
15+
| unsatisfied trait bound introduced here
16+
note: required because it appears within the type `Runtime<RootDatabase>`
17+
--> $DIR/cycle-via-builtin-auto-trait-impl.rs:24:8
18+
|
19+
LL | struct Runtime<DB: Database> {
20+
| ^^^^^^^
21+
22+
error: aborting due to previous error
23+
24+
For more information about this error, try `rustc --explain E0275`.

0 commit comments

Comments
 (0)