Skip to content

Commit f635c37

Browse files
authored
Rollup merge of rust-lang#69936 - Aaron1011:fix/suggestion-cycle, r=varkor
Fix cycle error when emitting suggestion for mismatched `fn` type Fixes rust-lang#66667 Previously, we called `tcx.typeck_tables_of` when determining whether or not to emit a suggestion for a type error. However, we might already be type-checking the `DefId` we pass to `typeck_tables_of` (it could be anywhere in the query stack). Fortunately, we only need the function signature, not the entire `TypeckTables`. By using `tcx.fn_sig`, we avoid the possibility of cycle errors while retaining the ability to emit a suggestion.
2 parents 0f6144a + ff65bff commit f635c37

File tree

3 files changed

+75
-22
lines changed

3 files changed

+75
-22
lines changed

src/librustc_typeck/check/op.rs

+4-22
Original file line numberDiff line numberDiff line change
@@ -492,36 +492,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
492492
err.span_label(span, ty.to_string());
493493
if let FnDef(def_id, _) = ty.kind {
494494
let source_map = self.tcx.sess.source_map();
495-
let hir_id = match self.tcx.hir().as_local_hir_id(def_id) {
496-
Some(hir_id) => hir_id,
497-
None => return false,
498-
};
499495
if !self.tcx.has_typeck_tables(def_id) {
500496
return false;
501497
}
502-
let fn_sig = {
503-
match self.tcx.typeck_tables_of(def_id).liberated_fn_sigs().get(hir_id) {
504-
Some(f) => *f,
505-
None => {
506-
bug!("No fn-sig entry for def_id={:?}", def_id);
507-
}
508-
}
509-
};
498+
// We're emitting a suggestion, so we can just ignore regions
499+
let fn_sig = *self.tcx.fn_sig(def_id).skip_binder();
510500

511501
let other_ty = if let FnDef(def_id, _) = other_ty.kind {
512-
let hir_id = match self.tcx.hir().as_local_hir_id(def_id) {
513-
Some(hir_id) => hir_id,
514-
None => return false,
515-
};
516502
if !self.tcx.has_typeck_tables(def_id) {
517503
return false;
518504
}
519-
match self.tcx.typeck_tables_of(def_id).liberated_fn_sigs().get(hir_id) {
520-
Some(f) => f.clone().output(),
521-
None => {
522-
bug!("No fn-sig entry for def_id={:?}", def_id);
523-
}
524-
}
505+
// We're emitting a suggestion, so we can just ignore regions
506+
self.tcx.fn_sig(def_id).skip_binder().output()
525507
} else {
526508
other_ty
527509
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
fn first() {
2+
second == 1 //~ ERROR binary operation
3+
//~^ ERROR mismatched types
4+
}
5+
6+
fn second() {
7+
first == 1 //~ ERROR binary operation
8+
//~^ ERROR mismatched types
9+
}
10+
11+
fn bar() {
12+
bar == 1 //~ ERROR binary operation
13+
//~^ ERROR mismatched types
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
error[E0369]: binary operation `==` cannot be applied to type `fn() {second}`
2+
--> $DIR/issue-66667-function-cmp-cycle.rs:2:12
3+
|
4+
LL | second == 1
5+
| ------ ^^ - {integer}
6+
| |
7+
| fn() {second}
8+
9+
error[E0308]: mismatched types
10+
--> $DIR/issue-66667-function-cmp-cycle.rs:2:15
11+
|
12+
LL | second == 1
13+
| ^ expected fn item, found integer
14+
|
15+
= note: expected fn item `fn() {second}`
16+
found type `{integer}`
17+
18+
error[E0369]: binary operation `==` cannot be applied to type `fn() {first}`
19+
--> $DIR/issue-66667-function-cmp-cycle.rs:7:11
20+
|
21+
LL | first == 1
22+
| ----- ^^ - {integer}
23+
| |
24+
| fn() {first}
25+
26+
error[E0308]: mismatched types
27+
--> $DIR/issue-66667-function-cmp-cycle.rs:7:14
28+
|
29+
LL | first == 1
30+
| ^ expected fn item, found integer
31+
|
32+
= note: expected fn item `fn() {first}`
33+
found type `{integer}`
34+
35+
error[E0369]: binary operation `==` cannot be applied to type `fn() {bar}`
36+
--> $DIR/issue-66667-function-cmp-cycle.rs:12:9
37+
|
38+
LL | bar == 1
39+
| --- ^^ - {integer}
40+
| |
41+
| fn() {bar}
42+
43+
error[E0308]: mismatched types
44+
--> $DIR/issue-66667-function-cmp-cycle.rs:12:12
45+
|
46+
LL | bar == 1
47+
| ^ expected fn item, found integer
48+
|
49+
= note: expected fn item `fn() {bar}`
50+
found type `{integer}`
51+
52+
error: aborting due to 6 previous errors
53+
54+
Some errors have detailed explanations: E0308, E0369.
55+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)