summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2020-03-27 01:23:49 +0100
committerGitHub <noreply@github.com>2020-03-27 01:23:49 +0100
commitf635c3757b61d1d8aefa86e1d3acdd7d4dea90f8 (patch)
treebb74d25bb9075d792b67a5f8b5132e792773b963
parentRollup merge of #69644 - ecstatic-morse:unified-dataflow-cleanup, r=eddyb (diff)
parentFix cycle error when emitting suggestion for mismatched `fn` type (diff)
downloadrust-f635c3757b61d1d8aefa86e1d3acdd7d4dea90f8.tar.gz
rust-f635c3757b61d1d8aefa86e1d3acdd7d4dea90f8.tar.bz2
rust-f635c3757b61d1d8aefa86e1d3acdd7d4dea90f8.tar.xz
Rollup merge of #69936 - Aaron1011:fix/suggestion-cycle, r=varkor
Fix cycle error when emitting suggestion for mismatched `fn` type Fixes #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.
-rw-r--r--src/librustc_typeck/check/op.rs26
-rw-r--r--src/test/ui/issues/issue-66667-function-cmp-cycle.rs16
-rw-r--r--src/test/ui/issues/issue-66667-function-cmp-cycle.stderr55
3 files changed, 75 insertions, 22 deletions
diff --git a/src/librustc_typeck/check/op.rs b/src/librustc_typeck/check/op.rs
index f589805..657926a 100644
--- a/src/librustc_typeck/check/op.rs
+++ b/src/librustc_typeck/check/op.rs
@@ -492,36 +492,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
492 err.span_label(span, ty.to_string()); 492 err.span_label(span, ty.to_string());
493 if let FnDef(def_id, _) = ty.kind { 493 if let FnDef(def_id, _) = ty.kind {
494 let source_map = self.tcx.sess.source_map(); 494 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 };
499 if !self.tcx.has_typeck_tables(def_id) { 495 if !self.tcx.has_typeck_tables(def_id) {
500 return false; 496 return false;
501 } 497 }
502 let fn_sig = { 498 // We're emitting a suggestion, so we can just ignore regions
503 match self.tcx.typeck_tables_of(def_id).liberated_fn_sigs().get(hir_id) { 499 let fn_sig = *self.tcx.fn_sig(def_id).skip_binder();
504 Some(f) => *f,
505 None => {
506 bug!("No fn-sig entry for def_id={:?}", def_id);
507 }
508 }
509 };
510 500
511 let other_ty = if let FnDef(def_id, _) = other_ty.kind { 501 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 };
516 if !self.tcx.has_typeck_tables(def_id) { 502 if !self.tcx.has_typeck_tables(def_id) {
517 return false; 503 return false;
518 } 504 }
519 match self.tcx.typeck_tables_of(def_id).liberated_fn_sigs().get(hir_id) { 505 // We're emitting a suggestion, so we can just ignore regions
520 Some(f) => f.clone().output(), 506 self.tcx.fn_sig(def_id).skip_binder().output()
521 None => {
522 bug!("No fn-sig entry for def_id={:?}", def_id);
523 }
524 }
525 } else { 507 } else {
526 other_ty 508 other_ty
527 }; 509 };
diff --git a/src/test/ui/issues/issue-66667-function-cmp-cycle.rs b/src/test/ui/issues/issue-66667-function-cmp-cycle.rs
new file mode 100644
index 0000000..7b025be
--- /dev/null
+++ b/src/test/ui/issues/issue-66667-function-cmp-cycle.rs
@@ -0,0 +1,16 @@
1fn first() {
2 second == 1 //~ ERROR binary operation
3 //~^ ERROR mismatched types
4}
5
6fn second() {
7 first == 1 //~ ERROR binary operation
8 //~^ ERROR mismatched types
9}
10
11fn bar() {
12 bar == 1 //~ ERROR binary operation
13 //~^ ERROR mismatched types
14}
15
16fn main() {}
diff --git a/src/test/ui/issues/issue-66667-function-cmp-cycle.stderr b/src/test/ui/issues/issue-66667-function-cmp-cycle.stderr
new file mode 100644
index 0000000..887699e
--- /dev/null
+++ b/src/test/ui/issues/issue-66667-function-cmp-cycle.stderr
@@ -0,0 +1,55 @@
1error[E0369]: binary operation `==` cannot be applied to type `fn() {second}`
2 --> $DIR/issue-66667-function-cmp-cycle.rs:2:12
3 |
4LL | second == 1
5 | ------ ^^ - {integer}
6 | |
7 | fn() {second}
8
9error[E0308]: mismatched types
10 --> $DIR/issue-66667-function-cmp-cycle.rs:2:15
11 |
12LL | second == 1
13 | ^ expected fn item, found integer
14 |
15 = note: expected fn item `fn() {second}`
16 found type `{integer}`
17
18error[E0369]: binary operation `==` cannot be applied to type `fn() {first}`
19 --> $DIR/issue-66667-function-cmp-cycle.rs:7:11
20 |
21LL | first == 1
22 | ----- ^^ - {integer}
23 | |
24 | fn() {first}
25
26error[E0308]: mismatched types
27 --> $DIR/issue-66667-function-cmp-cycle.rs:7:14
28 |
29LL | first == 1
30 | ^ expected fn item, found integer
31 |
32 = note: expected fn item `fn() {first}`
33 found type `{integer}`
34
35error[E0369]: binary operation `==` cannot be applied to type `fn() {bar}`
36 --> $DIR/issue-66667-function-cmp-cycle.rs:12:9
37 |
38LL | bar == 1
39 | --- ^^ - {integer}
40 | |
41 | fn() {bar}
42
43error[E0308]: mismatched types
44 --> $DIR/issue-66667-function-cmp-cycle.rs:12:12
45 |
46LL | bar == 1
47 | ^ expected fn item, found integer
48 |
49 = note: expected fn item `fn() {bar}`
50 found type `{integer}`
51
52error: aborting due to 6 previous errors
53
54Some errors have detailed explanations: E0308, E0369.
55For more information about an error, try `rustc --explain E0308`.