Skip to content

Commit 78ff609

Browse files
committed
Auto merge of #55152 - nikomatsakis:nll-issue-54571-type-annot-in-constants, r=pnkfelix
support type annot in constants, casts Fixes #54571 Fixes #54332 Fixes #55183 r? @pnkfelix
2 parents 74ff7dc + 9a7bb0e commit 78ff609

29 files changed

+553
-276
lines changed

src/librustc/ich/impls_mir.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,7 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for mir::UserTypeAnnotation<
597597
mir::UserTypeAnnotation::Ty(ref ty) => {
598598
ty.hash_stable(hcx, hasher);
599599
}
600-
mir::UserTypeAnnotation::FnDef(ref def_id, ref substs) => {
601-
def_id.hash_stable(hcx, hasher);
602-
substs.hash_stable(hcx, hasher);
603-
}
604-
mir::UserTypeAnnotation::AdtDef(ref def_id, ref substs) => {
600+
mir::UserTypeAnnotation::TypeOf(ref def_id, ref substs) => {
605601
def_id.hash_stable(hcx, hasher);
606602
substs.hash_stable(hcx, hasher);
607603
}

src/librustc/infer/canonical/query_response.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
use infer::canonical::substitute::substitute_value;
2121
use infer::canonical::{
22-
Canonical, CanonicalVarKind, CanonicalVarValues, CanonicalizedQueryResponse, Certainty,
22+
Canonical, CanonicalVarValues, CanonicalizedQueryResponse, Certainty,
2323
OriginalQueryValues, QueryRegionConstraint, QueryResponse,
2424
};
2525
use infer::region_constraints::{Constraint, RegionConstraintData};
@@ -262,13 +262,6 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
262262
where
263263
R: Debug + TypeFoldable<'tcx>,
264264
{
265-
// In an NLL query, there should be no type variables in the
266-
// query, only region variables.
267-
debug_assert!(query_response.variables.iter().all(|v| match v.kind {
268-
CanonicalVarKind::Ty(_) => false,
269-
CanonicalVarKind::Region => true,
270-
}));
271-
272265
let result_subst =
273266
self.query_response_substitution_guess(cause, original_values, query_response);
274267

src/librustc/infer/region_constraints/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub struct Verify<'tcx> {
149149
pub bound: VerifyBound<'tcx>,
150150
}
151151

152-
#[derive(Copy, Clone, PartialEq, Eq)]
152+
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
153153
pub enum GenericKind<'tcx> {
154154
Param(ty::ParamTy),
155155
Projection(ty::ProjectionTy<'tcx>),

src/librustc/mir/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -2425,15 +2425,16 @@ pub struct Constant<'tcx> {
24252425
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
24262426
pub enum UserTypeAnnotation<'tcx> {
24272427
Ty(CanonicalTy<'tcx>),
2428-
FnDef(DefId, CanonicalUserSubsts<'tcx>),
2429-
AdtDef(&'tcx AdtDef, CanonicalUserSubsts<'tcx>),
2428+
2429+
/// The canonical type is the result of `type_of(def_id)` with the
2430+
/// given substitutions applied.
2431+
TypeOf(DefId, CanonicalUserSubsts<'tcx>),
24302432
}
24312433

24322434
EnumTypeFoldableImpl! {
24332435
impl<'tcx> TypeFoldable<'tcx> for UserTypeAnnotation<'tcx> {
24342436
(UserTypeAnnotation::Ty)(ty),
2435-
(UserTypeAnnotation::FnDef)(def, substs),
2436-
(UserTypeAnnotation::AdtDef)(def, substs),
2437+
(UserTypeAnnotation::TypeOf)(def, substs),
24372438
}
24382439
}
24392440

src/librustc_mir/borrow_check/nll/region_infer/mod.rs

+30-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc::mir::{
2525
use rustc::ty::{self, RegionVid, Ty, TyCtxt, TypeFoldable};
2626
use rustc::util::common;
2727
use rustc_data_structures::bit_set::BitSet;
28-
use rustc_data_structures::fx::FxHashMap;
28+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2929
use rustc_data_structures::graph::scc::Sccs;
3030
use rustc_data_structures::indexed_vec::IndexVec;
3131
use rustc_errors::{Diagnostic, DiagnosticBuilder};
@@ -67,10 +67,8 @@ pub struct RegionInferenceContext<'tcx> {
6767
constraint_sccs: Rc<Sccs<RegionVid, ConstraintSccIndex>>,
6868

6969
/// Map closure bounds to a `Span` that should be used for error reporting.
70-
closure_bounds_mapping: FxHashMap<
71-
Location,
72-
FxHashMap<(RegionVid, RegionVid), (ConstraintCategory, Span)>,
73-
>,
70+
closure_bounds_mapping:
71+
FxHashMap<Location, FxHashMap<(RegionVid, RegionVid), (ConstraintCategory, Span)>>,
7472

7573
/// Contains the minimum universe of any variable within the same
7674
/// SCC. We will ensure that no SCC contains values that are not
@@ -580,6 +578,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
580578
) {
581579
let tcx = infcx.tcx;
582580

581+
// Sometimes we register equivalent type-tests that would
582+
// result in basically the exact same error being reported to
583+
// the user. Avoid that.
584+
let mut deduplicate_errors = FxHashSet::default();
585+
583586
for type_test in &self.type_tests {
584587
debug!("check_type_test: {:?}", type_test);
585588

@@ -605,11 +608,31 @@ impl<'tcx> RegionInferenceContext<'tcx> {
605608
}
606609
}
607610

608-
// Oh the humanity. Obviously we will do better than this error eventually.
611+
// Type-test failed. Report the error.
612+
613+
// Try to convert the lower-bound region into something named we can print for the user.
609614
let lower_bound_region = self.to_error_region(type_test.lower_bound);
615+
616+
// Skip duplicate-ish errors.
617+
let type_test_span = type_test.locations.span(mir);
618+
let erased_generic_kind = tcx.erase_regions(&type_test.generic_kind);
619+
if !deduplicate_errors.insert((
620+
erased_generic_kind,
621+
lower_bound_region,
622+
type_test.locations,
623+
)) {
624+
continue;
625+
} else {
626+
debug!(
627+
"check_type_test: reporting error for erased_generic_kind={:?}, \
628+
lower_bound_region={:?}, \
629+
type_test.locations={:?}",
630+
erased_generic_kind, lower_bound_region, type_test.locations,
631+
);
632+
}
633+
610634
if let Some(lower_bound_region) = lower_bound_region {
611635
let region_scope_tree = &tcx.region_scope_tree(mir_def_id);
612-
let type_test_span = type_test.locations.span(mir);
613636
infcx
614637
.construct_generic_bound_failure(
615638
region_scope_tree,
@@ -629,7 +652,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
629652
// to report it; we could probably handle it by
630653
// iterating over the universal regions and reporting
631654
// an error that multiple bounds are required.
632-
let type_test_span = type_test.locations.span(mir);
633655
tcx.sess
634656
.struct_span_err(
635657
type_test_span,

src/librustc_mir/borrow_check/nll/type_check/constraint_conversion.rs

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ impl<'a, 'gcx, 'tcx> ConstraintConversion<'a, 'gcx, 'tcx> {
142142
}
143143

144144
fn add_type_test(&mut self, type_test: TypeTest<'tcx>) {
145+
debug!("add_type_test(type_test={:?})", type_test);
145146
self.type_tests.push(type_test);
146147
}
147148
}

0 commit comments

Comments
 (0)