1
Fork 0

Rollup merge of #112508 - compiler-errors:trait-sig-lifetime-sugg-ice, r=cjgillot

Tweak spans for self arg, fix borrow suggestion for signature mismatch

1. Adjust a suggestion message that was annoying me
2. Fix #112503 by recording the right spans for the `self` part of the `&self` 0th argument
3. Remove the suggestion for adjusting a trait signature on type mismatch, bc that's gonna probably break all the other impls of the trait even if it fixes its one usage 😅
This commit is contained in:
Matthias Krüger 2023-07-22 19:57:35 +02:00 committed by GitHub
commit 0ed5f091a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 139 additions and 39 deletions

View file

@ -1,5 +1,8 @@
use crate::fluent_generated as fluent;
use rustc_errors::{ErrorGuaranteed, Handler, IntoDiagnostic};
use rustc_errors::{
AddToDiagnostic, Applicability, Diagnostic, ErrorGuaranteed, Handler, IntoDiagnostic,
SubdiagnosticMessage,
};
use rustc_macros::Diagnostic;
use rustc_middle::ty::{self, PolyTraitRef, Ty};
use rustc_span::{Span, Symbol};
@ -97,3 +100,34 @@ pub struct InherentProjectionNormalizationOverflow {
pub span: Span,
pub ty: String,
}
pub enum AdjustSignatureBorrow {
Borrow { to_borrow: Vec<(Span, String)> },
RemoveBorrow { remove_borrow: Vec<(Span, String)> },
}
impl AddToDiagnostic for AdjustSignatureBorrow {
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
where
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
{
match self {
AdjustSignatureBorrow::Borrow { to_borrow } => {
diag.set_arg("len", to_borrow.len());
diag.multipart_suggestion_verbose(
fluent::trait_selection_adjust_signature_borrow,
to_borrow,
Applicability::MaybeIncorrect,
);
}
AdjustSignatureBorrow::RemoveBorrow { remove_borrow } => {
diag.set_arg("len", remove_borrow.len());
diag.multipart_suggestion_verbose(
fluent::trait_selection_adjust_signature_remove_borrow,
remove_borrow,
Applicability::MaybeIncorrect,
);
}
}
}
}

View file

@ -5,6 +5,7 @@ use super::{
PredicateObligation,
};
use crate::errors;
use crate::infer::InferCtxt;
use crate::traits::{NormalizeExt, ObligationCtxt};
@ -4031,6 +4032,10 @@ fn hint_missing_borrow<'tcx>(
found_node: Node<'_>,
err: &mut Diagnostic,
) {
if matches!(found_node, Node::TraitItem(..)) {
return;
}
let found_args = match found.kind() {
ty::FnPtr(f) => infcx.instantiate_binder_with_placeholders(*f).inputs().iter(),
kind => {
@ -4102,19 +4107,11 @@ fn hint_missing_borrow<'tcx>(
}
if !to_borrow.is_empty() {
err.multipart_suggestion_verbose(
"consider borrowing the argument",
to_borrow,
Applicability::MaybeIncorrect,
);
err.subdiagnostic(errors::AdjustSignatureBorrow::Borrow { to_borrow });
}
if !remove_borrow.is_empty() {
err.multipart_suggestion_verbose(
"do not borrow the argument",
remove_borrow,
Applicability::MaybeIncorrect,
);
err.subdiagnostic(errors::AdjustSignatureBorrow::RemoveBorrow { remove_borrow });
}
}