Skip to content

Commit b5e53dc

Browse files
committed
Cleanup from_cycle_error::variances_of.
variances_of currently used search_for_cycle_permutation, which can fail and abort when constructed error result value does not match query input. This commit changes variances_of to receive a def_id which means it can compute a value without using search_for_cycle_permutation, avoiding the possible abort. Fixes #127971
1 parent b711f95 commit b5e53dc

File tree

1 file changed

+6
-23
lines changed

1 file changed

+6
-23
lines changed

compiler/rustc_query_impl/src/from_cycle_error.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use rustc_errors::codes::*;
88
use rustc_errors::{Applicability, Diag, MultiSpan, pluralize, struct_span_code_err};
99
use rustc_hir as hir;
1010
use rustc_hir::def::{DefKind, Res};
11+
use rustc_middle::bug;
1112
use rustc_middle::queries::{QueryVTables, TaggedQueryKey};
1213
use rustc_middle::query::CycleError;
1314
use rustc_middle::query::erase::erase_val;
1415
use rustc_middle::ty::layout::LayoutError;
1516
use rustc_middle::ty::{self, Ty, TyCtxt};
16-
use rustc_middle::{bug, span_bug};
1717
use rustc_span::def_id::{DefId, LocalDefId};
1818
use rustc_span::{ErrorGuaranteed, Span};
1919

@@ -31,9 +31,9 @@ pub(crate) fn specialize_query_vtables<'tcx>(vtables: &mut QueryVTables<'tcx>) {
3131
vtables.check_representability_adt_ty.value_from_cycle_error =
3232
|tcx, _, cycle, _err| check_representability(tcx, cycle);
3333

34-
vtables.variances_of.value_from_cycle_error = |tcx, _, cycle, err| {
34+
vtables.variances_of.value_from_cycle_error = |tcx, key, _, err| {
3535
let _guar = err.delay_as_bug();
36-
erase_val(variances_of(tcx, cycle))
36+
erase_val(variances_of(tcx, key))
3737
};
3838

3939
vtables.layout_of.value_from_cycle_error = |tcx, _, cycle, err| {
@@ -103,26 +103,9 @@ fn check_representability<'tcx>(tcx: TyCtxt<'tcx>, cycle_error: CycleError<'tcx>
103103
guar.raise_fatal()
104104
}
105105

106-
fn variances_of<'tcx>(tcx: TyCtxt<'tcx>, cycle_error: CycleError<'tcx>) -> &'tcx [ty::Variance] {
107-
search_for_cycle_permutation(
108-
&cycle_error.cycle,
109-
|cycle| {
110-
if let Some(frame) = cycle.get(0)
111-
&& let TaggedQueryKey::variances_of(def_id) = frame.node.tagged_key
112-
{
113-
let n = tcx.generics_of(def_id).own_params.len();
114-
ControlFlow::Break(tcx.arena.alloc_from_iter(iter::repeat_n(ty::Bivariant, n)))
115-
} else {
116-
ControlFlow::Continue(())
117-
}
118-
},
119-
|| {
120-
span_bug!(
121-
cycle_error.usage.as_ref().unwrap().span,
122-
"only `variances_of` returns `&[ty::Variance]`"
123-
)
124-
},
125-
)
106+
fn variances_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx [ty::Variance] {
107+
let n = tcx.generics_of(def_id).own_params.len();
108+
tcx.arena.alloc_from_iter(iter::repeat_n(ty::Bivariant, n))
126109
}
127110

128111
// Take a cycle of `Q` and try `try_cycle` on every permutation, falling back to `otherwise`.

0 commit comments

Comments
 (0)