Skip to content

Commit 3399ed3

Browse files
committed
Simplify type_of_opaque.
There is a bunch of complexity supporting the "cannot check whether the hidden type of opaque type satisfies auto traits" error that shows up in `tests/ui/impl-trait/auto-trait-leak.rs`. This is an obscure error that shows up in a single test. If we are willing to downgrade that error message to a cycle error, we can do the following. - Simplify the `type_of_opaque` return value. - Remove the `cycle_stash` query modifier. - Remove the `CyclePlaceholder` type. - Remove the `SelectionError::OpaqueTypeAutoTraitLeakageUnknown` variant. - Remove a `FromCycleError` impl. - Remove `report_opaque_type_auto_trait_leakage`. - Remove the `StashKey::Cycle` variant. - Remove the `CycleErrorHandling::Stash` variant. That's a lot! I think this is a worthwhile trade-off.
1 parent b2fabe3 commit 3399ed3

16 files changed

Lines changed: 91 additions & 144 deletions

File tree

compiler/rustc_errors/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,6 @@ pub enum StashKey {
371371
MaybeFruTypo,
372372
CallAssocMethod,
373373
AssociatedTypeSuggestion,
374-
/// Query cycle detected, stashing in favor of a better error.
375-
Cycle,
376374
UndeterminedMacroResolution,
377375
/// Used by `Parser::maybe_recover_trailing_expr`
378376
ExprInPat,

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rustc_errors::{Applicability, StashKey, Suggestions};
44
use rustc_hir::def_id::{DefId, LocalDefId};
55
use rustc_hir::intravisit::VisitorExt;
66
use rustc_hir::{self as hir, AmbigArg, HirId};
7-
use rustc_middle::query::plumbing::CyclePlaceholder;
87
use rustc_middle::ty::print::with_forced_trimmed_paths;
98
use rustc_middle::ty::util::IntTypeExt;
109
use rustc_middle::ty::{self, DefiningScopeKind, IsSuggestable, Ty, TyCtxt, TypeVisitableExt};
@@ -183,10 +182,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
183182
}
184183
},
185184

186-
Node::OpaqueTy(..) => tcx.type_of_opaque(def_id).map_or_else(
187-
|CyclePlaceholder(guar)| Ty::new_error(tcx, guar),
188-
|ty| ty.instantiate_identity(),
189-
),
185+
Node::OpaqueTy(..) => tcx.type_of_opaque(def_id).instantiate_identity(),
190186

191187
Node::ForeignItem(foreign_item) => match foreign_item.kind {
192188
ForeignItemKind::Fn(..) => {
@@ -249,12 +245,9 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
249245
}
250246
}
251247

252-
pub(super) fn type_of_opaque(
253-
tcx: TyCtxt<'_>,
254-
def_id: DefId,
255-
) -> Result<ty::EarlyBinder<'_, Ty<'_>>, CyclePlaceholder> {
248+
pub(super) fn type_of_opaque(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<'_, Ty<'_>> {
256249
if let Some(def_id) = def_id.as_local() {
257-
Ok(match tcx.hir_node_by_def_id(def_id).expect_opaque_ty().origin {
250+
match tcx.hir_node_by_def_id(def_id).expect_opaque_ty().origin {
258251
hir::OpaqueTyOrigin::TyAlias { in_assoc_ty: false, .. } => {
259252
opaque::find_opaque_ty_constraints_for_tait(
260253
tcx,
@@ -287,11 +280,11 @@ pub(super) fn type_of_opaque(
287280
DefiningScopeKind::MirBorrowck,
288281
)
289282
}
290-
})
283+
}
291284
} else {
292285
// Foreign opaque type will go through the foreign provider
293286
// and load the type from metadata.
294-
Ok(tcx.type_of(def_id))
287+
tcx.type_of(def_id)
295288
}
296289
}
297290

compiler/rustc_macros/src/query.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ struct QueryModifiers {
144144
arena_cache: Option<Ident>,
145145
cache_on_disk_if: Option<CacheOnDiskIf>,
146146
cycle_delay_bug: Option<Ident>,
147-
cycle_stash: Option<Ident>,
148147
depth_limit: Option<Ident>,
149148
desc: Desc,
150149
eval_always: Option<Ident>,
@@ -159,7 +158,6 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
159158
let mut cache_on_disk_if = None;
160159
let mut desc = None;
161160
let mut cycle_delay_bug = None;
162-
let mut cycle_stash = None;
163161
let mut no_hash = None;
164162
let mut anon = None;
165163
let mut eval_always = None;
@@ -195,8 +193,6 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
195193
try_insert!(arena_cache = modifier);
196194
} else if modifier == "cycle_delay_bug" {
197195
try_insert!(cycle_delay_bug = modifier);
198-
} else if modifier == "cycle_stash" {
199-
try_insert!(cycle_stash = modifier);
200196
} else if modifier == "no_hash" {
201197
try_insert!(no_hash = modifier);
202198
} else if modifier == "anon" {
@@ -221,7 +217,6 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
221217
cache_on_disk_if,
222218
desc,
223219
cycle_delay_bug,
224-
cycle_stash,
225220
no_hash,
226221
anon,
227222
eval_always,
@@ -257,7 +252,6 @@ fn make_modifiers_stream(query: &Query) -> proc_macro2::TokenStream {
257252
arena_cache,
258253
cache_on_disk_if,
259254
cycle_delay_bug,
260-
cycle_stash,
261255
depth_limit,
262256
desc: _,
263257
eval_always,
@@ -273,8 +267,6 @@ fn make_modifiers_stream(query: &Query) -> proc_macro2::TokenStream {
273267

274268
let cycle_error_handling = if cycle_delay_bug.is_some() {
275269
quote! { DelayBug }
276-
} else if cycle_stash.is_some() {
277-
quote! { Stash }
278270
} else {
279271
quote! { Error }
280272
};
@@ -411,7 +403,6 @@ fn add_to_analyzer_stream(query: &Query, analyzer_stream: &mut proc_macro2::Toke
411403
doc_link!(
412404
arena_cache,
413405
cycle_delay_bug,
414-
cycle_stash,
415406
no_hash,
416407
anon,
417408
eval_always,

compiler/rustc_middle/src/queries.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
//! - `cache_on_disk_if { ... }`: Cache the query result to disk if the provided block evaluates to
3434
//! true. The query key identifier is available for use within the block, as is `tcx`.
3535
//! - `cycle_delay_bug`: If a dependency cycle is detected, emit a delayed bug instead of aborting immediately.
36-
//! - `cycle_stash`: If a dependency cycle is detected, stash the error for later handling.
3736
//! - `no_hash`: Do not hash the query result for incremental compilation; just mark as dirty if recomputed.
3837
//! - `anon`: Make the query anonymous in the dependency graph (no dep node is created).
3938
//! - `eval_always`: Always evaluate the query, ignoring its dependencies and cached results.
@@ -118,7 +117,6 @@ use crate::mir::mono::{
118117
CodegenUnit, CollectionMode, MonoItem, MonoItemPartitions, NormalizationErrorInMono,
119118
};
120119
use crate::query::describe_as_module;
121-
use crate::query::plumbing::CyclePlaceholder;
122120
use crate::traits::query::{
123121
CanonicalAliasGoal, CanonicalDropckOutlivesGoal, CanonicalImpliedOutlivesBoundsGoal,
124122
CanonicalMethodAutoderefStepsGoal, CanonicalPredicateGoal, CanonicalTypeOpAscribeUserTypeGoal,
@@ -339,22 +337,16 @@ rustc_queries! {
339337
feedable
340338
}
341339

342-
/// Returns the *hidden type* of the opaque type given by `DefId` unless a cycle occurred.
343-
///
344-
/// This is a specialized instance of [`Self::type_of`] that detects query cycles.
345-
/// Unless `CyclePlaceholder` needs to be handled separately, call [`Self::type_of`] instead.
346-
/// This is used to improve the error message in cases where revealing the hidden type
347-
/// for auto-trait leakage cycles.
340+
/// Returns the *hidden type* of the opaque type given by `DefId`.
348341
///
349342
/// # Panics
350343
///
351344
/// This query will panic if the given definition is not an opaque type.
352-
query type_of_opaque(key: DefId) -> Result<ty::EarlyBinder<'tcx, Ty<'tcx>>, CyclePlaceholder> {
345+
query type_of_opaque(key: DefId) -> ty::EarlyBinder<'tcx, Ty<'tcx>> {
353346
desc {
354347
"computing type of opaque `{path}`",
355348
path = tcx.def_path_str(key),
356349
}
357-
cycle_stash
358350
}
359351
query type_of_opaque_hir_typeck(key: LocalDefId) -> ty::EarlyBinder<'tcx, Ty<'tcx>> {
360352
desc {

compiler/rustc_middle/src/query/erase.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc_span::{ErrorGuaranteed, Spanned};
1414

1515
use crate::mir::interpret::EvalToValTreeResult;
1616
use crate::mir::mono::{MonoItem, NormalizationErrorInMono};
17-
use crate::query::plumbing::CyclePlaceholder;
1817
use crate::traits::solve;
1918
use crate::ty::adjustment::CoerceUnsizedInfo;
2019
use crate::ty::{self, Ty, TyCtxt};
@@ -212,10 +211,6 @@ impl Erasable for Result<&'_ ty::List<Ty<'_>>, ty::util::AlwaysRequiresDrop> {
212211
[u8; size_of::<Result<&'static ty::List<Ty<'static>>, ty::util::AlwaysRequiresDrop>>()];
213212
}
214213

215-
impl Erasable for Result<ty::EarlyBinder<'_, Ty<'_>>, CyclePlaceholder> {
216-
type Storage = [u8; size_of::<Result<ty::EarlyBinder<'static, Ty<'_>>, CyclePlaceholder>>()];
217-
}
218-
219214
impl Erasable
220215
for Result<(&'_ [Spanned<MonoItem<'_>>], &'_ [Spanned<MonoItem<'_>>]), NormalizationErrorInMono>
221216
{

compiler/rustc_middle/src/query/modifiers.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ pub(crate) struct cache_on_disk_if;
2828
/// A cycle error results in a delay_bug call
2929
pub(crate) struct cycle_delay_bug;
3030

31-
/// # `cycle_stash` query modifier
32-
///
33-
/// A cycle error results in a stashed cycle error that can be unstashed and canceled later
34-
pub(crate) struct cycle_stash;
35-
3631
/// # `depth_limit` query modifier
3732
///
3833
/// Whether the query has a call depth limit

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_data_structures::sharded::Sharded;
77
use rustc_data_structures::sync::{AtomicU64, WorkerLocal};
88
use rustc_hir::def_id::{DefId, LocalDefId};
99
use rustc_hir::hir_id::OwnerId;
10-
use rustc_macros::HashStable;
1110
use rustc_span::{ErrorGuaranteed, Span};
1211
pub use sealed::IntoQueryParam;
1312

@@ -58,7 +57,6 @@ pub enum ActiveKeyStatus<'tcx> {
5857
pub enum CycleErrorHandling {
5958
Error,
6059
DelayBug,
61-
Stash,
6260
}
6361

6462
#[derive(Clone, Debug)]
@@ -651,9 +649,6 @@ mod sealed {
651649
}
652650
}
653651

654-
#[derive(Copy, Clone, Debug, HashStable)]
655-
pub struct CyclePlaceholder(pub ErrorGuaranteed);
656-
657652
#[cold]
658653
pub(crate) fn default_query(name: &str, key: &dyn std::fmt::Debug) -> ! {
659654
bug!(

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,6 @@ pub enum SelectionError<'tcx> {
633633
NotConstEvaluatable(NotConstEvaluatable),
634634
/// Exceeded the recursion depth during type projection.
635635
Overflow(OverflowError),
636-
/// Computing an opaque type's hidden type caused an error (e.g. a cycle error).
637-
/// We can thus not know whether the hidden type implements an auto trait, so
638-
/// we should not presume anything about it.
639-
OpaqueTypeAutoTraitLeakageUnknown(DefId),
640636
/// Error for a `ConstArgHasType` goal
641637
ConstArgHasWrongType { ct: ty::Const<'tcx>, ct_ty: Ty<'tcx>, expected_ty: Ty<'tcx> },
642638
}

compiler/rustc_query_impl/src/execution.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_data_structures::hash_table::{Entry, HashTable};
55
use rustc_data_structures::stack::ensure_sufficient_stack;
66
use rustc_data_structures::sync::{DynSend, DynSync};
77
use rustc_data_structures::{outline, sharded, sync};
8-
use rustc_errors::{FatalError, StashKey};
8+
use rustc_errors::FatalError;
99
use rustc_middle::dep_graph::{DepGraphData, DepNodeKey, SerializedDepNodeIndex};
1010
use rustc_middle::query::plumbing::QueryVTable;
1111
use rustc_middle::query::{
@@ -110,16 +110,6 @@ fn mk_cycle<'tcx, C: QueryCache>(
110110
let guar = error.delay_as_bug();
111111
(query.value_from_cycle_error)(tcx, cycle_error, guar)
112112
}
113-
CycleErrorHandling::Stash => {
114-
let guar = if let Some(root) = cycle_error.cycle.first()
115-
&& let Some(span) = root.frame.info.span
116-
{
117-
error.stash(span, StashKey::Cycle).unwrap()
118-
} else {
119-
error.emit()
120-
};
121-
(query.value_from_cycle_error)(tcx, cycle_error, guar)
122-
}
123113
}
124114
}
125115

compiler/rustc_query_impl/src/from_cycle_error.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use rustc_middle::dep_graph::DepKind;
1212
use rustc_middle::queries::QueryVTables;
1313
use rustc_middle::query::CycleError;
1414
use rustc_middle::query::erase::erase_val;
15-
use rustc_middle::query::plumbing::CyclePlaceholder;
1615
use rustc_middle::ty::layout::{LayoutError, TyAndLayout};
1716
use rustc_middle::ty::{self, Ty, TyCtxt};
1817
use rustc_middle::{bug, span_bug};
@@ -31,9 +30,6 @@ pub(crate) fn specialize_query_vtables<'tcx>(vtables: &mut QueryVTables<'tcx>) {
3130
vtables.erase_and_anonymize_regions_ty.value_from_cycle_error =
3231
|tcx, _, guar| erase_val(Ty::new_error(tcx, guar));
3332

34-
vtables.type_of_opaque.value_from_cycle_error =
35-
|_, _, guar| erase_val(Err(CyclePlaceholder(guar)));
36-
3733
vtables.fn_sig.value_from_cycle_error = |tcx, cycle, guar| erase_val(fn_sig(tcx, cycle, guar));
3834

3935
vtables.check_representability.value_from_cycle_error =

0 commit comments

Comments
 (0)