Skip to content

Commit 89fd6ae

Browse files
committed
correct span, add help message and add UI test when query depth overflows
1 parent 44506f3 commit 89fd6ae

File tree

8 files changed

+82
-24
lines changed

8 files changed

+82
-24
lines changed

compiler/rustc_error_messages/locales/en-US/query_system.ftl

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ query_system_cycle_recursive_trait_alias = trait aliases cannot be recursive
2323
query_system_cycle_which_requires = ...which requires {$desc}...
2424
2525
query_system_query_overflow = queries overflow the depth limit!
26+
.help = consider increasing the recursion limit by adding a `#![recursion_limit = "{$suggested_limit}"]` attribute to your crate (`{$crate_name}`)
2627
27-
query_system_layout_of_depth = Query depth increased by {$depth} when {$desc}!
28+
query_system_layout_of_depth = query depth increased by {$depth} when {$desc}

compiler/rustc_query_impl/src/plumbing.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ use rustc_query_system::query::{
1919
force_query, QueryConfig, QueryContext, QueryDescription, QueryJobId, QueryMap,
2020
QuerySideEffects, QueryStackFrame,
2121
};
22-
use rustc_query_system::Value;
22+
use rustc_query_system::{LayoutOfDepth, QueryOverflow, Value};
2323
use rustc_serialize::Decodable;
24+
use rustc_session::Limit;
25+
use rustc_span::def_id::LOCAL_CRATE;
2426
use std::any::Any;
2527
use std::num::NonZeroU64;
2628
use thin_vec::ThinVec;
@@ -127,6 +129,29 @@ impl QueryContext for QueryCtxt<'_> {
127129
})
128130
})
129131
}
132+
133+
fn depth_limit_error(&self, job: QueryJobId) {
134+
let mut span = None;
135+
let mut layout_of_depth = None;
136+
if let Some(map) = self.try_collect_active_jobs() {
137+
if let Some((info, depth)) = job.try_find_layout_root(map) {
138+
span = Some(info.job.span);
139+
layout_of_depth = Some(LayoutOfDepth { desc: info.query.description, depth });
140+
}
141+
}
142+
143+
let suggested_limit = match self.recursion_limit() {
144+
Limit(0) => Limit(2),
145+
limit => limit * 2,
146+
};
147+
148+
self.sess.emit_fatal(QueryOverflow {
149+
span,
150+
layout_of_depth,
151+
suggested_limit,
152+
crate_name: self.crate_name(LOCAL_CRATE),
153+
});
154+
}
130155
}
131156

132157
impl<'tcx> QueryCtxt<'tcx> {

compiler/rustc_query_system/src/error.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_errors::AddSubdiagnostic;
2-
use rustc_span::Span;
2+
use rustc_session::Limit;
3+
use rustc_span::{Span, Symbol};
34

45
pub struct CycleStack {
56
pub span: Span,
@@ -76,17 +77,20 @@ pub struct IncrementCompilation {
7677
}
7778

7879
#[derive(SessionDiagnostic)]
80+
#[help]
7981
#[diag(query_system::query_overflow)]
8082
pub struct QueryOverflow {
83+
#[primary_span]
84+
pub span: Option<Span>,
8185
#[subdiagnostic]
8286
pub layout_of_depth: Option<LayoutOfDepth>,
87+
pub suggested_limit: Limit,
88+
pub crate_name: Symbol,
8389
}
8490

8591
#[derive(SessionSubdiagnostic)]
8692
#[note(query_system::layout_of_depth)]
8793
pub struct LayoutOfDepth {
88-
#[primary_span]
89-
pub span: Span,
9094
pub desc: String,
9195
pub depth: usize,
9296
}

compiler/rustc_query_system/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ pub mod query;
2323
mod values;
2424

2525
pub use error::HandleCycleError;
26+
pub use error::LayoutOfDepth;
27+
pub use error::QueryOverflow;
2628
pub use values::Value;

compiler/rustc_query_system/src/query/job.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,7 @@ impl QueryJobId {
160160

161161
#[cold]
162162
#[inline(never)]
163-
pub(super) fn try_find_layout_root(
164-
&self,
165-
query_map: QueryMap,
166-
) -> Option<(QueryJobInfo, usize)> {
163+
pub fn try_find_layout_root(&self, query_map: QueryMap) -> Option<(QueryJobInfo, usize)> {
167164
let mut last_layout = None;
168165
let mut current_id = Some(*self);
169166
let mut depth = 0;

compiler/rustc_query_system/src/query/mod.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use self::caches::{
1414
mod config;
1515
pub use self::config::{QueryConfig, QueryDescription, QueryVTable};
1616

17-
use crate::dep_graph::{DepContext, DepNodeIndex, HasDepContext, SerializedDepNodeIndex};
17+
use crate::dep_graph::{DepNodeIndex, HasDepContext, SerializedDepNodeIndex};
1818
use rustc_data_structures::sync::Lock;
1919
use rustc_errors::Diagnostic;
2020
use rustc_hir::def::DefKind;
@@ -123,18 +123,5 @@ pub trait QueryContext: HasDepContext {
123123
compute: impl FnOnce() -> R,
124124
) -> R;
125125

126-
fn depth_limit_error(&self, job: QueryJobId) {
127-
let sess = self.dep_context().sess();
128-
let mut layout_of_depth = None;
129-
if let Some(map) = self.try_collect_active_jobs() {
130-
if let Some((info, depth)) = job.try_find_layout_root(map) {
131-
layout_of_depth = Some(crate::error::LayoutOfDepth {
132-
span: info.job.span,
133-
desc: info.query.description,
134-
depth,
135-
});
136-
}
137-
}
138-
sess.emit_fatal(crate::error::QueryOverflow { layout_of_depth });
139-
}
126+
fn depth_limit_error(&self, job: QueryJobId);
140127
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// build-fail
2+
3+
#![recursion_limit = "64"]
4+
type Byte = Option<Option<Option<Option< Option<Option<Option<Option<
5+
Option<Option<Option<Option< Option<Option<Option<Option<
6+
Option<Option<Option<Option< Option<Option<Option<Option<
7+
Option<Option<Option<Option< Option<Option<Option<Option<
8+
Option<Option<Option<Option< Option<Option<Option<Option<
9+
Option<Option<Option<Option< Option<Option<Option<Option<
10+
Option<Option<Option<Option< Option<Option<Option<Option<
11+
Option<Option<Option<Option< Option<Option<Option<Option<
12+
Option<Option<Option<Option< Option<Option<Option<Option<
13+
Option<Option<Option<Option< Option<Option<Option<Option<
14+
Option<Option<Option<Option< Option<Option<Option<Option<
15+
Box<String>
16+
>>>> >>>>
17+
>>>> >>>>
18+
>>>> >>>>
19+
>>>> >>>>
20+
>>>> >>>>
21+
>>>> >>>>
22+
>>>> >>>>
23+
>>>> >>>>
24+
>>>> >>>>
25+
>>>> >>>>
26+
>>>> >>>>;
27+
28+
fn main() {
29+
//~^ ERROR: queries overflow the depth limit!
30+
println!("{}", std::mem::size_of::<Byte>());
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: queries overflow the depth limit!
2+
--> $DIR/query_depth.rs:28:1
3+
|
4+
LL | fn main() {
5+
| ^^^^^^^^^
6+
|
7+
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "128"]` attribute to your crate (`query_depth`)
8+
= note: query depth increased by 66 when computing layout of `core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<core::option::Option<alloc::boxed::Box<alloc::string::String>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>`
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)