Skip to content

Commit c0fca90

Browse files
committed
Add newtype for first input type
1 parent 5215ca4 commit c0fca90

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

compiler/rustc_hir_analysis/src/coherence/orphan.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::ty::{
1313
use rustc_session::lint;
1414
use rustc_span::def_id::{DefId, LocalDefId};
1515
use rustc_span::Span;
16-
use rustc_trait_selection::traits;
16+
use rustc_trait_selection::traits::{self, IsFirstInputType};
1717
use std::ops::ControlFlow;
1818

1919
use crate::errors;
@@ -309,7 +309,7 @@ fn emit_orphan_check_error<'tcx>(
309309
(Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new());
310310
let mut sugg = None;
311311
for &(mut ty, is_target_ty) in &tys {
312-
let span = if is_target_ty {
312+
let span = if matches!(is_target_ty, IsFirstInputType::Yes) {
313313
// Point at `D<A>` in `impl<A, B> for C<B> in D<A>`
314314
self_ty_span
315315
} else {
@@ -342,7 +342,8 @@ fn emit_orphan_check_error<'tcx>(
342342
}
343343
}
344344

345-
let is_foreign = !trait_ref.def_id.is_local() && !is_target_ty;
345+
let is_foreign =
346+
!trait_ref.def_id.is_local() && matches!(is_target_ty, IsFirstInputType::No);
346347

347348
match &ty.kind() {
348349
ty::Slice(_) => {

compiler/rustc_trait_selection/src/traits/coherence.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,24 @@ pub fn trait_ref_is_local_or_fundamental<'tcx>(
596596
trait_ref.def_id.krate == LOCAL_CRATE || tcx.has_attr(trait_ref.def_id, sym::fundamental)
597597
}
598598

599+
#[derive(Debug, Copy, Clone)]
600+
pub enum IsFirstInputType {
601+
No,
602+
Yes,
603+
}
604+
605+
impl From<bool> for IsFirstInputType {
606+
fn from(b: bool) -> IsFirstInputType {
607+
match b {
608+
false => IsFirstInputType::No,
609+
true => IsFirstInputType::Yes,
610+
}
611+
}
612+
}
613+
599614
#[derive(Debug)]
600615
pub enum OrphanCheckErr<'tcx> {
601-
NonLocalInputType(Vec<(Ty<'tcx>, bool /* Is this the first input type? */)>),
616+
NonLocalInputType(Vec<(Ty<'tcx>, IsFirstInputType)>),
602617
UncoveredTy(Ty<'tcx>, Option<Ty<'tcx>>),
603618
}
604619

@@ -749,7 +764,7 @@ struct OrphanChecker<'tcx, F> {
749764
/// Ignore orphan check failures and exclusively search for the first
750765
/// local type.
751766
search_first_local_ty: bool,
752-
non_local_tys: Vec<(Ty<'tcx>, bool)>,
767+
non_local_tys: Vec<(Ty<'tcx>, IsFirstInputType)>,
753768
}
754769

755770
impl<'tcx, F, E> OrphanChecker<'tcx, F>
@@ -767,7 +782,7 @@ where
767782
}
768783

769784
fn found_non_local_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<OrphanCheckEarlyExit<'tcx, E>> {
770-
self.non_local_tys.push((t, self.in_self_ty));
785+
self.non_local_tys.push((t, self.in_self_ty.into()));
771786
ControlFlow::Continue(())
772787
}
773788

compiler/rustc_trait_selection/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use std::ops::ControlFlow;
4343
pub(crate) use self::project::{needs_normalization, BoundVarReplacer, PlaceholderReplacer};
4444

4545
pub use self::coherence::{add_placeholder_note, orphan_check, overlapping_impls};
46-
pub use self::coherence::{OrphanCheckErr, OverlapResult};
46+
pub use self::coherence::{IsFirstInputType, OrphanCheckErr, OverlapResult};
4747
pub use self::engine::{ObligationCtxt, TraitEngineExt};
4848
pub use self::fulfill::{FulfillmentContext, PendingPredicateObligation};
4949
pub use self::object_safety::astconv_object_safety_violations;

0 commit comments

Comments
 (0)