Skip to content

Commit d0fb57c

Browse files
committed
Make may_use_unstable_feature a free function
1 parent 0ed4c2a commit d0fb57c

File tree

4 files changed

+44
-41
lines changed

4 files changed

+44
-41
lines changed

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::coherence;
2121
use crate::delegate::SolverDelegate;
2222
use crate::placeholder::BoundVarReplacer;
2323
use crate::solve::inspect::{self, ProofTreeBuilder};
24+
use crate::solve::ty::may_use_unstable_feature;
2425
use crate::solve::search_graph::SearchGraph;
2526
use crate::solve::{
2627
CanonicalInput, Certainty, FIXPOINT_STEP_LIMIT, Goal, GoalEvaluation, GoalEvaluationKind,
@@ -1186,7 +1187,7 @@ where
11861187
param_env: I::ParamEnv,
11871188
symbol: I::Symbol,
11881189
) -> bool {
1189-
self.delegate.may_use_unstable_feature(param_env, symbol)
1190+
may_use_unstable_feature(&**self.delegate, param_env, symbol)
11901191
}
11911192
}
11921193

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_middle::bug;
1212
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
1313
use rustc_middle::ty::error::{ExpectedFound, TypeError};
1414
use rustc_middle::ty::{self, Binder, Const, GenericArgsRef, TypeVisitableExt, TypingMode};
15-
use rustc_type_ir::InferCtxtLike;
15+
use rustc_type_ir::may_use_unstable_feature;
1616
use thin_vec::{ThinVec, thin_vec};
1717
use tracing::{debug, debug_span, instrument};
1818

@@ -772,8 +772,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
772772
}
773773
}
774774
ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(symbol)) => {
775-
#[allow(rustc::usage_of_type_ir_traits)]
776-
if self.selcx.infcx.may_use_unstable_feature(obligation.param_env, symbol) {
775+
if may_use_unstable_feature(self.selcx.infcx, obligation.param_env, symbol) {
777776
ProcessResult::Changed(Default::default())
778777
} else {
779778
ProcessResult::Unchanged

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_middle::ty::{
3131
TypeFoldable, TypeVisitableExt, TypingMode, Upcast, elaborate,
3232
};
3333
use rustc_span::{Symbol, sym};
34-
use rustc_type_ir::InferCtxtLike;
34+
use rustc_type_ir::may_use_unstable_feature;
3535
use tracing::{debug, instrument, trace};
3636

3737
use self::EvaluationResult::*;
@@ -834,8 +834,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
834834
}
835835

836836
ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(symbol)) => {
837-
#[allow(rustc::usage_of_type_ir_traits)]
838-
if self.infcx.may_use_unstable_feature(obligation.param_env, symbol) {
837+
if may_use_unstable_feature(self.infcx, obligation.param_env, symbol) {
839838
Ok(EvaluatedToOk)
840839
} else {
841840
Ok(EvaluatedToAmbig)

compiler/rustc_type_ir/src/infer_ctxt.rs

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -285,42 +285,46 @@ pub trait InferCtxtLike: Sized {
285285

286286
fn reset_opaque_types(&self);
287287

288-
fn may_use_unstable_feature(
289-
&self,
290-
param_env: <Self::Interner as Interner>::ParamEnv,
291-
symbol: <Self::Interner as Interner>::Symbol,
292-
) -> bool {
293-
// Iterate through all goals in param_env to find the one that has the same symbol.
294-
for pred in param_env.caller_bounds().iter() {
295-
if let ty::ClauseKind::UnstableFeature(sym) = pred.kind().skip_binder() {
296-
if sym == symbol {
297-
return true;
298-
}
288+
}
289+
290+
291+
pub fn may_use_unstable_feature<'a, I: Interner, Infcx>(
292+
infcx: &'a Infcx,
293+
param_env: I::ParamEnv,
294+
symbol: I::Symbol,
295+
) -> bool
296+
where Infcx: InferCtxtLike<Interner = I>
297+
{
298+
// Iterate through all goals in param_env to find the one that has the same symbol.
299+
for pred in param_env.caller_bounds().iter() {
300+
if let ty::ClauseKind::UnstableFeature(sym) = pred.kind().skip_binder() {
301+
if sym == symbol {
302+
return true;
299303
}
300304
}
305+
}
301306

302-
// During codegen we must assume that all feature bounds hold as we may be
303-
// monomorphizing a body from an upstream crate which had an unstable feature
304-
// enabled that we do not.
305-
//
306-
// Coherence should already report overlap errors involving unstable impls
307-
// as the affected code would otherwise break when stabilizing this feature.
308-
// It is also easily possible to accidentally cause unsoundness this way as
309-
// we have to always enable unstable impls during codegen.
310-
//
311-
// Return ambiguity can also prevent people from writing code which depends on inference guidance
312-
// that might no longer work after the impl is stabilised,
313-
// tests/ui/unstable-feature_bound/unstable_impl_coherence_inherence.rs is one of the example.
314-
//
315-
// Note: `feature_bound_holds_in_crate` does not consider a feature to be enabled
316-
// if we are in std/core even if there is a corresponding `feature` attribute on the crate.
317-
318-
if (self.typing_mode() == TypingMode::PostAnalysis)
319-
|| self.cx().features().feature_bound_holds_in_crate(symbol)
320-
{
321-
return true;
322-
} else {
323-
return false;
324-
}
307+
// During codegen we must assume that all feature bounds hold as we may be
308+
// monomorphizing a body from an upstream crate which had an unstable feature
309+
// enabled that we do not.
310+
//
311+
// Coherence should already report overlap errors involving unstable impls
312+
// as the affected code would otherwise break when stabilizing this feature.
313+
// It is also easily possible to accidentally cause unsoundness this way as
314+
// we have to always enable unstable impls during codegen.
315+
//
316+
// Return ambiguity can also prevent people from writing code which depends on inference guidance
317+
// that might no longer work after the impl is stabilised,
318+
// tests/ui/unstable-feature_bound/unstable_impl_coherence_inherence.rs is one of the example.
319+
//
320+
// Note: `feature_bound_holds_in_crate` does not consider a feature to be enabled
321+
// if we are in std/core even if there is a corresponding `feature` attribute on the crate.
322+
323+
if (infcx.typing_mode() == TypingMode::PostAnalysis)
324+
|| infcx.cx().features().feature_bound_holds_in_crate(symbol)
325+
{
326+
return true;
327+
} else {
328+
return false;
325329
}
326330
}

0 commit comments

Comments
 (0)