Skip to content

Commit d6f0c51

Browse files
authored
Rollup merge of #107585 - compiler-errors:fndef-sig-cycle, r=oli-obk
Don't cause a cycle when formatting query description that references a FnDef When a function returns `-> _`, we use typeck to compute what the resulting type of the body _should_ be. If we call another query inside of typeck and hit a cycle error, we attempt to report the cycle error which requires us to compute all of the query descriptions for the stack. However, if one of the queries in that cycle has a query description that references this function as a FnDef type, we'll cause a *second* cycle error from within the cycle error reporting code, since rendering a FnDef requires us to compute its signature. This causes an unwrap to ICE, since during the *second* cycle reporting code, we try to look for a job that isn't in the active jobs list. We can avoid this by using `with_no_queries!` when computing these query descriptions. Fixes #107089 The only drawback is that the rendering of opaque types in cycles regresses a bit :| I'm open to alternate suggestions about how we may handle this...
2 parents 815dc9c + 745d60c commit d6f0c51

File tree

7 files changed

+51
-20
lines changed

7 files changed

+51
-20
lines changed

compiler/rustc_middle/src/ty/print/pretty.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,12 @@ pub trait PrettyPrinter<'tcx>:
675675
p!(")")
676676
}
677677
ty::FnDef(def_id, substs) => {
678-
let sig = self.tcx().fn_sig(def_id).subst(self.tcx(), substs);
679-
p!(print(sig), " {{", print_value_path(def_id, substs), "}}");
678+
if NO_QUERIES.with(|q| q.get()) {
679+
p!(print_def_path(def_id, substs));
680+
} else {
681+
let sig = self.tcx().fn_sig(def_id).subst(self.tcx(), substs);
682+
p!(print(sig), " {{", print_value_path(def_id, substs), "}}");
683+
}
680684
}
681685
ty::FnPtr(ref bare_fn) => p!(print(bare_fn)),
682686
ty::Infer(infer_ty) => {
@@ -734,20 +738,22 @@ pub trait PrettyPrinter<'tcx>:
734738
}
735739
ty::Placeholder(placeholder) => p!(write("Placeholder({:?})", placeholder)),
736740
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => {
737-
// FIXME(eddyb) print this with `print_def_path`.
738741
// We use verbose printing in 'NO_QUERIES' mode, to
739742
// avoid needing to call `predicates_of`. This should
740743
// only affect certain debug messages (e.g. messages printed
741744
// from `rustc_middle::ty` during the computation of `tcx.predicates_of`),
742745
// and should have no effect on any compiler output.
743-
if self.should_print_verbose() || NO_QUERIES.with(|q| q.get()) {
746+
if self.should_print_verbose() {
747+
// FIXME(eddyb) print this with `print_def_path`.
744748
p!(write("Opaque({:?}, {:?})", def_id, substs));
745749
return Ok(self);
746750
}
747751

748752
let parent = self.tcx().parent(def_id);
749753
match self.tcx().def_kind(parent) {
750754
DefKind::TyAlias | DefKind::AssocTy => {
755+
// NOTE: I know we should check for NO_QUERIES here, but it's alright.
756+
// `type_of` on a type alias or assoc type should never cause a cycle.
751757
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id: d, .. }) =
752758
*self.tcx().type_of(parent).kind()
753759
{
@@ -762,7 +768,14 @@ pub trait PrettyPrinter<'tcx>:
762768
p!(print_def_path(def_id, substs));
763769
return Ok(self);
764770
}
765-
_ => return self.pretty_print_opaque_impl_type(def_id, substs),
771+
_ => {
772+
if NO_QUERIES.with(|q| q.get()) {
773+
p!(print_def_path(def_id, &[]));
774+
return Ok(self);
775+
} else {
776+
return self.pretty_print_opaque_impl_type(def_id, substs);
777+
}
778+
}
766779
}
767780
}
768781
ty::Str => p!("str"),

compiler/rustc_query_impl/src/plumbing.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,14 @@ pub(crate) fn create_query_frame<
314314
kind: DepKind,
315315
name: &'static str,
316316
) -> QueryStackFrame<DepKind> {
317-
// Disable visible paths printing for performance reasons.
318-
// Showing visible path instead of any path is not that important in production.
319-
let description = ty::print::with_no_visible_paths!(
320-
// Force filename-line mode to avoid invoking `type_of` query.
321-
ty::print::with_forced_impl_filename_line!(do_describe(tcx.tcx, key))
317+
// Avoid calling queries while formatting the description
318+
let description = ty::print::with_no_queries!(
319+
// Disable visible paths printing for performance reasons.
320+
// Showing visible path instead of any path is not that important in production.
321+
ty::print::with_no_visible_paths!(
322+
// Force filename-line mode to avoid invoking `type_of` query.
323+
ty::print::with_forced_impl_filename_line!(do_describe(tcx.tcx, key))
324+
)
322325
);
323326
let description =
324327
if tcx.sess.verbose() { format!("{description} [{name:?}]") } else { description };

tests/ui/async-await/no-const-async.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ note: ...which requires const checking `x`...
2828
|
2929
LL | pub const async fn x() {}
3030
| ^^^^^^^^^^^^^^^^^^^^^^
31-
= note: ...which requires computing whether `impl core::future::future::Future<Output = ()>` is freeze...
32-
= note: ...which requires evaluating trait selection obligation `impl core::future::future::Future<Output = ()>: core::marker::Freeze`...
31+
= note: ...which requires computing whether `x::{opaque#0}` is freeze...
32+
= note: ...which requires evaluating trait selection obligation `x::{opaque#0}: core::marker::Freeze`...
3333
= note: ...which again requires computing type of `x::{opaque#0}`, completing the cycle
3434
note: cycle used when checking item types in top-level module
3535
--> $DIR/no-const-async.rs:4:1

tests/ui/impl-trait/auto-trait-leak.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ note: ...which requires type-checking `cycle1`...
3939
|
4040
LL | send(cycle2().clone());
4141
| ^^^^
42-
= note: ...which requires evaluating trait selection obligation `impl core::clone::Clone: core::marker::Send`...
42+
= note: ...which requires evaluating trait selection obligation `cycle2::{opaque#0}: core::marker::Send`...
4343
note: ...which requires computing type of `cycle2::{opaque#0}`...
4444
--> $DIR/auto-trait-leak.rs:19:16
4545
|
@@ -80,7 +80,7 @@ note: ...which requires type-checking `cycle2`...
8080
|
8181
LL | send(cycle1().clone());
8282
| ^^^^
83-
= note: ...which requires evaluating trait selection obligation `impl core::clone::Clone: core::marker::Send`...
83+
= note: ...which requires evaluating trait selection obligation `cycle1::{opaque#0}: core::marker::Send`...
8484
= note: ...which again requires computing type of `cycle1::{opaque#0}`, completing the cycle
8585
note: cycle used when checking item types in top-level module
8686
--> $DIR/auto-trait-leak.rs:1:1

tests/ui/parser/fn-header-semantic-fail.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ note: ...which requires const checking `main::ff5`...
209209
|
210210
LL | const async unsafe extern "C" fn ff5() {}
211211
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
212-
= note: ...which requires computing whether `impl core::future::future::Future<Output = ()>` is freeze...
213-
= note: ...which requires evaluating trait selection obligation `impl core::future::future::Future<Output = ()>: core::marker::Freeze`...
212+
= note: ...which requires computing whether `main::ff5::{opaque#0}` is freeze...
213+
= note: ...which requires evaluating trait selection obligation `main::ff5::{opaque#0}: core::marker::Freeze`...
214214
= note: ...which again requires computing type of `main::ff5::{opaque#0}`, completing the cycle
215215
note: cycle used when checking item types in top-level module
216216
--> $DIR/fn-header-semantic-fail.rs:5:1
@@ -245,8 +245,8 @@ note: ...which requires const checking `main::<impl at $DIR/fn-header-semantic-f
245245
|
246246
LL | const async unsafe extern "C" fn ft5() {}
247247
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
248-
= note: ...which requires computing whether `impl core::future::future::Future<Output = ()>` is freeze...
249-
= note: ...which requires evaluating trait selection obligation `impl core::future::future::Future<Output = ()>: core::marker::Freeze`...
248+
= note: ...which requires computing whether `main::<impl at $DIR/fn-header-semantic-fail.rs:28:5: 28:17>::ft5::{opaque#0}` is freeze...
249+
= note: ...which requires evaluating trait selection obligation `main::<impl at $DIR/fn-header-semantic-fail.rs:28:5: 28:17>::ft5::{opaque#0}: core::marker::Freeze`...
250250
= note: ...which again requires computing type of `main::<impl at $DIR/fn-header-semantic-fail.rs:28:5: 28:17>::ft5::{opaque#0}`, completing the cycle
251251
note: cycle used when checking item types in top-level module
252252
--> $DIR/fn-header-semantic-fail.rs:5:1
@@ -281,8 +281,8 @@ note: ...which requires const checking `main::<impl at $DIR/fn-header-semantic-f
281281
|
282282
LL | const async unsafe extern "C" fn fi5() {}
283283
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
284-
= note: ...which requires computing whether `impl core::future::future::Future<Output = ()>` is freeze...
285-
= note: ...which requires evaluating trait selection obligation `impl core::future::future::Future<Output = ()>: core::marker::Freeze`...
284+
= note: ...which requires computing whether `main::<impl at $DIR/fn-header-semantic-fail.rs:40:5: 40:11>::fi5::{opaque#0}` is freeze...
285+
= note: ...which requires evaluating trait selection obligation `main::<impl at $DIR/fn-header-semantic-fail.rs:40:5: 40:11>::fi5::{opaque#0}: core::marker::Freeze`...
286286
= note: ...which again requires computing type of `main::<impl at $DIR/fn-header-semantic-fail.rs:40:5: 40:11>::fi5::{opaque#0}`, completing the cycle
287287
note: cycle used when checking item types in top-level module
288288
--> $DIR/fn-header-semantic-fail.rs:5:1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn a() -> _ {
2+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
3+
&a
4+
}
5+
6+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
2+
--> $DIR/no-query-in-printing-during-query-descr.rs:1:11
3+
|
4+
LL | fn a() -> _ {
5+
| ^ not allowed in type signatures
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0121`.

0 commit comments

Comments
 (0)