Skip to content

Commit ff65bff

Browse files
committed
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.
1 parent c20d7ee commit ff65bff

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
@@ -491,36 +491,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
491491
err.span_label(span, ty.to_string());
492492
if let FnDef(def_id, _) = ty.kind {
493493
let source_map = self.tcx.sess.source_map();
494-
let hir_id = match self.tcx.hir().as_local_hir_id(def_id) {
495-
Some(hir_id) => hir_id,
496-
None => return false,
497-
};
498494
if !self.tcx.has_typeck_tables(def_id) {
499495
return false;
500496
}
501-
let fn_sig = {
502-
match self.tcx.typeck_tables_of(def_id).liberated_fn_sigs().get(hir_id) {
503-
Some(f) => *f,
504-
None => {
505-
bug!("No fn-sig entry for def_id={:?}", def_id);
506-
}
507-
}
508-
};
497+
// We're emitting a suggestion, so we can just ignore regions
498+
let fn_sig = *self.tcx.fn_sig(def_id).skip_binder();
509499

510500
let other_ty = if let FnDef(def_id, _) = other_ty.kind {
511-
let hir_id = match self.tcx.hir().as_local_hir_id(def_id) {
512-
Some(hir_id) => hir_id,
513-
None => return false,
514-
};
515501
if !self.tcx.has_typeck_tables(def_id) {
516502
return false;
517503
}
518-
match self.tcx.typeck_tables_of(def_id).liberated_fn_sigs().get(hir_id) {
519-
Some(f) => f.clone().output(),
520-
None => {
521-
bug!("No fn-sig entry for def_id={:?}", def_id);
522-
}
523-
}
504+
// We're emitting a suggestion, so we can just ignore regions
505+
self.tcx.fn_sig(def_id).skip_binder().output()
524506
} else {
525507
other_ty
526508
};
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)