|
1 | 1 | use std::ops::ControlFlow; |
2 | 2 |
|
3 | | -use rustc_hir::LangItem; |
| 3 | +use rustc_hir::{self as hir, LangItem}; |
4 | 4 | use rustc_infer::infer::InferCtxt; |
5 | | -use rustc_infer::traits::solve::{CandidateSource, GoalSource, MaybeCause}; |
| 5 | +use rustc_infer::traits::solve::{BuiltinImplSource, CandidateSource, GoalSource, MaybeCause}; |
6 | 6 | use rustc_infer::traits::{ |
7 | 7 | self, MismatchedProjectionTypes, Obligation, ObligationCause, ObligationCauseCode, |
8 | 8 | PredicateObligation, SelectionError, |
@@ -248,6 +248,55 @@ impl<'tcx> BestObligation<'tcx> { |
248 | 248 | candidates |
249 | 249 | } |
250 | 250 |
|
| 251 | + fn is_opaque_auto_trait_candidate( |
| 252 | + &self, |
| 253 | + tcx: TyCtxt<'tcx>, |
| 254 | + candidate: &inspect::InspectCandidate<'_, 'tcx>, |
| 255 | + pred: ty::Predicate<'tcx>, |
| 256 | + ) -> bool { |
| 257 | + self.is_builtin_misc_trait_candidate(candidate) |
| 258 | + && self.is_positive_auto_trait_predicate_with_opaque_self(tcx, pred) |
| 259 | + } |
| 260 | + |
| 261 | + fn is_builtin_misc_trait_candidate( |
| 262 | + &self, |
| 263 | + candidate: &inspect::InspectCandidate<'_, 'tcx>, |
| 264 | + ) -> bool { |
| 265 | + matches!( |
| 266 | + candidate.kind(), |
| 267 | + inspect::ProbeKind::TraitCandidate { |
| 268 | + source: CandidateSource::BuiltinImpl(BuiltinImplSource::Misc), |
| 269 | + result: _, |
| 270 | + } |
| 271 | + ) |
| 272 | + } |
| 273 | + |
| 274 | + fn is_positive_auto_trait_predicate_with_opaque_self( |
| 275 | + &self, |
| 276 | + tcx: TyCtxt<'tcx>, |
| 277 | + pred: ty::Predicate<'tcx>, |
| 278 | + ) -> bool { |
| 279 | + let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) = |
| 280 | + pred.kind().skip_binder() |
| 281 | + else { |
| 282 | + return false; |
| 283 | + }; |
| 284 | + |
| 285 | + if trait_pred.polarity != ty::PredicatePolarity::Positive |
| 286 | + || !tcx.trait_is_auto(trait_pred.def_id()) |
| 287 | + { |
| 288 | + return false; |
| 289 | + } |
| 290 | + |
| 291 | + let ty::Alias(_, ty::AliasTy { kind: ty::Opaque { def_id, .. }, .. }) = |
| 292 | + trait_pred.self_ty().kind() |
| 293 | + else { |
| 294 | + return false; |
| 295 | + }; |
| 296 | + |
| 297 | + !matches!(tcx.opaque_ty_origin(*def_id), hir::OpaqueTyOrigin::AsyncFn { .. }) |
| 298 | + } |
| 299 | + |
251 | 300 | /// HACK: We walk the nested obligations for a well-formed arg manually, |
252 | 301 | /// since there's nontrivial logic in `wf.rs` to set up an obligation cause. |
253 | 302 | /// Ideally we'd be able to track this better. |
@@ -447,6 +496,13 @@ impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> { |
447 | 496 | return ControlFlow::Break(self.obligation.clone()); |
448 | 497 | } |
449 | 498 |
|
| 499 | + // Don't walk into opaque auto trait candidates, as doing so would expose |
| 500 | + // the opaque's hidden type in diagnostics outside of its defining scope. |
| 501 | + if self.is_opaque_auto_trait_candidate(tcx, candidate, pred) { |
| 502 | + trace!("opaque auto trait candidate -> exit"); |
| 503 | + return ControlFlow::Break(self.obligation.clone()); |
| 504 | + } |
| 505 | + |
450 | 506 | // FIXME: Also, what about considering >1 layer up the stack? May be necessary |
451 | 507 | // for normalizes-to. |
452 | 508 | let child_mode = match pred.kind().skip_binder() { |
|
0 commit comments