Skip to content

Commit 9bfc46c

Browse files
committed
Add newtype for first input type
1 parent cb51c85 commit 9bfc46c

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

compiler/rustc_hir_analysis/src/coherence/orphan.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
//! Orphan checker: every impl either implements a trait defined in this
22
//! crate or pertains to a type defined in this crate.
33
4+
use crate::errors;
45
use rustc_errors::ErrorGuaranteed;
56
use rustc_hir as hir;
67
use rustc_middle::ty::{self, AliasKind, Ty, TyCtxt, TypeVisitableExt};
78
use rustc_span::def_id::LocalDefId;
89
use rustc_span::Span;
9-
use rustc_trait_selection::traits;
10-
11-
use crate::errors;
10+
use rustc_trait_selection::traits::{self, IsFirstInputType};
1211

1312
#[instrument(skip(tcx), level = "debug")]
1413
pub(crate) fn orphan_check_impl(
@@ -288,7 +287,7 @@ fn emit_orphan_check_error<'tcx>(
288287
(Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new());
289288
let mut sugg = None;
290289
for &(mut ty, is_target_ty) in &tys {
291-
let span = if is_target_ty {
290+
let span = if matches!(is_target_ty, IsFirstInputType::Yes) {
292291
// Point at `D<A>` in `impl<A, B> for C<B> in D<A>`
293292
self_ty_span
294293
} else {
@@ -321,7 +320,8 @@ fn emit_orphan_check_error<'tcx>(
321320
}
322321
}
323322

324-
let is_foreign = !trait_ref.def_id.is_local() && !is_target_ty;
323+
let is_foreign =
324+
!trait_ref.def_id.is_local() && matches!(is_target_ty, IsFirstInputType::No);
325325

326326
match &ty.kind() {
327327
ty::Slice(_) => {

compiler/rustc_trait_selection/src/traits/coherence.rs

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

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

@@ -751,7 +766,7 @@ struct OrphanChecker<'tcx, F> {
751766
/// Ignore orphan check failures and exclusively search for the first
752767
/// local type.
753768
search_first_local_ty: bool,
754-
non_local_tys: Vec<(Ty<'tcx>, bool)>,
769+
non_local_tys: Vec<(Ty<'tcx>, IsFirstInputType)>,
755770
}
756771

757772
impl<'tcx, F, E> OrphanChecker<'tcx, F>
@@ -769,7 +784,7 @@ where
769784
}
770785

771786
fn found_non_local_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<OrphanCheckEarlyExit<'tcx, E>> {
772-
self.non_local_tys.push((t, self.in_self_ty));
787+
self.non_local_tys.push((t, self.in_self_ty.into()));
773788
ControlFlow::Continue(())
774789
}
775790

compiler/rustc_trait_selection/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use std::fmt::Debug;
4242
use std::ops::ControlFlow;
4343

4444
pub use self::coherence::{add_placeholder_note, orphan_check, overlapping_impls};
45-
pub use self::coherence::{OrphanCheckErr, OverlapResult};
45+
pub use self::coherence::{IsFirstInputType, OrphanCheckErr, OverlapResult};
4646
pub use self::engine::{ObligationCtxt, TraitEngineExt};
4747
pub use self::fulfill::{FulfillmentContext, PendingPredicateObligation};
4848
pub use self::normalize::NormalizeExt;

0 commit comments

Comments
 (0)