Skip to content

Commit 852e08e

Browse files
authored
Rollup merge of #130282 - compiler-errors:over-overflow, r=BoxyUwU
Do not report an excessive number of overflow errors for an ever-growing deref impl Check that we don't first hit the recursion limit in `get_field_candidates_considering_privacy` before probing for methods when we have a method lookup failure and we want to see if `.field.method()` exists. We also silence overflow error messages if we're probing for methods for diagnostics. Also renames some functions to make it clearer that they're only for diagnostics, and sprinkle some `Autoderef::silence_errors` around to silence unnecessary overflow errors that come from diagnostics. Fixes#130224.
2 parents 7be15b8 + 575c15a commit 852e08e

7 files changed

Lines changed: 99 additions & 39 deletions

File tree

‎compiler/rustc_hir_typeck/src/coercion.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10491049
/// trait or region sub-obligations. (presumably we could, but it's not
10501050
/// particularly important for diagnostics...)
10511051
pub(crate)fnderef_once_mutably_for_diagnostic(&self,expr_ty:Ty<'tcx>) -> Option<Ty<'tcx>>{
1052-
self.autoderef(DUMMY_SP, expr_ty).nth(1).and_then(|(deref_ty, _)| {
1052+
self.autoderef(DUMMY_SP, expr_ty).silence_errors().nth(1).and_then(|(deref_ty, _)| {
10531053
self.infcx
10541054
.type_implements_trait(
10551055
self.tcx.lang_items().deref_mut_trait()?,

‎compiler/rustc_hir_typeck/src/expr.rs‎

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2864,13 +2864,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28642864
(expr_t,"")
28652865
};
28662866
for(found_fields, args)in
2867-
self.get_field_candidates_considering_privacy(span, ty, mod_id, id)
2867+
self.get_field_candidates_considering_privacy_for_diag(span, ty, mod_id, id)
28682868
{
28692869
let field_names = found_fields.iter().map(|field| field.name).collect::<Vec<_>>();
28702870
letmut candidate_fields:Vec<_> = found_fields
28712871
.into_iter()
28722872
.filter_map(|candidate_field| {
2873-
self.check_for_nested_field_satisfying(
2873+
self.check_for_nested_field_satisfying_condition_for_diag(
28742874
span,
28752875
&|candidate_field, _| candidate_field.ident(self.tcx()) == field,
28762876
candidate_field,
@@ -2933,7 +2933,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29332933
.with_span_label(field.span,"private field")
29342934
}
29352935

2936-
pub(crate)fnget_field_candidates_considering_privacy(
2936+
pub(crate)fnget_field_candidates_considering_privacy_for_diag(
29372937
&self,
29382938
span:Span,
29392939
base_ty:Ty<'tcx>,
@@ -2942,7 +2942,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29422942
) -> Vec<(Vec<&'tcx ty::FieldDef>,GenericArgsRef<'tcx>)>{
29432943
debug!("get_field_candidates(span: {:?}, base_t: {:?}", span, base_ty);
29442944

2945-
self.autoderef(span, base_ty)
2945+
letmut autoderef = self.autoderef(span, base_ty).silence_errors();
2946+
let deref_chain:Vec<_> = autoderef.by_ref().collect();
2947+
2948+
// Don't probe if we hit the recursion limit, since it may result in
2949+
// quadratic blowup if we then try to further deref the results of this
2950+
// function. This is a best-effort method, after all.
2951+
if autoderef.reached_recursion_limit(){
2952+
returnvec![];
2953+
}
2954+
2955+
deref_chain
2956+
.into_iter()
29462957
.filter_map(move |(base_t, _)| {
29472958
match base_t.kind(){
29482959
ty::Adt(base_def, args)if !base_def.is_enum() => {
@@ -2975,7 +2986,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29752986

29762987
/// This method is called after we have encountered a missing field error to recursively
29772988
/// search for the field
2978-
pub(crate)fncheck_for_nested_field_satisfying(
2989+
pub(crate)fncheck_for_nested_field_satisfying_condition_for_diag(
29792990
&self,
29802991
span:Span,
29812992
matches:&implFn(&ty::FieldDef,Ty<'tcx>) -> bool,
@@ -3000,20 +3011,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30003011
ifmatches(candidate_field, field_ty){
30013012
returnSome(field_path);
30023013
}else{
3003-
for(nested_fields, subst)in
3004-
self.get_field_candidates_considering_privacy(span, field_ty, mod_id, hir_id)
3014+
for(nested_fields, subst)inself
3015+
.get_field_candidates_considering_privacy_for_diag(
3016+
span, field_ty, mod_id, hir_id,
3017+
)
30053018
{
30063019
// recursively search fields of `candidate_field` if it's a ty::Adt
30073020
for field in nested_fields {
3008-
ifletSome(field_path) = self.check_for_nested_field_satisfying(
3009-
span,
3010-
matches,
3011-
field,
3012-
subst,
3013-
field_path.clone(),
3014-
mod_id,
3015-
hir_id,
3016-
){
3021+
ifletSome(field_path) = self
3022+
.check_for_nested_field_satisfying_condition_for_diag(
3023+
span,
3024+
matches,
3025+
field,
3026+
subst,
3027+
field_path.clone(),
3028+
mod_id,
3029+
hir_id,
3030+
)
3031+
{
30173032
returnSome(field_path);
30183033
}
30193034
}

‎compiler/rustc_hir_typeck/src/method/probe.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
375375
// If our autoderef loop had reached the recursion limit,
376376
// report an overflow error, but continue going on with
377377
// the truncated autoderef list.
378-
if steps.reached_recursion_limit{
378+
if steps.reached_recursion_limit&& !is_suggestion.0{
379379
self.probe(|_| {
380380
let ty = &steps
381381
.steps

‎compiler/rustc_hir_typeck/src/method/suggest.rs‎

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6262
// It might seem that we can use `predicate_must_hold_modulo_regions`,
6363
// but since a Dummy binder is used to fill in the FnOnce trait's arguments,
6464
// type resolution always gives a "maybe" here.
65-
ifself.autoderef(span, ty).any(|(ty, _)| {
65+
ifself.autoderef(span, ty).silence_errors().any(|(ty, _)| {
6666
info!("check deref {:?} error", ty);
6767
matches!(ty.kind(), ty::Error(_) | ty::Infer(_))
6868
}){
6969
returnfalse;
7070
}
7171

72-
self.autoderef(span, ty).any(|(ty, _)| {
72+
self.autoderef(span, ty).silence_errors().any(|(ty, _)| {
7373
info!("check deref {:?} impl FnOnce", ty);
7474
self.probe(|_| {
7575
let trait_ref =
@@ -90,7 +90,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9090
}
9191

9292
fnis_slice_ty(&self,ty:Ty<'tcx>,span:Span) -> bool{
93-
self.autoderef(span, ty).any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
93+
self.autoderef(span, ty)
94+
.silence_errors()
95+
.any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
9496
}
9597

9698
fnimpl_into_iterator_should_be_iterator(
@@ -672,7 +674,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
672674
letmut ty_str_reported = ty_str.clone();
673675
iflet ty::Adt(_, generics) = rcvr_ty.kind(){
674676
if generics.len() > 0{
675-
letmut autoderef = self.autoderef(span, rcvr_ty);
677+
letmut autoderef = self.autoderef(span, rcvr_ty).silence_errors();
676678
let candidate_found = autoderef.any(|(ty, _)| {
677679
iflet ty::Adt(adt_def, _) = ty.kind(){
678680
self.tcx
@@ -2237,6 +2239,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22372239
let impl_ty = self.tcx.type_of(*impl_did).instantiate_identity();
22382240
let target_ty = self
22392241
.autoderef(sugg_span, rcvr_ty)
2242+
.silence_errors()
22402243
.find(|(rcvr_ty, _)| {
22412244
DeepRejectCtxt::relate_rigid_infer(self.tcx).types_may_unify(*rcvr_ty, impl_ty)
22422245
})
@@ -2352,17 +2355,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23522355
err:&mutDiag<'_>,
23532356
) -> bool{
23542357
let tcx = self.tcx;
2355-
let field_receiver = self.autoderef(span, rcvr_ty).find_map(|(ty, _)| match ty.kind(){
2356-
ty::Adt(def, args)if !def.is_enum() => {
2357-
let variant = &def.non_enum_variant();
2358-
tcx.find_field_index(item_name, variant).map(|index| {
2359-
let field = &variant.fields[index];
2360-
let field_ty = field.ty(tcx, args);
2361-
(field, field_ty)
2362-
})
2363-
}
2364-
_ => None,
2365-
});
2358+
let field_receiver =
2359+
self.autoderef(span, rcvr_ty).silence_errors().find_map(|(ty, _)| match ty.kind(){
2360+
ty::Adt(def, args)if !def.is_enum() => {
2361+
let variant = &def.non_enum_variant();
2362+
tcx.find_field_index(item_name, variant).map(|index| {
2363+
let field = &variant.fields[index];
2364+
let field_ty = field.ty(tcx, args);
2365+
(field, field_ty)
2366+
})
2367+
}
2368+
_ => None,
2369+
});
23662370
ifletSome((field, field_ty)) = field_receiver {
23672371
let scope = tcx.parent_module_from_def_id(self.body_id);
23682372
let is_accessible = field.vis.is_accessible_from(scope, tcx);
@@ -2675,9 +2679,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26752679
){
26762680
ifletSelfSource::MethodCall(expr) = source {
26772681
let mod_id = self.tcx.parent_module(expr.hir_id).to_def_id();
2678-
for(fields, args)in
2679-
self.get_field_candidates_considering_privacy(span, actual, mod_id, expr.hir_id)
2680-
{
2682+
for(fields, args)inself.get_field_candidates_considering_privacy_for_diag(
2683+
span,
2684+
actual,
2685+
mod_id,
2686+
expr.hir_id,
2687+
){
26812688
let call_expr = self.tcx.hir().expect_expr(self.tcx.parent_hir_id(expr.hir_id));
26822689

26832690
let lang_items = self.tcx.lang_items();
@@ -2693,7 +2700,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26932700
letmut candidate_fields:Vec<_> = fields
26942701
.into_iter()
26952702
.filter_map(|candidate_field| {
2696-
self.check_for_nested_field_satisfying(
2703+
self.check_for_nested_field_satisfying_condition_for_diag(
26972704
span,
26982705
&|_, field_ty| {
26992706
self.lookup_probe_for_diagnostic(
@@ -3195,7 +3202,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
31953202
letSelfSource::QPath(ty) = self_source else{
31963203
return;
31973204
};
3198-
for(deref_ty, _)inself.autoderef(DUMMY_SP, rcvr_ty).skip(1){
3205+
for(deref_ty, _)inself.autoderef(DUMMY_SP, rcvr_ty).silence_errors().skip(1){
31993206
ifletOk(pick) = self.probe_for_name(
32003207
Mode::Path,
32013208
item_name,
@@ -4221,7 +4228,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
42214228
returnis_local(rcvr_ty);
42224229
}
42234230

4224-
self.autoderef(span, rcvr_ty).any(|(ty, _)| is_local(ty))
4231+
self.autoderef(span, rcvr_ty).silence_errors().any(|(ty, _)| is_local(ty))
42254232
}
42264233
}
42274234

‎compiler/rustc_hir_typeck/src/pat.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,6 +2533,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25332533
err.help("the semantics of slice patterns changed recently; see issue #62254");
25342534
}elseifself
25352535
.autoderef(span, expected_ty)
2536+
.silence_errors()
25362537
.any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
25372538
&& letSome(span) = ti.span
25382539
&& letSome(_) = ti.origin_expr
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use std::ops::Deref;
2+
3+
// Make sure that method probe error reporting doesn't get too tangled up
4+
// on this infinite deref impl. See #130224.
5+
6+
structWrap<T>(T);
7+
impl<T>DerefforWrap<T>{
8+
typeTarget = Wrap<Wrap<T>>;
9+
fnderef(&self) -> &Wrap<Wrap<T>>{todo!()}
10+
}
11+
12+
fnmain(){
13+
Wrap(1).lmao();
14+
//~^ ERROR reached the recursion limit
15+
//~| ERROR no method named `lmao`
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0055]: reached the recursion limit while auto-dereferencing `Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<Wrap<{integer}>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>`
2+
--> $DIR/probe-error-on-infinite-deref.rs:13:13
3+
|
4+
LL | Wrap(1).lmao();
5+
| ^^^^ deref recursion limit reached
6+
|
7+
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`probe_error_on_infinite_deref`)
8+
9+
error[E0599]: no method named `lmao` found for struct `Wrap<{integer}>` in the current scope
10+
--> $DIR/probe-error-on-infinite-deref.rs:13:13
11+
|
12+
LL | struct Wrap<T>(T);
13+
| -------------- method `lmao` not found for this struct
14+
...
15+
LL | Wrap(1).lmao();
16+
| ^^^^ method not found in `Wrap<{integer}>`
17+
18+
error: aborting due to 2 previous errors
19+
20+
Some errors have detailed explanations: E0055, E0599.
21+
For more information about an error, try `rustc --explain E0055`.

0 commit comments

Comments
 (0)