Skip to content

Commit 2e677c0

Browse files
committed
Auto merge of rust-lang#106616 - compiler-errors:rollup-emcj0o3, r=compiler-errors
Rollup of 8 pull requests Successful merges: - rust-lang#104163 (Don't derive Debug for `OnceWith` & `RepeatWith`) - rust-lang#106131 (Mention "signature" rather than "fn pointer" when impl/trait methods are incompatible) - rust-lang#106363 (Structured suggestion for `&mut dyn Iterator` when possible) - rust-lang#106497 (Suggest using clone when we have &T and T implemented Clone) - rust-lang#106584 (Document that `Vec::from_raw_parts[_in]` must be given a pointer from the correct allocator.) - rust-lang#106600 (Suppress type errors that come from private fields) - rust-lang#106602 (Add goml scripts to tidy checks) - rust-lang#106606 (Do not emit structured suggestion for turbofish with wrong span) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents a377893 + 5e8e97f commit 2e677c0

File tree

75 files changed

+594
-204
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+594
-204
lines changed

compiler/rustc_error_messages/locales/en-US/infer.ftl

+2-2
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ infer_trait_placeholder_mismatch = implementation of `{$trait_def_id}` is not ge
253253
infer_trait_impl_diff = `impl` item signature doesn't match `trait` item signature
254254
.found = found `{$found}`
255255
.expected = expected `{$expected}`
256-
.expected_found = expected `{$expected}`
257-
{" "}found `{$found}`
256+
.expected_found = expected signature `{$expected}`
257+
{" "}found signature `{$found}`
258258
259259
infer_tid_rel_help = verify the lifetime relationships in the `trait` and `impl` between the `self` argument, the other inputs and its output
260260
infer_tid_consider_borrowing = consider borrowing this type parameter in the trait

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+14-22
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ fn compare_method_predicate_entailment<'tcx>(
270270
let unnormalized_impl_fty = tcx.mk_fn_ptr(ty::Binder::dummy(unnormalized_impl_sig));
271271

272272
let norm_cause = ObligationCause::misc(impl_m_span, impl_m_hir_id);
273-
let impl_fty = ocx.normalize(&norm_cause, param_env, unnormalized_impl_fty);
274-
debug!("compare_impl_method: impl_fty={:?}", impl_fty);
273+
let impl_sig = ocx.normalize(&norm_cause, param_env, unnormalized_impl_sig);
274+
debug!("compare_impl_method: impl_fty={:?}", impl_sig);
275275

276276
let trait_sig = tcx.bound_fn_sig(trait_m.def_id).subst(tcx, trait_to_placeholder_substs);
277277
let trait_sig = tcx.liberate_late_bound_regions(impl_m.def_id, trait_sig);
@@ -294,18 +294,17 @@ fn compare_method_predicate_entailment<'tcx>(
294294
// type would be more appropriate. In other places we have a `Vec<Span>`
295295
// corresponding to their `Vec<Predicate>`, but we don't have that here.
296296
// Fixing this would improve the output of test `issue-83765.rs`.
297-
let result = ocx.sup(&cause, param_env, trait_fty, impl_fty);
297+
let result = ocx.sup(&cause, param_env, trait_sig, impl_sig);
298298

299299
if let Err(terr) = result {
300-
debug!(?terr, "sub_types failed: impl ty {:?}, trait ty {:?}", impl_fty, trait_fty);
300+
debug!(?impl_sig, ?trait_sig, ?terr, "sub_types failed");
301301

302302
let emitted = report_trait_method_mismatch(
303303
&infcx,
304304
cause,
305305
terr,
306-
(trait_m, trait_fty),
307-
(impl_m, impl_fty),
308-
trait_sig,
306+
(trait_m, trait_sig),
307+
(impl_m, impl_sig),
309308
impl_trait_ref,
310309
);
311310
return Err(emitted);
@@ -484,7 +483,8 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
484483
let impl_trait_ref = tcx.impl_trait_ref(impl_m.impl_container(tcx).unwrap()).unwrap();
485484
let param_env = tcx.param_env(def_id);
486485

487-
// First, check a few of the same thing as `compare_impl_method`, just so we don't ICE during substitutions later.
486+
// First, check a few of the same things as `compare_impl_method`,
487+
// just so we don't ICE during substitution later.
488488
compare_number_of_generics(tcx, impl_m, trait_m, tcx.hir().span_if_local(impl_m.def_id), true)?;
489489
compare_generic_param_kinds(tcx, impl_m, trait_m, true)?;
490490
check_region_bounds_on_impl_item(tcx, impl_m, trait_m, true)?;
@@ -577,14 +577,11 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
577577

578578
debug!(?trait_sig, ?impl_sig, "equating function signatures");
579579

580-
let trait_fty = tcx.mk_fn_ptr(ty::Binder::dummy(trait_sig));
581-
let impl_fty = tcx.mk_fn_ptr(ty::Binder::dummy(impl_sig));
582-
583580
// Unify the whole function signature. We need to do this to fully infer
584581
// the lifetimes of the return type, but do this after unifying just the
585582
// return types, since we want to avoid duplicating errors from
586583
// `compare_method_predicate_entailment`.
587-
match ocx.eq(&cause, param_env, trait_fty, impl_fty) {
584+
match ocx.eq(&cause, param_env, trait_sig, impl_sig) {
588585
Ok(()) => {}
589586
Err(terr) => {
590587
// This function gets called during `compare_method_predicate_entailment` when normalizing a
@@ -595,9 +592,8 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
595592
infcx,
596593
cause,
597594
terr,
598-
(trait_m, trait_fty),
599-
(impl_m, impl_fty),
600-
trait_sig,
595+
(trait_m, trait_sig),
596+
(impl_m, impl_sig),
601597
impl_trait_ref,
602598
);
603599
return Err(emitted);
@@ -771,9 +767,8 @@ fn report_trait_method_mismatch<'tcx>(
771767
infcx: &InferCtxt<'tcx>,
772768
mut cause: ObligationCause<'tcx>,
773769
terr: TypeError<'tcx>,
774-
(trait_m, trait_fty): (&ty::AssocItem, Ty<'tcx>),
775-
(impl_m, impl_fty): (&ty::AssocItem, Ty<'tcx>),
776-
trait_sig: ty::FnSig<'tcx>,
770+
(trait_m, trait_sig): (&ty::AssocItem, ty::FnSig<'tcx>),
771+
(impl_m, impl_sig): (&ty::AssocItem, ty::FnSig<'tcx>),
777772
impl_trait_ref: ty::TraitRef<'tcx>,
778773
) -> ErrorGuaranteed {
779774
let tcx = infcx.tcx;
@@ -858,10 +853,7 @@ fn report_trait_method_mismatch<'tcx>(
858853
&mut diag,
859854
&cause,
860855
trait_err_span.map(|sp| (sp, "type in trait".to_owned())),
861-
Some(infer::ValuePairs::Terms(ExpectedFound {
862-
expected: trait_fty.into(),
863-
found: impl_fty.into(),
864-
})),
856+
Some(infer::ValuePairs::Sigs(ExpectedFound { expected: trait_sig, found: impl_sig })),
865857
terr,
866858
false,
867859
false,

compiler/rustc_hir_typeck/src/demand.rs

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5757
|| self.suggest_boxing_when_appropriate(err, expr, expected, expr_ty)
5858
|| self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected)
5959
|| self.suggest_copied_or_cloned(err, expr, expr_ty, expected)
60+
|| self.suggest_clone_for_ref(err, expr, expr_ty, expected)
6061
|| self.suggest_into(err, expr, expr_ty, expected)
6162
|| self.suggest_floating_point_literal(err, expr, expected);
6263
if !suggested {

compiler/rustc_hir_typeck/src/expr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2217,7 +2217,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22172217
self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
22182218
return field_ty;
22192219
}
2220-
private_candidate = Some((adjustments, base_def.did(), field_ty));
2220+
private_candidate = Some((adjustments, base_def.did()));
22212221
}
22222222
}
22232223
ty::Tuple(tys) => {
@@ -2240,12 +2240,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22402240
}
22412241
self.structurally_resolved_type(autoderef.span(), autoderef.final_ty(false));
22422242

2243-
if let Some((adjustments, did, field_ty)) = private_candidate {
2243+
if let Some((adjustments, did)) = private_candidate {
22442244
// (#90483) apply adjustments to avoid ExprUseVisitor from
22452245
// creating erroneous projection.
22462246
self.apply_adjustments(base, adjustments);
22472247
self.ban_private_field_access(expr, base_ty, field, did);
2248-
return field_ty;
2248+
return self.tcx().ty_error();
22492249
}
22502250

22512251
if field.name == kw::Empty {

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+30
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10141014
}
10151015
}
10161016

1017+
pub(crate) fn suggest_clone_for_ref(
1018+
&self,
1019+
diag: &mut Diagnostic,
1020+
expr: &hir::Expr<'_>,
1021+
expr_ty: Ty<'tcx>,
1022+
expected_ty: Ty<'tcx>,
1023+
) -> bool {
1024+
if let ty::Ref(_, inner_ty, hir::Mutability::Not) = expr_ty.kind()
1025+
&& let Some(clone_trait_def) = self.tcx.lang_items().clone_trait()
1026+
&& expected_ty == *inner_ty
1027+
&& self
1028+
.infcx
1029+
.type_implements_trait(
1030+
clone_trait_def,
1031+
[self.tcx.erase_regions(expected_ty)],
1032+
self.param_env
1033+
)
1034+
.must_apply_modulo_regions()
1035+
{
1036+
diag.span_suggestion_verbose(
1037+
expr.span.shrink_to_hi(),
1038+
"consider using clone here",
1039+
".clone()",
1040+
Applicability::MachineApplicable,
1041+
);
1042+
return true;
1043+
}
1044+
false
1045+
}
1046+
10171047
pub(crate) fn suggest_copied_or_cloned(
10181048
&self,
10191049
diag: &mut Diagnostic,

compiler/rustc_hir_typeck/src/method/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ pub enum MethodError<'tcx> {
5757
PrivateMatch(DefKind, DefId, Vec<DefId>),
5858

5959
// Found a `Self: Sized` bound where `Self` is a trait object.
60-
IllegalSizedBound(Vec<DefId>, bool, Span),
60+
IllegalSizedBound {
61+
candidates: Vec<DefId>,
62+
needs_mut: bool,
63+
bound_span: Span,
64+
self_expr: &'tcx hir::Expr<'tcx>,
65+
},
6166

6267
// Found a match, but the return type is wrong
6368
BadReturnType,
@@ -112,7 +117,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
112117
Err(NoMatch(..)) => false,
113118
Err(Ambiguity(..)) => true,
114119
Err(PrivateMatch(..)) => allow_private,
115-
Err(IllegalSizedBound(..)) => true,
120+
Err(IllegalSizedBound { .. }) => true,
116121
Err(BadReturnType) => bug!("no return type expectations but got BadReturnType"),
117122
}
118123
}
@@ -236,7 +241,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
236241
_ => Vec::new(),
237242
};
238243

239-
return Err(IllegalSizedBound(candidates, needs_mut, span));
244+
return Err(IllegalSizedBound { candidates, needs_mut, bound_span: span, self_expr });
240245
}
241246

242247
Ok(result.callee)

compiler/rustc_hir_typeck/src/method/suggest.rs

+37-4
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
176176
err.emit();
177177
}
178178

179-
MethodError::IllegalSizedBound(candidates, needs_mut, bound_span) => {
180-
let msg = format!("the `{}` method cannot be invoked on a trait object", item_name);
179+
MethodError::IllegalSizedBound { candidates, needs_mut, bound_span, self_expr } => {
180+
let msg = if needs_mut {
181+
with_forced_trimmed_paths!(format!(
182+
"the `{item_name}` method cannot be invoked on `{rcvr_ty}`"
183+
))
184+
} else {
185+
format!("the `{item_name}` method cannot be invoked on a trait object")
186+
};
181187
let mut err = self.sess().struct_span_err(span, &msg);
182-
err.span_label(bound_span, "this has a `Sized` requirement");
188+
if !needs_mut {
189+
err.span_label(bound_span, "this has a `Sized` requirement");
190+
}
183191
if !candidates.is_empty() {
184192
let help = format!(
185193
"{an}other candidate{s} {were} found in the following trait{s}, perhaps \
@@ -197,7 +205,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
197205
*region,
198206
ty::TypeAndMut { ty: *t_type, mutbl: mutability.invert() },
199207
);
200-
err.note(&format!("you need `{}` instead of `{}`", trait_type, rcvr_ty));
208+
let msg = format!("you need `{}` instead of `{}`", trait_type, rcvr_ty);
209+
let mut kind = &self_expr.kind;
210+
while let hir::ExprKind::AddrOf(_, _, expr)
211+
| hir::ExprKind::Unary(hir::UnOp::Deref, expr) = kind
212+
{
213+
kind = &expr.kind;
214+
}
215+
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = kind
216+
&& let hir::def::Res::Local(hir_id) = path.res
217+
&& let Some(hir::Node::Pat(b)) = self.tcx.hir().find(hir_id)
218+
&& let Some(hir::Node::Param(p)) = self.tcx.hir().find_parent(b.hir_id)
219+
&& let Some(node) = self.tcx.hir().find_parent(p.hir_id)
220+
&& let Some(decl) = node.fn_decl()
221+
&& let Some(ty) = decl.inputs.iter().find(|ty| ty.span == p.ty_span)
222+
&& let hir::TyKind::Ref(_, mut_ty) = &ty.kind
223+
&& let hir::Mutability::Not = mut_ty.mutbl
224+
{
225+
err.span_suggestion_verbose(
226+
mut_ty.ty.span.shrink_to_lo(),
227+
&msg,
228+
"mut ",
229+
Applicability::MachineApplicable,
230+
);
231+
} else {
232+
err.help(&msg);
233+
}
201234
}
202235
}
203236
err.emit();

compiler/rustc_infer/src/infer/at.rs

+12
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,15 @@ impl<'tcx> ToTrace<'tcx> for ty::AliasTy<'tcx> {
427427
}
428428
}
429429
}
430+
431+
impl<'tcx> ToTrace<'tcx> for ty::FnSig<'tcx> {
432+
fn to_trace(
433+
_: TyCtxt<'tcx>,
434+
cause: &ObligationCause<'tcx>,
435+
a_is_expected: bool,
436+
a: Self,
437+
b: Self,
438+
) -> TypeTrace<'tcx> {
439+
TypeTrace { cause: cause.clone(), values: Sigs(ExpectedFound::new(a_is_expected, a, b)) }
440+
}
441+
}

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -1428,8 +1428,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
14281428
impl<'tcx> OpaqueTypesVisitor<'tcx> {
14291429
fn visit_expected_found(
14301430
tcx: TyCtxt<'tcx>,
1431-
expected: Ty<'tcx>,
1432-
found: Ty<'tcx>,
1431+
expected: impl TypeVisitable<'tcx>,
1432+
found: impl TypeVisitable<'tcx>,
14331433
ignore_span: Span,
14341434
) -> Self {
14351435
let mut types_visitor = OpaqueTypesVisitor {
@@ -1569,6 +1569,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
15691569
_ => (false, Mismatch::Fixed("type")),
15701570
}
15711571
}
1572+
ValuePairs::Sigs(infer::ExpectedFound { expected, found }) => {
1573+
OpaqueTypesVisitor::visit_expected_found(self.tcx, expected, found, span)
1574+
.report(diag);
1575+
(false, Mismatch::Fixed("signature"))
1576+
}
15721577
ValuePairs::TraitRefs(_) | ValuePairs::PolyTraitRefs(_) => {
15731578
(false, Mismatch::Fixed("trait"))
15741579
}
@@ -2040,6 +2045,17 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
20402045
ret => ret,
20412046
}
20422047
}
2048+
infer::Sigs(exp_found) => {
2049+
let exp_found = self.resolve_vars_if_possible(exp_found);
2050+
if exp_found.references_error() {
2051+
return None;
2052+
}
2053+
let (exp, fnd) = self.cmp_fn_sig(
2054+
&ty::Binder::dummy(exp_found.expected),
2055+
&ty::Binder::dummy(exp_found.found),
2056+
);
2057+
Some((exp, fnd, None, None))
2058+
}
20432059
}
20442060
}
20452061

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs

+20-14
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
use crate::errors::{ConsiderBorrowingParamHelp, RelationshipHelp, TraitImplDiff};
44
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
55
use crate::infer::lexical_region_resolve::RegionResolutionError;
6-
use crate::infer::Subtype;
6+
use crate::infer::{Subtype, ValuePairs};
77
use crate::traits::ObligationCauseCode::CompareImplItemObligation;
88
use rustc_errors::ErrorGuaranteed;
99
use rustc_hir as hir;
1010
use rustc_hir::def::Res;
1111
use rustc_hir::def_id::DefId;
1212
use rustc_hir::intravisit::Visitor;
1313
use rustc_middle::hir::nested_filter;
14+
use rustc_middle::ty::error::ExpectedFound;
1415
use rustc_middle::ty::print::RegionHighlightMode;
1516
use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor};
1617
use rustc_span::Span;
@@ -23,22 +24,27 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
2324
let error = self.error.as_ref()?;
2425
debug!("try_report_impl_not_conforming_to_trait {:?}", error);
2526
if let RegionResolutionError::SubSupConflict(
26-
_,
27-
var_origin,
28-
sub_origin,
29-
_sub,
30-
sup_origin,
31-
_sup,
32-
_,
33-
) = error.clone()
27+
_,
28+
var_origin,
29+
sub_origin,
30+
_sub,
31+
sup_origin,
32+
_sup,
33+
_,
34+
) = error.clone()
3435
&& let (Subtype(sup_trace), Subtype(sub_trace)) = (&sup_origin, &sub_origin)
35-
&& let sub_expected_found @ Some((sub_expected, sub_found)) = sub_trace.values.ty()
36-
&& let sup_expected_found @ Some(_) = sup_trace.values.ty()
3736
&& let CompareImplItemObligation { trait_item_def_id, .. } = sub_trace.cause.code()
38-
&& sup_expected_found == sub_expected_found
37+
&& sub_trace.values == sup_trace.values
38+
&& let ValuePairs::Sigs(ExpectedFound { expected, found }) = sub_trace.values
3939
{
40-
let guar =
41-
self.emit_err(var_origin.span(), sub_expected, sub_found, *trait_item_def_id);
40+
// FIXME(compiler-errors): Don't like that this needs `Ty`s, but
41+
// all of the region highlighting machinery only deals with those.
42+
let guar = self.emit_err(
43+
var_origin.span(),
44+
self.cx.tcx.mk_fn_ptr(ty::Binder::dummy(expected)),
45+
self.cx.tcx.mk_fn_ptr(ty::Binder::dummy(found)),
46+
*trait_item_def_id,
47+
);
4248
return Some(guar);
4349
}
4450
None

compiler/rustc_infer/src/infer/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ pub enum ValuePairs<'tcx> {
361361
Terms(ExpectedFound<ty::Term<'tcx>>),
362362
TraitRefs(ExpectedFound<ty::TraitRef<'tcx>>),
363363
PolyTraitRefs(ExpectedFound<ty::PolyTraitRef<'tcx>>),
364+
Sigs(ExpectedFound<ty::FnSig<'tcx>>),
364365
}
365366

366367
impl<'tcx> ValuePairs<'tcx> {

0 commit comments

Comments
 (0)