Skip to content

Commit f22fadd

Browse files
committed
Normalize trait ref before orphan check
1 parent 9ab0749 commit f22fadd

28 files changed

+332
-131
lines changed

compiler/rustc_hir_analysis/messages.ftl

+4-4
Original file line numberDiff line numberDiff line change
@@ -370,13 +370,13 @@ hir_analysis_transparent_non_zero_sized_enum = the variant of a transparent {$de
370370
.label = needs at most one field with non-trivial size or alignment, but has {$field_count}
371371
.labels = this field has non-zero size or requires alignment
372372
373-
hir_analysis_ty_param_first_local = type parameter `{$param_ty}` must be covered by another type when it appears before the first local type (`{$local_type}`)
374-
.label = type parameter `{$param_ty}` must be covered by another type when it appears before the first local type (`{$local_type}`)
373+
hir_analysis_ty_param_first_local = type parameter `{$param}` must be covered by another type when it appears before the first local type (`{$local_type}`)
374+
.label = type parameter `{$param}` must be covered by another type when it appears before the first local type (`{$local_type}`)
375375
.note = implementing a foreign trait is only possible if at least one of the types for which it is implemented is local, and no uncovered type parameters appear before that first local type
376376
.case_note = in this case, 'before' refers to the following order: `impl<..> ForeignTrait<T1, ..., Tn> for T0`, where `T0` is the first and `Tn` is the last
377377
378-
hir_analysis_ty_param_some = type parameter `{$param_ty}` must be used as the type parameter for some local type (e.g., `MyStruct<{$param_ty}>`)
379-
.label = type parameter `{$param_ty}` must be used as the type parameter for some local type
378+
hir_analysis_ty_param_some = type parameter `{$param}` must be used as the type parameter for some local type (e.g., `MyStruct<{$param}>`)
379+
.label = type parameter `{$param}` must be used as the type parameter for some local type
380380
.note = implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
381381
.only_note = only traits defined in the current crate can be implemented for a type parameter
382382

compiler/rustc_hir_analysis/src/coherence/orphan.rs

+37-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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 rustc_data_structures::fx::FxHashSet;
4+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
55
use rustc_errors::{DelayDm, ErrorGuaranteed};
66
use rustc_hir as hir;
77
use rustc_middle::ty::util::CheckRegions;
@@ -12,7 +12,7 @@ use rustc_middle::ty::{
1212
};
1313
use rustc_session::lint;
1414
use rustc_span::def_id::{DefId, LocalDefId};
15-
use rustc_span::Span;
15+
use rustc_span::{Span, Symbol};
1616
use rustc_trait_selection::traits;
1717
use std::ops::ControlFlow;
1818

@@ -424,22 +424,49 @@ fn emit_orphan_check_error<'tcx>(
424424
};
425425
tcx.sess.emit_err(err_struct)
426426
}
427-
traits::OrphanCheckErr::UncoveredTy(param_ty, local_type) => {
428-
let mut sp = sp;
429-
for param in generics.params {
430-
if param.name.ident().to_string() == param_ty.to_string() {
431-
sp = param.span;
427+
traits::OrphanCheckErr::UncoveredTy(uncovered_ty, local_type) => {
428+
struct TyParamFinder {
429+
ty_params: FxHashMap<Symbol, Span>,
430+
}
431+
432+
// FIXME: This only reports the uncovered type parameter it finds when in fact there
433+
// could be multiple. E.g., in `<Type<T, U> as Trait>::Assoc` for `<T, U>`.
434+
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for TyParamFinder {
435+
type BreakTy = (Symbol, Span);
436+
437+
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
438+
// FIXME: This doesn't respect macro hygiene.
439+
if let ty::Param(param_ty) = ty.kind()
440+
&& let Some(&span) = self.ty_params.get(&param_ty.name)
441+
{
442+
return ControlFlow::Break((param_ty.name, span));
443+
}
444+
445+
ty.super_visit_with(self)
432446
}
433447
}
434448

449+
let mut visitor = TyParamFinder {
450+
ty_params: generics
451+
.params
452+
.iter()
453+
.filter(|param| matches!(param.kind, hir::GenericParamKind::Type { .. }))
454+
.map(|param| (param.name.ident().name, param.span))
455+
.collect(),
456+
};
457+
458+
let ControlFlow::Break((param, span)) = uncovered_ty.visit_with(&mut visitor) else {
459+
bug!("failed to find ty param in {uncovered_ty}");
460+
};
461+
435462
match local_type {
436463
Some(local_type) => tcx.sess.emit_err(errors::TyParamFirstLocal {
437-
span: sp,
464+
span,
438465
note: (),
439-
param_ty,
466+
param,
440467
local_type,
441468
}),
442-
None => tcx.sess.emit_err(errors::TyParamSome { span: sp, note: (), param_ty }),
469+
None => tcx.sess.emit_err(errors::TyParamSome { span, note: (), param }),
443470
}
444471
}
445472
})

compiler/rustc_hir_analysis/src/errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1198,20 +1198,20 @@ pub struct TyParamFirstLocal<'a> {
11981198
pub span: Span,
11991199
#[note(hir_analysis_case_note)]
12001200
pub note: (),
1201-
pub param_ty: Ty<'a>,
1201+
pub param: Symbol,
12021202
pub local_type: Ty<'a>,
12031203
}
12041204

12051205
#[derive(Diagnostic)]
12061206
#[diag(hir_analysis_ty_param_some, code = "E0210")]
12071207
#[note]
1208-
pub struct TyParamSome<'a> {
1208+
pub struct TyParamSome {
12091209
#[primary_span]
12101210
#[label]
12111211
pub span: Span,
12121212
#[note(hir_analysis_only_note)]
12131213
pub note: (),
1214-
pub param_ty: Ty<'a>,
1214+
pub param: Symbol,
12151215
}
12161216

12171217
#[derive(Diagnostic)]

compiler/rustc_trait_selection/src/traits/coherence.rs

+45-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::traits::{
2020
};
2121
use rustc_data_structures::fx::FxIndexSet;
2222
use rustc_errors::Diagnostic;
23-
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
23+
use rustc_hir::def_id::DefId;
2424
use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt, TyCtxtInferExt};
2525
use rustc_infer::traits::{util, TraitEngine};
2626
use rustc_middle::traits::query::NoSolution;
@@ -620,7 +620,7 @@ pub fn trait_ref_is_local_or_fundamental<'tcx>(
620620
tcx: TyCtxt<'tcx>,
621621
trait_ref: ty::TraitRef<'tcx>,
622622
) -> bool {
623-
trait_ref.def_id.krate == LOCAL_CRATE || tcx.has_attr(trait_ref.def_id, sym::fundamental)
623+
trait_ref.def_id.is_local() || tcx.has_attr(trait_ref.def_id, sym::fundamental)
624624
}
625625

626626
#[derive(Debug)]
@@ -637,7 +637,7 @@ pub enum OrphanCheckErr<'tcx> {
637637
/// 2. Some local type must appear in `Self`.
638638
#[instrument(level = "debug", skip(tcx), ret)]
639639
pub fn orphan_check(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Result<(), OrphanCheckErr<'_>> {
640-
// We only except this routine to be invoked on implementations
640+
// We only accept this routine to be invoked on implementations
641641
// of a trait, not inherent implementations.
642642
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity();
643643
debug!(?trait_ref);
@@ -648,7 +648,38 @@ pub fn orphan_check(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Result<(), OrphanChe
648648
return Ok(());
649649
}
650650

651-
orphan_check_trait_ref::<!>(trait_ref, InCrate::Local, |ty| Ok(ty)).unwrap()
651+
let delay_bug = || {
652+
tcx.sess.delay_span_bug(
653+
tcx.def_span(impl_def_id),
654+
format!(
655+
"orphan check: failed to normalize `{trait_ref}` while checking {impl_def_id:?}"
656+
),
657+
)
658+
};
659+
660+
let infcx = tcx.infer_ctxt().build();
661+
let cause = ObligationCause::dummy();
662+
let param_env = tcx.param_env(impl_def_id);
663+
664+
let ocx = ObligationCtxt::new(&infcx);
665+
let trait_ref = ocx.normalize(&cause, param_env, trait_ref);
666+
let trait_ref = infcx.resolve_vars_if_possible(trait_ref);
667+
if !ocx.select_where_possible().is_empty() {
668+
delay_bug();
669+
}
670+
671+
orphan_check_trait_ref::<!>(trait_ref, InCrate::Local, |ty| {
672+
Ok(if infcx.next_trait_solver() && let ty::Alias(..) = ty.kind() {
673+
let mut fulfill_cx = <dyn TraitEngine<'_>>::new(&infcx);
674+
match infcx.at(&cause, param_env).structurally_normalize(ty, &mut *fulfill_cx) {
675+
Ok(ty) => ty,
676+
_ => Ty::new_error(tcx, delay_bug()),
677+
}
678+
} else {
679+
ty
680+
})
681+
})
682+
.unwrap()
652683
}
653684

654685
/// Checks whether a trait-ref is potentially implementable by a crate.
@@ -754,7 +785,7 @@ fn orphan_check_trait_ref<'tcx, E: Debug>(
754785
Ok(match trait_ref.visit_with(&mut checker) {
755786
ControlFlow::Continue(()) => Err(OrphanCheckErr::NonLocalInputType(checker.non_local_tys)),
756787
ControlFlow::Break(OrphanCheckEarlyExit::NormalizationFailure(err)) => return Err(err),
757-
ControlFlow::Break(OrphanCheckEarlyExit::ParamTy(ty)) => {
788+
ControlFlow::Break(OrphanCheckEarlyExit::UncoveredTy(ty)) => {
758789
// Does there exist some local type after the `ParamTy`.
759790
checker.search_first_local_ty = true;
760791
if let Some(OrphanCheckEarlyExit::LocalTy(local_ty)) =
@@ -798,11 +829,11 @@ where
798829
ControlFlow::Continue(())
799830
}
800831

801-
fn found_param_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<OrphanCheckEarlyExit<'tcx, E>> {
832+
fn found_uncovered_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<OrphanCheckEarlyExit<'tcx, E>> {
802833
if self.search_first_local_ty {
803834
ControlFlow::Continue(())
804835
} else {
805-
ControlFlow::Break(OrphanCheckEarlyExit::ParamTy(t))
836+
ControlFlow::Break(OrphanCheckEarlyExit::UncoveredTy(t))
806837
}
807838
}
808839

@@ -816,7 +847,7 @@ where
816847

817848
enum OrphanCheckEarlyExit<'tcx, E> {
818849
NormalizationFailure(E),
819-
ParamTy(Ty<'tcx>),
850+
UncoveredTy(Ty<'tcx>),
820851
LocalTy(Ty<'tcx>),
821852
}
822853

@@ -851,10 +882,14 @@ where
851882
| ty::Never
852883
| ty::Tuple(..)
853884
| ty::Alias(ty::Projection | ty::Inherent | ty::Weak, ..) => {
854-
self.found_non_local_ty(ty)
885+
if ty.has_type_flags(ty::TypeFlags::HAS_TY_PARAM) {
886+
self.found_uncovered_ty(ty)
887+
} else {
888+
ControlFlow::Continue(())
889+
}
855890
}
856891

857-
ty::Param(..) => self.found_param_ty(ty),
892+
ty::Param(..) => self.found_uncovered_ty(ty),
858893

859894
ty::Placeholder(..) | ty::Bound(..) | ty::Infer(..) => match self.in_crate {
860895
InCrate::Local => self.found_non_local_ty(ty),

tests/ui/associated-types/issue-38821.rs

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub trait Column: Expression {}
2222

2323
#[derive(Debug, Copy, Clone)]
2424
//~^ ERROR the trait bound `<Col as Expression>::SqlType: NotNull` is not satisfied
25+
//~| ERROR the trait bound `<Col as Expression>::SqlType: NotNull` is not satisfied
26+
//~| ERROR the trait bound `<Col as Expression>::SqlType: NotNull` is not satisfied
2527
pub enum ColumnInsertValue<Col, Expr> where
2628
Col: Column,
2729
Expr: Expression<SqlType=<Col::SqlType as IntoNullable>::Nullable>,

tests/ui/associated-types/issue-38821.stderr

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
error[E0277]: the trait bound `<Col as Expression>::SqlType: NotNull` is not satisfied
2+
--> $DIR/issue-38821.rs:23:10
3+
|
4+
LL | #[derive(Debug, Copy, Clone)]
5+
| ^^^^^ the trait `NotNull` is not implemented for `<Col as Expression>::SqlType`
6+
|
7+
note: required for `<Col as Expression>::SqlType` to implement `IntoNullable`
8+
--> $DIR/issue-38821.rs:9:18
9+
|
10+
LL | impl<T: NotNull> IntoNullable for T {
11+
| ------- ^^^^^^^^^^^^ ^
12+
| |
13+
| unsatisfied trait bound introduced here
14+
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
15+
help: consider further restricting the associated type
16+
|
17+
LL | Expr: Expression<SqlType=<Col::SqlType as IntoNullable>::Nullable>, <Col as Expression>::SqlType: NotNull,
18+
| +++++++++++++++++++++++++++++++++++++++
19+
120
error[E0277]: the trait bound `<Col as Expression>::SqlType: NotNull` is not satisfied
221
--> $DIR/issue-38821.rs:23:17
322
|
@@ -17,6 +36,25 @@ help: consider further restricting the associated type
1736
LL | Expr: Expression<SqlType=<Col::SqlType as IntoNullable>::Nullable>, <Col as Expression>::SqlType: NotNull,
1837
| +++++++++++++++++++++++++++++++++++++++
1938

20-
error: aborting due to previous error
39+
error[E0277]: the trait bound `<Col as Expression>::SqlType: NotNull` is not satisfied
40+
--> $DIR/issue-38821.rs:23:23
41+
|
42+
LL | #[derive(Debug, Copy, Clone)]
43+
| ^^^^^ the trait `NotNull` is not implemented for `<Col as Expression>::SqlType`
44+
|
45+
note: required for `<Col as Expression>::SqlType` to implement `IntoNullable`
46+
--> $DIR/issue-38821.rs:9:18
47+
|
48+
LL | impl<T: NotNull> IntoNullable for T {
49+
| ------- ^^^^^^^^^^^^ ^
50+
| |
51+
| unsatisfied trait bound introduced here
52+
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
53+
help: consider further restricting the associated type
54+
|
55+
LL | Expr: Expression<SqlType=<Col::SqlType as IntoNullable>::Nullable>, <Col as Expression>::SqlType: NotNull,
56+
| +++++++++++++++++++++++++++++++++++++++
57+
58+
error: aborting due to 3 previous errors
2159

2260
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub trait Trait0<T, U, V> {}
2+
pub trait Trait1<T, U> {}

tests/ui/coherence/coherence-impls-copy.stderr

+4-16
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0117]: only traits defined in the current crate can be implemented for pr
22
--> $DIR/coherence-impls-copy.rs:5:1
33
|
44
LL | impl Copy for i32 {}
5-
| ^^^^^^^^^^^^^^---
6-
| | |
7-
| | `i32` is not defined in the current crate
8-
| impl doesn't use only types from inside the current crate
5+
| ^^^^^^^^^^^^^^^^^ impl doesn't use only types from inside the current crate
96
|
107
= note: define and implement a trait or new type instead
118

@@ -23,32 +20,23 @@ error[E0117]: only traits defined in the current crate can be implemented for ar
2320
--> $DIR/coherence-impls-copy.rs:33:1
2421
|
2522
LL | impl Copy for &'static [NotSync] {}
26-
| ^^^^^^^^^^^^^^------------------
27-
| | |
28-
| | this is not defined in the current crate because slices are always foreign
29-
| impl doesn't use only types from inside the current crate
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl doesn't use only types from inside the current crate
3024
|
3125
= note: define and implement a trait or new type instead
3226

3327
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
3428
--> $DIR/coherence-impls-copy.rs:25:1
3529
|
3630
LL | impl Copy for (MyType, MyType) {}
37-
| ^^^^^^^^^^^^^^----------------
38-
| | |
39-
| | this is not defined in the current crate because tuples are always foreign
40-
| impl doesn't use only types from inside the current crate
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl doesn't use only types from inside the current crate
4132
|
4233
= note: define and implement a trait or new type instead
4334

4435
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
4536
--> $DIR/coherence-impls-copy.rs:30:1
4637
|
4738
LL | impl Copy for [MyType] {}
48-
| ^^^^^^^^^^^^^^--------
49-
| | |
50-
| | this is not defined in the current crate because slices are always foreign
51-
| impl doesn't use only types from inside the current crate
39+
| ^^^^^^^^^^^^^^^^^^^^^^ impl doesn't use only types from inside the current crate
5240
|
5341
= note: define and implement a trait or new type instead
5442

tests/ui/coherence/coherence-impls-send.stderr

+3-12
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,15 @@ error[E0117]: only traits defined in the current crate can be implemented for ar
22
--> $DIR/coherence-impls-send.rs:25:1
33
|
44
LL | unsafe impl Send for &'static [NotSync] {}
5-
| ^^^^^^^^^^^^^^^^^^^^^------------------
6-
| | |
7-
| | this is not defined in the current crate because slices are always foreign
8-
| impl doesn't use only types from inside the current crate
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl doesn't use only types from inside the current crate
96
|
107
= note: define and implement a trait or new type instead
118

129
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
1310
--> $DIR/coherence-impls-send.rs:16:1
1411
|
1512
LL | unsafe impl Send for (MyType, MyType) {}
16-
| ^^^^^^^^^^^^^^^^^^^^^----------------
17-
| | |
18-
| | this is not defined in the current crate because tuples are always foreign
19-
| impl doesn't use only types from inside the current crate
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl doesn't use only types from inside the current crate
2014
|
2115
= note: define and implement a trait or new type instead
2216

@@ -30,10 +24,7 @@ error[E0117]: only traits defined in the current crate can be implemented for ar
3024
--> $DIR/coherence-impls-send.rs:22:1
3125
|
3226
LL | unsafe impl Send for [MyType] {}
33-
| ^^^^^^^^^^^^^^^^^^^^^--------
34-
| | |
35-
| | this is not defined in the current crate because slices are always foreign
36-
| impl doesn't use only types from inside the current crate
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl doesn't use only types from inside the current crate
3728
|
3829
= note: define and implement a trait or new type instead
3930

0 commit comments

Comments
 (0)