Skip to content

Commit 028d293

Browse files
Deeply normalize when processing registered region obligations
1 parent e11a6a9 commit 028d293

File tree

17 files changed

+90
-12
lines changed

17 files changed

+90
-12
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_middle::ty::{
2020
};
2121
use rustc_middle::ty::{GenericParamDefKind, TyCtxt};
2222
use rustc_span::Span;
23+
use rustc_trait_selection::regions::InferCtxtRegionExt;
2324
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
2425
use rustc_trait_selection::traits::outlives_bounds::InferCtxtExt as _;
2526
use rustc_trait_selection::traits::{

compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_middle::ty::{
88
self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperVisitable, TypeVisitable, TypeVisitor,
99
};
1010
use rustc_span::Span;
11+
use rustc_trait_selection::regions::InferCtxtRegionExt;
1112
use rustc_trait_selection::traits::{
1213
elaborate, normalize_param_env_or_error, outlives_bounds::InferCtxtExt, ObligationCtxt,
1314
};

compiler/rustc_hir_analysis/src/check/dropck.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_infer::infer::{RegionResolutionError, TyCtxtInferExt};
88
use rustc_middle::ty::util::CheckRegions;
99
use rustc_middle::ty::GenericArgsRef;
1010
use rustc_middle::ty::{self, TyCtxt};
11+
use rustc_trait_selection::regions::InferCtxtRegionExt;
1112
use rustc_trait_selection::traits::{self, ObligationCtxt};
1213

1314
use crate::errors;
@@ -188,6 +189,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
188189
RegionResolutionError::UpperBoundUniverseConflict(a, _, _, _, b) => {
189190
format!("{b}: {a}", a = ty::Region::new_var(tcx, a))
190191
}
192+
RegionResolutionError::CannotNormalize(..) => todo!(),
191193
};
192194
guar = Some(
193195
struct_span_code_err!(

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_session::parse::feature_err;
2424
use rustc_span::symbol::{sym, Ident, Symbol};
2525
use rustc_span::{Span, DUMMY_SP};
2626
use rustc_target::spec::abi::Abi;
27+
use rustc_trait_selection::regions::InferCtxtRegionExt;
2728
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
2829
use rustc_trait_selection::traits::misc::{
2930
type_allowed_to_implement_const_param_ty, ConstParamTyImplementationError,

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
518518

519519
self.report_placeholder_failure(sup_origin, sub_r, sup_r).emit();
520520
}
521+
522+
RegionResolutionError::CannotNormalize(ty, origin) => {
523+
self.tcx
524+
.dcx()
525+
.struct_span_err(origin.span(), format!("cannot normalize `{ty}`"))
526+
.emit();
527+
}
521528
}
522529
}
523530
}
@@ -559,7 +566,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
559566
RegionResolutionError::GenericBoundFailure(..) => true,
560567
RegionResolutionError::ConcreteFailure(..)
561568
| RegionResolutionError::SubSupConflict(..)
562-
| RegionResolutionError::UpperBoundUniverseConflict(..) => false,
569+
| RegionResolutionError::UpperBoundUniverseConflict(..)
570+
| RegionResolutionError::CannotNormalize(..) => false,
563571
};
564572

565573
let mut errors = if errors.iter().all(|e| is_bound_failure(e)) {
@@ -574,6 +582,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
574582
RegionResolutionError::GenericBoundFailure(ref sro, _, _) => sro.span(),
575583
RegionResolutionError::SubSupConflict(_, ref rvo, _, _, _, _, _) => rvo.span(),
576584
RegionResolutionError::UpperBoundUniverseConflict(_, ref rvo, _, _, _) => rvo.span(),
585+
RegionResolutionError::CannotNormalize(_, ref sro) => sro.span(),
577586
});
578587
errors
579588
}

compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ pub enum RegionResolutionError<'tcx> {
9898
SubregionOrigin<'tcx>, // cause of the constraint
9999
Region<'tcx>, // the placeholder `'b`
100100
),
101+
102+
CannotNormalize(Ty<'tcx>, SubregionOrigin<'tcx>),
101103
}
102104

103105
impl<'tcx> RegionResolutionError<'tcx> {
@@ -106,7 +108,8 @@ impl<'tcx> RegionResolutionError<'tcx> {
106108
RegionResolutionError::ConcreteFailure(origin, _, _)
107109
| RegionResolutionError::GenericBoundFailure(origin, _, _)
108110
| RegionResolutionError::SubSupConflict(_, _, origin, _, _, _, _)
109-
| RegionResolutionError::UpperBoundUniverseConflict(_, _, _, origin, _) => origin,
111+
| RegionResolutionError::UpperBoundUniverseConflict(_, _, _, origin, _)
112+
| RegionResolutionError::CannotNormalize(_, origin) => origin,
110113
}
111114
}
112115
}

compiler/rustc_infer/src/infer/outlives/mod.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::{InferCtxt, RegionResolutionError};
55
use crate::infer::free_regions::RegionRelations;
66
use crate::infer::lexical_region_resolve;
77
use rustc_middle::traits::query::OutlivesBound;
8-
use rustc_middle::ty;
8+
use rustc_middle::ty::{self, Ty};
99

1010
pub mod components;
1111
pub mod env;
@@ -41,12 +41,22 @@ impl<'tcx> InferCtxt<'tcx> {
4141
/// result. After this, no more unification operations should be
4242
/// done -- or the compiler will panic -- but it is legal to use
4343
/// `resolve_vars_if_possible` as well as `fully_resolve`.
44+
///
45+
/// If you are in a crate that has access to `rustc_trai_selection`,
46+
/// then it's probably better to use `resolve_regions_normalizing_outlives_obligations`,
47+
/// which knows how to normalize registered region obligations.
4448
#[must_use]
45-
pub fn resolve_regions(
49+
pub fn resolve_regions_with_normalize(
4650
&self,
4751
outlives_env: &OutlivesEnvironment<'tcx>,
52+
deeply_normalize_ty: impl Fn(Ty<'tcx>) -> Result<Ty<'tcx>, Ty<'tcx>>,
4853
) -> Vec<RegionResolutionError<'tcx>> {
49-
self.process_registered_region_obligations(outlives_env);
54+
match self.process_registered_region_obligations(outlives_env, deeply_normalize_ty) {
55+
Ok(()) => {}
56+
Err((ty, origin)) => {
57+
return vec![RegionResolutionError::CannotNormalize(ty, origin)];
58+
}
59+
};
5060

5161
let (var_infos, data) = {
5262
let mut inner = self.inner.borrow_mut();

compiler/rustc_infer/src/infer/outlives/obligations.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,19 @@ impl<'tcx> InferCtxt<'tcx> {
123123
/// flow of the inferencer. The key point is that it is
124124
/// invoked after all type-inference variables have been bound --
125125
/// right before lexical region resolution.
126-
#[instrument(level = "debug", skip(self, outlives_env))]
127-
pub fn process_registered_region_obligations(&self, outlives_env: &OutlivesEnvironment<'tcx>) {
126+
#[instrument(level = "debug", skip(self, outlives_env, deeply_normalize_ty))]
127+
pub fn process_registered_region_obligations<E>(
128+
&self,
129+
outlives_env: &OutlivesEnvironment<'tcx>,
130+
mut deeply_normalize_ty: impl FnMut(Ty<'tcx>) -> Result<Ty<'tcx>, E>,
131+
) -> Result<(), (E, SubregionOrigin<'tcx>)> {
128132
assert!(!self.in_snapshot(), "cannot process registered region obligations in a snapshot");
129133

130134
let my_region_obligations = self.take_registered_region_obligations();
131135

132136
for RegionObligation { sup_type, sub_region, origin } in my_region_obligations {
137+
let sup_type = deeply_normalize_ty(sup_type).map_err(|e| (e, origin.clone()))?;
133138
debug!(?sup_type, ?sub_region, ?origin);
134-
let sup_type = self.resolve_vars_if_possible(sup_type);
135139

136140
let outlives = &mut TypeOutlives::new(
137141
self,
@@ -143,6 +147,8 @@ impl<'tcx> InferCtxt<'tcx> {
143147
let category = origin.to_constraint_category();
144148
outlives.type_must_outlive(origin, sup_type, sub_region, category);
145149
}
150+
151+
Ok(())
146152
}
147153
}
148154

compiler/rustc_trait_selection/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extern crate smallvec;
3939

4040
pub mod errors;
4141
pub mod infer;
42+
pub mod regions;
4243
pub mod solve;
4344
pub mod traits;
4445

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
2+
use rustc_infer::infer::{InferCtxt, RegionResolutionError};
3+
use rustc_middle::traits::ObligationCause;
4+
5+
pub trait InferCtxtRegionExt<'tcx> {
6+
/// Resolve regions, using the deep normalizer to normalize any type-outlives
7+
/// obligations in the process. This is in `rustc_trait_selection` because
8+
/// we need to normalize.
9+
///
10+
/// Prefer this method over `resolve_regions_with_normalize`, unless you are
11+
/// doing something specific for normalization.
12+
fn resolve_regions(
13+
&self,
14+
outlives_env: &OutlivesEnvironment<'tcx>,
15+
) -> Vec<RegionResolutionError<'tcx>>;
16+
}
17+
18+
impl<'tcx> InferCtxtRegionExt<'tcx> for InferCtxt<'tcx> {
19+
fn resolve_regions(
20+
&self,
21+
outlives_env: &OutlivesEnvironment<'tcx>,
22+
) -> Vec<RegionResolutionError<'tcx>> {
23+
self.resolve_regions(outlives_env, |ty| {
24+
let ty = self.resolve_vars_if_possible(ty);
25+
26+
if self.next_trait_solver() {
27+
crate::solve::deeply_normalize(
28+
self.at(&ObligationCause::dummy(), outlives_env.param_env),
29+
ty,
30+
)
31+
.map_err(|_| ty)
32+
} else {
33+
Ok(ty)
34+
}
35+
})
36+
}
37+
}

compiler/rustc_trait_selection/src/traits/auto_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
179179
}
180180

181181
let outlives_env = OutlivesEnvironment::new(full_env);
182-
infcx.process_registered_region_obligations(&outlives_env);
182+
let _ = infcx.process_registered_region_obligations::<!>(&outlives_env, |ty| Ok(ty));
183183

184184
let region_data =
185185
infcx.inner.borrow_mut().unwrap_region_constraints().region_constraint_data().clone();

compiler/rustc_trait_selection/src/traits/coherence.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
use crate::infer::outlives::env::OutlivesEnvironment;
88
use crate::infer::InferOk;
9+
use crate::regions::InferCtxtRegionExt;
910
use crate::solve::inspect::{InspectGoal, ProofTreeInferCtxtExt, ProofTreeVisitor};
1011
use crate::solve::{deeply_normalize_for_diagnostics, inspect};
1112
use crate::traits::engine::TraitEngineExt;

compiler/rustc_trait_selection/src/traits/engine.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fmt::Debug;
33

44
use super::FulfillmentContext;
55
use super::TraitEngine;
6+
use crate::regions::InferCtxtRegionExt;
67
use crate::solve::FulfillmentCtxt as NextFulfillmentCtxt;
78
use crate::traits::error_reporting::TypeErrCtxtExt;
89
use crate::traits::NormalizeExt;

compiler/rustc_trait_selection/src/traits/misc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Miscellaneous type-system utilities that are too small to deserve their own modules.
22
3+
use crate::regions::InferCtxtRegionExt;
34
use crate::traits::{self, ObligationCause, ObligationCtxt};
45

56
use hir::LangItem;

compiler/rustc_trait_selection/src/traits/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub mod wf;
2525

2626
use crate::infer::outlives::env::OutlivesEnvironment;
2727
use crate::infer::{InferCtxt, TyCtxtInferExt};
28+
use crate::regions::InferCtxtRegionExt;
2829
use crate::traits::error_reporting::TypeErrCtxtExt as _;
2930
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
3031
use rustc_errors::ErrorGuaranteed;

tests/ui/traits/next-solver/specialization-transmute.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// compile-flags: -Znext-solver
2+
//~^ ERROR cannot normalize `<T as Default>::Id`
23

34
#![feature(specialization)]
45
//~^ WARN the feature `specialization` is incomplete
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: the feature `specialization` is incomplete and may not be safe to use and/or cause compiler crashes
2-
--> $DIR/specialization-transmute.rs:3:12
2+
--> $DIR/specialization-transmute.rs:4:12
33
|
44
LL | #![feature(specialization)]
55
| ^^^^^^^^^^^^^^
@@ -8,12 +8,14 @@ LL | #![feature(specialization)]
88
= help: consider using `min_specialization` instead, which is more stable and complete
99
= note: `#[warn(incomplete_features)]` on by default
1010

11+
error: cannot normalize `<T as Default>::Id`
12+
1113
error[E0282]: type annotations needed
12-
--> $DIR/specialization-transmute.rs:13:23
14+
--> $DIR/specialization-transmute.rs:14:23
1315
|
1416
LL | default type Id = T;
1517
| ^ cannot infer type for associated type `<T as Default>::Id`
1618

17-
error: aborting due to 1 previous error; 1 warning emitted
19+
error: aborting due to 2 previous errors; 1 warning emitted
1820

1921
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)