Skip to content

Commit 7f99536

Browse files
authored
Rollup merge of #93714 - compiler-errors:can-type-impl-copy-error-span, r=jackh726
better ObligationCause for normalization errors in `can_type_implement_copy` Some logic is needed so we can point to the field when given totally nonsense types like `struct Foo(<u32 as Iterator>::Item);` Fixes #93687
2 parents 6ba167a + ee98dc8 commit 7f99536

File tree

6 files changed

+65
-5
lines changed

6 files changed

+65
-5
lines changed

compiler/rustc_lint/src/builtin.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use rustc_span::source_map::Spanned;
5151
use rustc_span::symbol::{kw, sym, Ident, Symbol};
5252
use rustc_span::{BytePos, InnerSpan, MultiSpan, Span};
5353
use rustc_target::abi::VariantIdx;
54-
use rustc_trait_selection::traits::misc::can_type_implement_copy;
54+
use rustc_trait_selection::traits::{self, misc::can_type_implement_copy};
5555

5656
use crate::nonstandard_style::{method_context, MethodLateContext};
5757

@@ -764,7 +764,14 @@ impl<'tcx> LateLintPass<'tcx> for MissingCopyImplementations {
764764
if ty.is_copy_modulo_regions(cx.tcx.at(item.span), param_env) {
765765
return;
766766
}
767-
if can_type_implement_copy(cx.tcx, param_env, ty).is_ok() {
767+
if can_type_implement_copy(
768+
cx.tcx,
769+
param_env,
770+
ty,
771+
traits::ObligationCause::misc(item.span, item.hir_id()),
772+
)
773+
.is_ok()
774+
{
768775
cx.struct_span_lint(MISSING_COPY_IMPLEMENTATIONS, item.span, |lint| {
769776
lint.build(
770777
"type could implement `Copy`; consider adding `impl \

compiler/rustc_trait_selection/src/traits/misc.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub fn can_type_implement_copy<'tcx>(
2020
tcx: TyCtxt<'tcx>,
2121
param_env: ty::ParamEnv<'tcx>,
2222
self_type: Ty<'tcx>,
23+
cause: ObligationCause<'tcx>,
2324
) -> Result<(), CopyImplementationError<'tcx>> {
2425
// FIXME: (@jroesch) float this code up
2526
tcx.infer_ctxt().enter(|infcx| {
@@ -49,7 +50,19 @@ pub fn can_type_implement_copy<'tcx>(
4950
continue;
5051
}
5152
let span = tcx.def_span(field.did);
52-
let cause = ObligationCause::dummy_with_span(span);
53+
// FIXME(compiler-errors): This gives us better spans for bad
54+
// projection types like in issue-50480.
55+
// If the ADT has substs, point to the cause we are given.
56+
// If it does not, then this field probably doesn't normalize
57+
// to begin with, and point to the bad field's span instead.
58+
let cause = if field
59+
.ty(tcx, traits::InternalSubsts::identity_for_item(tcx, adt.did))
60+
.has_param_types_or_consts()
61+
{
62+
cause.clone()
63+
} else {
64+
ObligationCause::dummy_with_span(span)
65+
};
5366
let ctx = traits::FulfillmentContext::new();
5467
match traits::fully_normalize(&infcx, ctx, cause, param_env, ty) {
5568
Ok(ty) => {

compiler/rustc_typeck/src/coherence/builtin.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
7474

7575
debug!("visit_implementation_of_copy: self_type={:?} (free)", self_type);
7676

77-
match can_type_implement_copy(tcx, param_env, self_type) {
77+
let cause = traits::ObligationCause::misc(span, impl_hir_id);
78+
match can_type_implement_copy(tcx, param_env, self_type, cause) {
7879
Ok(()) => {}
7980
Err(CopyImplementationError::InfrigingFields(fields)) => {
8081
let item = tcx.hir().expect_item(impl_did);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
trait TraitFoo {
2+
type Bar;
3+
}
4+
5+
struct Foo<T>
6+
where
7+
T: TraitFoo,
8+
{
9+
inner: T::Bar,
10+
}
11+
12+
impl<T> Clone for Foo<T>
13+
where
14+
T: TraitFoo,
15+
T::Bar: Clone,
16+
{
17+
fn clone(&self) -> Self {
18+
Self { inner: self.inner.clone() }
19+
}
20+
}
21+
22+
impl<T> Copy for Foo<T> {}
23+
//~^ ERROR the trait bound `T: TraitFoo` is not satisfied
24+
25+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0277]: the trait bound `T: TraitFoo` is not satisfied
2+
--> $DIR/copy-impl-cannot-normalize.rs:22:1
3+
|
4+
LL | impl<T> Copy for Foo<T> {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `TraitFoo` is not implemented for `T`
6+
|
7+
help: consider restricting type parameter `T`
8+
|
9+
LL | impl<T: TraitFoo> Copy for Foo<T> {}
10+
| ++++++++++
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0277`.

src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
199199
let sugg = |diag: &mut DiagnosticBuilder<'_>| {
200200
if let ty::Adt(def, ..) = ty.kind() {
201201
if let Some(span) = cx.tcx.hir().span_if_local(def.did) {
202-
if can_type_implement_copy(cx.tcx, cx.param_env, ty).is_ok() {
202+
if can_type_implement_copy(cx.tcx, cx.param_env, ty, traits::ObligationCause::dummy_with_span(span)).is_ok() {
203203
diag.span_help(span, "consider marking this type as `Copy`");
204204
}
205205
}

0 commit comments

Comments
 (0)