Skip to content

Commit ef0684a

Browse files
committed
make all typing-mode conditional code an exhaustive match
1 parent 2bb47e1 commit ef0684a

9 files changed

Lines changed: 98 additions & 27 deletions

File tree

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,6 @@ pub enum AttributeKind {
16151615
/// Represents `#[rustc_exhaustivo compiler/rustc_hir/src/attrs/encode_cross_crate.rs:19:15]`
16161616
RustcMustMatchExhaustively(Span),
16171617

1618-
16191618
/// Represents `#[rustc_never_returns_null_ptr]`
16201619
RustcNeverReturnsNullPtr,
16211620

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,20 @@ impl<'tcx> InferCtxt<'tcx> {
8989
if def_id.is_local() =>
9090
{
9191
let def_id = def_id.expect_local();
92-
if let ty::TypingMode::Coherence = self.typing_mode() {
93-
// See comment on `insert_hidden_type` for why this is sufficient in coherence
94-
return Some(self.register_hidden_type(
95-
OpaqueTypeKey { def_id, args },
96-
span,
97-
param_env,
98-
b,
99-
));
92+
match self.typing_mode() {
93+
ty::TypingMode::Coherence => {
94+
// See comment on `insert_hidden_type` for why this is sufficient in coherence
95+
return Some(self.register_hidden_type(
96+
OpaqueTypeKey { def_id, args },
97+
span,
98+
param_env,
99+
b,
100+
));
101+
}
102+
ty::TypingMode::Analysis { .. }
103+
| ty::TypingMode::Borrowck { .. }
104+
| ty::TypingMode::PostBorrowckAnalysis { .. }
105+
| ty::TypingMode::PostAnalysis => {}
100106
}
101107
// Check that this is `impl Trait` type is
102108
// declared by `parent_def_id` -- i.e., one whose

compiler/rustc_infer/src/infer/relate/generalize.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,13 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
603603
//
604604
// cc trait-system-refactor-initiative#108
605605
if self.infcx.next_trait_solver()
606-
&& !matches!(self.infcx.typing_mode(), TypingMode::Coherence)
606+
&& match self.infcx.typing_mode() {
607+
TypingMode::Coherence => false,
608+
TypingMode::Analysis { .. }
609+
| TypingMode::Borrowck { .. }
610+
| TypingMode::PostBorrowckAnalysis { .. }
611+
| TypingMode::PostAnalysis => true,
612+
}
607613
&& self.in_alias
608614
{
609615
inner.type_variables().equate(vid, new_var_id);
@@ -735,7 +741,13 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
735741
// See the comment for type inference variables
736742
// for more details.
737743
if self.infcx.next_trait_solver()
738-
&& !matches!(self.infcx.typing_mode(), TypingMode::Coherence)
744+
&& match self.infcx.typing_mode() {
745+
TypingMode::Coherence => false,
746+
TypingMode::Analysis { .. }
747+
| TypingMode::Borrowck { .. }
748+
| TypingMode::PostBorrowckAnalysis { .. }
749+
| TypingMode::PostAnalysis => true,
750+
}
739751
&& self.in_alias
740752
{
741753
variable_table.union(vid, new_var_id);

compiler/rustc_next_trait_solver/src/solve/normalizes_to/opaque_types.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ where
9393
});
9494
self.eq(goal.param_env, expected, actual)?;
9595
}
96-
_ => unreachable!(),
96+
TypingMode::Coherence
97+
| TypingMode::PostBorrowckAnalysis { .. }
98+
| TypingMode::PostAnalysis => unreachable!(),
9799
}
98100
}
99101

compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,12 +1421,18 @@ where
14211421
mut candidates: Vec<Candidate<I>>,
14221422
failed_candidate_info: FailedCandidateInfo,
14231423
) -> Result<(CanonicalResponse<I>, Option<TraitGoalProvenVia>), NoSolution> {
1424-
if let TypingMode::Coherence = self.typing_mode() {
1425-
return if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1426-
Ok((response, Some(TraitGoalProvenVia::Misc)))
1427-
} else {
1428-
self.flounder(&candidates).map(|r| (r, None))
1429-
};
1424+
match self.typing_mode() {
1425+
TypingMode::Coherence => {
1426+
return if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1427+
Ok((response, Some(TraitGoalProvenVia::Misc)))
1428+
} else {
1429+
self.flounder(&candidates).map(|r| (r, None))
1430+
};
1431+
}
1432+
TypingMode::Analysis { .. }
1433+
| TypingMode::Borrowck { .. }
1434+
| TypingMode::PostBorrowckAnalysis { .. }
1435+
| TypingMode::PostAnalysis => {}
14301436
}
14311437

14321438
// We prefer trivial builtin candidates, i.e. builtin impls without any

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,14 @@ impl<'a, 'tcx> FulfillProcessor<'a, 'tcx> {
841841
stalled_on: &mut Vec<TyOrConstInferVar>,
842842
) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
843843
let infcx = self.selcx.infcx;
844-
if obligation.predicate.is_global() && !matches!(infcx.typing_mode(), TypingMode::Coherence)
844+
if obligation.predicate.is_global()
845+
&& match infcx.typing_mode() {
846+
TypingMode::Coherence => false,
847+
TypingMode::Analysis { .. }
848+
| TypingMode::Borrowck { .. }
849+
| TypingMode::PostBorrowckAnalysis { .. }
850+
| TypingMode::PostAnalysis => true,
851+
}
845852
{
846853
// no type variables present, can use evaluation for better caching.
847854
// FIXME: consider caching errors too.
@@ -896,7 +903,14 @@ impl<'a, 'tcx> FulfillProcessor<'a, 'tcx> {
896903
) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
897904
let tcx = self.selcx.tcx();
898905
let infcx = self.selcx.infcx;
899-
if obligation.predicate.is_global() && !matches!(infcx.typing_mode(), TypingMode::Coherence)
906+
if obligation.predicate.is_global()
907+
&& match infcx.typing_mode() {
908+
TypingMode::Coherence => false,
909+
TypingMode::Analysis { .. }
910+
| TypingMode::Borrowck { .. }
911+
| TypingMode::PostBorrowckAnalysis { .. }
912+
| TypingMode::PostAnalysis => true,
913+
}
900914
{
901915
// no type variables present, can use evaluation for better caching.
902916
// FIXME: consider caching errors too.

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
849849
//
850850
// Note that this is only sound as projection candidates of opaque types
851851
// are always applicable for auto traits.
852-
} else if let TypingMode::Coherence = self.infcx.typing_mode() {
852+
} else if match self.infcx.typing_mode() {
853+
TypingMode::Coherence => true,
854+
TypingMode::Analysis { .. }
855+
| TypingMode::Borrowck { .. }
856+
| TypingMode::PostBorrowckAnalysis { .. }
857+
| TypingMode::PostAnalysis => false,
858+
} {
853859
// We do not emit auto trait candidates for opaque types in coherence.
854860
// Doing so can result in weird dependency cycles.
855861
candidates.ambiguous = true;

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/resolution.html#selection
44
55
use std::cell::{Cell, RefCell};
6+
use std::cmp;
67
use std::fmt::{self, Display};
78
use std::ops::ControlFlow;
8-
use std::{assert_matches, cmp};
99

1010
use hir::def::DefKind;
1111
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
@@ -210,7 +210,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
210210
/// Enables tracking of intercrate ambiguity causes. See
211211
/// the documentation of [`Self::intercrate_ambiguity_causes`] for more.
212212
pub fn enable_tracking_intercrate_ambiguity_causes(&mut self) {
213-
assert_matches!(self.infcx.typing_mode(), TypingMode::Coherence);
213+
match self.infcx.typing_mode() {
214+
TypingMode::Coherence => {}
215+
TypingMode::Analysis { .. }
216+
| TypingMode::Borrowck { .. }
217+
| TypingMode::PostBorrowckAnalysis { .. }
218+
| TypingMode::PostAnalysis => bug!(),
219+
}
220+
214221
assert!(self.intercrate_ambiguity_causes.is_none());
215222
self.intercrate_ambiguity_causes = Some(FxIndexSet::default());
216223
debug!("selcx: enable_tracking_intercrate_ambiguity_causes");
@@ -222,7 +229,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
222229
pub fn take_intercrate_ambiguity_causes(
223230
&mut self,
224231
) -> FxIndexSet<IntercrateAmbiguityCause<'tcx>> {
225-
assert_matches!(self.infcx.typing_mode(), TypingMode::Coherence);
232+
match self.infcx.typing_mode() {
233+
TypingMode::Coherence => {}
234+
TypingMode::Analysis { .. }
235+
| TypingMode::Borrowck { .. }
236+
| TypingMode::PostBorrowckAnalysis { .. }
237+
| TypingMode::PostAnalysis => bug!(),
238+
}
239+
226240
self.intercrate_ambiguity_causes.take().unwrap_or_default()
227241
}
228242

@@ -1016,8 +1030,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
10161030
previous_stack: TraitObligationStackList<'o, 'tcx>,
10171031
mut obligation: PolyTraitObligation<'tcx>,
10181032
) -> Result<EvaluationResult, OverflowError> {
1019-
if !matches!(self.infcx.typing_mode(), TypingMode::Coherence)
1020-
&& obligation.is_global()
1033+
if match self.infcx.typing_mode() {
1034+
TypingMode::Coherence => false,
1035+
TypingMode::Analysis { .. }
1036+
| TypingMode::Borrowck { .. }
1037+
| TypingMode::PostBorrowckAnalysis { .. }
1038+
| TypingMode::PostAnalysis => true,
1039+
} && obligation.is_global()
10211040
&& obligation.param_env.caller_bounds().iter().all(|bound| bound.has_param())
10221041
{
10231042
// If a param env has no global bounds, global obligations do not
@@ -2548,7 +2567,13 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
25482567
nested_obligations.extend(obligations);
25492568

25502569
if impl_trait_header.polarity == ty::ImplPolarity::Reservation
2551-
&& !matches!(self.infcx.typing_mode(), TypingMode::Coherence)
2570+
&& match self.infcx.typing_mode() {
2571+
TypingMode::Coherence => false,
2572+
TypingMode::Analysis { .. }
2573+
| TypingMode::Borrowck { .. }
2574+
| TypingMode::PostBorrowckAnalysis { .. }
2575+
| TypingMode::PostAnalysis => true,
2576+
}
25522577
{
25532578
debug!("reservation impls only apply in intercrate mode");
25542579
return Err(());

compiler/rustc_type_ir/src/infer_ctxt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::{self as ty, Interner, TyVid};
2323
feature = "nightly",
2424
derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
2525
)]
26+
#[cfg_attr(not(bootstrap), rustc_must_match_exhaustively)]
2627
pub enum TypingMode<I: Interner> {
2728
/// When checking whether impls overlap, we check whether any obligations
2829
/// are guaranteed to never hold when unifying the impls. This requires us

0 commit comments

Comments
 (0)