Skip to content

Commit 5ce5e08

Browse files
committed
Auto merge of #53588 - tristanburgess:52985_diagnostics_no_concrete_type_behind_existential_type, r=oli-obk
52985 diagnostics no concrete type behind existential type @oli-obk FYI. See below for new cycle error generated. ```rust error[E0391]: cycle detected when processing `Foo` --> /dev/staging/existential_type_no_concrete_type_nouse_potential.rs:3:1 | 3 | existential type Foo: Copy; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: ...which requires processing `bar`... --> /dev/staging/existential_type_no_concrete_type_nouse_potential.rs:6:23 | 6 | fn bar(x: Foo) -> Foo { | _______________________^ 7 | | x 8 | | } | |_^ = note: ...which again requires processing `Foo`, completing the cycle error: aborting due to previous error For more information about this error, try `rustc --explain E0391`. ```
2 parents 54d82d0 + 7440125 commit 5ce5e08

File tree

4 files changed

+95
-66
lines changed

4 files changed

+95
-66
lines changed

src/librustc/traits/query/normalize.rs

+8-30
Original file line numberDiff line numberDiff line change
@@ -121,36 +121,14 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
121121
let concrete_ty = generic_ty.subst(self.tcx(), substs);
122122
self.anon_depth += 1;
123123
if concrete_ty == ty {
124-
// The type in question can only be inferred in terms of itself. This
125-
// is likely a user code issue, not a compiler issue. Thus, we will
126-
// induce a cycle error by calling the parent query again on the type.
127-
//
128-
// FIXME: Perhaps a better solution would be to have fold_ty()
129-
// itself be a query. Then, a type fold cycle would be detected
130-
// and reported more naturally as part of the query system, rather
131-
// than forcing it here.
132-
//
133-
// FIXME: Need a better span than just one pointing to the type def.
134-
// Should point to a defining use of the type that results in this
135-
// un-normalizable state.
136-
if let Some(param_env_lifted) =
137-
self.tcx().lift_to_global(&self.param_env)
138-
{
139-
if let Some(ty_lifted) = self.tcx().lift_to_global(&concrete_ty) {
140-
let span = self.tcx().def_span(def_id);
141-
self.tcx()
142-
.global_tcx()
143-
.at(span)
144-
.normalize_ty_after_erasing_regions(
145-
param_env_lifted.and(ty_lifted),
146-
);
147-
self.tcx().sess.abort_if_errors();
148-
}
149-
}
150-
// If a cycle error can't be emitted, indicate a NoSolution error
151-
// and let the caller handle it.
152-
self.error = true;
153-
return concrete_ty;
124+
bug!(
125+
"infinite recursion generic_ty: {:#?}, substs: {:#?}, \
126+
concrete_ty: {:#?}, ty: {:#?}",
127+
generic_ty,
128+
substs,
129+
concrete_ty,
130+
ty
131+
);
154132
}
155133
let folded_ty = self.fold_ty(concrete_ty);
156134
self.anon_depth -= 1;

src/librustc_typeck/check/writeback.rs

+49-34
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ use rustc::hir;
1717
use rustc::hir::def_id::{DefId, DefIndex};
1818
use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
1919
use rustc::infer::InferCtxt;
20+
use rustc::ty::adjustment::{Adjust, Adjustment};
21+
use rustc::ty::fold::{BottomUpFolder, TypeFoldable, TypeFolder};
2022
use rustc::ty::subst::UnpackedKind;
2123
use rustc::ty::{self, Ty, TyCtxt};
22-
use rustc::ty::adjustment::{Adjust, Adjustment};
23-
use rustc::ty::fold::{TypeFoldable, TypeFolder, BottomUpFolder};
2424
use rustc::util::nodemap::DefIdSet;
25+
use rustc_data_structures::sync::Lrc;
26+
use std::mem;
2527
use syntax::ast;
2628
use syntax_pos::Span;
27-
use std::mem;
28-
use rustc_data_structures::sync::Lrc;
2929

3030
///////////////////////////////////////////////////////////////////////////
3131
// Entry point
@@ -55,17 +55,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
5555
);
5656
debug!(
5757
"used_trait_imports({:?}) = {:?}",
58-
item_def_id,
59-
used_trait_imports
58+
item_def_id, used_trait_imports
6059
);
6160
wbcx.tables.used_trait_imports = used_trait_imports;
6261

6362
wbcx.tables.tainted_by_errors = self.is_tainted_by_errors();
6463

6564
debug!(
6665
"writeback: tables for {:?} are {:#?}",
67-
item_def_id,
68-
wbcx.tables
66+
item_def_id, wbcx.tables
6967
);
7068

7169
self.tcx.alloc_tables(wbcx.tables)
@@ -118,8 +116,8 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
118116
// operating on scalars, we clear the overload.
119117
fn fix_scalar_builtin_expr(&mut self, e: &hir::Expr) {
120118
match e.node {
121-
hir::ExprKind::Unary(hir::UnNeg, ref inner) |
122-
hir::ExprKind::Unary(hir::UnNot, ref inner) => {
119+
hir::ExprKind::Unary(hir::UnNeg, ref inner)
120+
| hir::ExprKind::Unary(hir::UnNot, ref inner) => {
123121
let inner_ty = self.fcx.node_ty(inner.hir_id);
124122
let inner_ty = self.fcx.resolve_type_vars_if_possible(&inner_ty);
125123

@@ -178,8 +176,7 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
178176
let index_ty = tables.expr_ty_adjusted(&index);
179177
let index_ty = self.fcx.resolve_type_vars_if_possible(&index_ty);
180178

181-
if base_ty.builtin_index().is_some()
182-
&& index_ty == self.fcx.tcx.types.usize {
179+
if base_ty.builtin_index().is_some() && index_ty == self.fcx.tcx.types.usize {
183180
// Remove the method call record
184181
tables.type_dependent_defs_mut().remove(e.hir_id);
185182
tables.node_substs_mut().remove(e.hir_id);
@@ -191,24 +188,26 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
191188
// of size information - we need to get rid of it
192189
// Since this is "after" the other adjustment to be
193190
// discarded, we do an extra `pop()`
194-
Some(Adjustment { kind: Adjust::Unsize, .. }) => {
191+
Some(Adjustment {
192+
kind: Adjust::Unsize,
193+
..
194+
}) => {
195195
// So the borrow discard actually happens here
196196
a.pop();
197-
},
197+
}
198198
_ => {}
199199
}
200200
});
201201
}
202-
},
202+
}
203203
// Might encounter non-valid indexes at this point, so there
204204
// has to be a fall-through
205-
_ => {},
205+
_ => {}
206206
}
207207
}
208208
}
209209
}
210210

211-
212211
///////////////////////////////////////////////////////////////////////////
213212
// Impl of Visitor for Resolver
214213
//
@@ -262,7 +261,9 @@ impl<'cx, 'gcx, 'tcx> Visitor<'gcx> for WritebackCx<'cx, 'gcx, 'tcx> {
262261
if let Some(&bm) = self.fcx.tables.borrow().pat_binding_modes().get(p.hir_id) {
263262
self.tables.pat_binding_modes_mut().insert(p.hir_id, bm);
264263
} else {
265-
self.tcx().sess.delay_span_bug(p.span, "missing binding mode");
264+
self.tcx()
265+
.sess
266+
.delay_span_bug(p.span, "missing binding mode");
266267
}
267268
}
268269
hir::PatKind::Struct(_, ref fields, _) => {
@@ -310,8 +311,7 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
310311
};
311312
debug!(
312313
"Upvar capture for {:?} resolved to {:?}",
313-
upvar_id,
314-
new_upvar_capture
314+
upvar_id, new_upvar_capture
315315
);
316316
self.tables
317317
.upvar_capture_map
@@ -425,8 +425,7 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
425425
if subst == ty {
426426
// found it in the substitution list, replace with the
427427
// parameter from the existential type
428-
return self
429-
.tcx()
428+
return self.tcx()
430429
.global_tcx()
431430
.mk_ty_param(param.index, param.name);
432431
}
@@ -464,14 +463,16 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
464463
name: p.name,
465464
};
466465
trace!("replace {:?} with {:?}", region, reg);
467-
return self.tcx().global_tcx()
466+
return self.tcx()
467+
.global_tcx()
468468
.mk_region(ty::ReEarlyBound(reg));
469469
}
470470
}
471471
}
472472
trace!("anon_defn: {:#?}", anon_defn);
473473
trace!("generics: {:#?}", generics);
474-
self.tcx().sess
474+
self.tcx()
475+
.sess
475476
.struct_span_err(
476477
span,
477478
"non-defining existential type use in defining scope",
@@ -480,25 +481,35 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
480481
span,
481482
format!(
482483
"lifetime `{}` is part of concrete type but not used \
483-
in parameter list of existential type",
484+
in parameter list of existential type",
484485
region,
485486
),
486487
)
487488
.emit();
488489
self.tcx().global_tcx().mk_region(ty::ReStatic)
489490
}
490491
}
491-
}
492+
},
492493
})
493494
};
494495

495-
let old = self.tables.concrete_existential_types.insert(def_id, definition_ty);
496+
if let ty::Anon(defin_ty_def_id, _substs) = definition_ty.sty {
497+
if def_id == defin_ty_def_id {
498+
// Concrete type resolved to the existential type itself
499+
// Force a cycle error
500+
self.tcx().at(span).type_of(defin_ty_def_id);
501+
}
502+
}
503+
504+
let old = self.tables
505+
.concrete_existential_types
506+
.insert(def_id, definition_ty);
496507
if let Some(old) = old {
497508
if old != definition_ty {
498509
span_bug!(
499510
span,
500511
"visit_anon_types tried to write \
501-
different types for the same existential type: {:?}, {:?}, {:?}",
512+
different types for the same existential type: {:?}, {:?}, {:?}",
502513
def_id,
503514
definition_ty,
504515
old,
@@ -510,7 +521,12 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
510521

511522
fn visit_field_id(&mut self, node_id: ast::NodeId) {
512523
let hir_id = self.tcx().hir.node_to_hir_id(node_id);
513-
if let Some(index) = self.fcx.tables.borrow_mut().field_indices_mut().remove(hir_id) {
524+
if let Some(index) = self.fcx
525+
.tables
526+
.borrow_mut()
527+
.field_indices_mut()
528+
.remove(hir_id)
529+
{
514530
self.tables.field_indices_mut().insert(hir_id, index);
515531
}
516532
}
@@ -559,8 +575,7 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
559575
let resolved_adjustment = self.resolve(&adjustment, &span);
560576
debug!(
561577
"Adjustments for node {:?}: {:?}",
562-
hir_id,
563-
resolved_adjustment
578+
hir_id, resolved_adjustment
564579
);
565580
self.tables
566581
.adjustments_mut()
@@ -584,8 +599,7 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
584599
let resolved_adjustment = self.resolve(&adjustment, &span);
585600
debug!(
586601
"pat_adjustments for node {:?}: {:?}",
587-
hir_id,
588-
resolved_adjustment
602+
hir_id, resolved_adjustment
589603
);
590604
self.tables
591605
.pat_adjustments_mut()
@@ -701,7 +715,8 @@ impl<'cx, 'gcx, 'tcx> Resolver<'cx, 'gcx, 'tcx> {
701715
fn report_error(&self, t: Ty<'tcx>) {
702716
if !self.tcx.sess.has_errors() {
703717
self.infcx
704-
.need_type_info_err(Some(self.body.id()), self.span.to_span(&self.tcx), t).emit();
718+
.need_type_info_err(Some(self.body.id()), self.span.to_span(&self.tcx), t)
719+
.emit();
705720
}
706721
}
707722
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(existential_type)]
12+
// compile-pass
13+
mod my_mod {
14+
use std::fmt::Debug;
15+
16+
pub existential type Foo: Debug;
17+
pub existential type Foot: Debug;
18+
19+
pub fn get_foo() -> Foo {
20+
5i32
21+
}
22+
23+
pub fn get_foot() -> Foot {
24+
get_foo()
25+
}
26+
}
27+
28+
fn main() {
29+
let _: my_mod::Foot = my_mod::get_foot();
30+
}
31+

src/test/ui/existential_types/no_inferrable_concrete_type.stderr

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
error[E0391]: cycle detected when normalizing `ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: All }, value: Foo }`
1+
error[E0391]: cycle detected when processing `Foo`
22
--> $DIR/no_inferrable_concrete_type.rs:16:1
33
|
44
LL | existential type Foo: Copy; //~ cycle detected
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: ...which again requires normalizing `ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: All }, value: Foo }`, completing the cycle
7+
note: ...which requires processing `bar`...
8+
--> $DIR/no_inferrable_concrete_type.rs:19:23
9+
|
10+
LL | fn bar(x: Foo) -> Foo { x }
11+
| ^^^^^
12+
= note: ...which again requires processing `Foo`, completing the cycle
813

914
error: aborting due to previous error
1015

0 commit comments

Comments
 (0)