Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve compiler error message for wrong generic parameter order #72271

Merged
merged 1 commit into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 67 additions & 6 deletions src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
use crate::collect::PlaceholderHirTyCollector;
use crate::middle::resolve_lifetime as rl;
use crate::require_c_abi_if_c_variadic;
use rustc_ast::ast::ParamKindOrd;
use rustc_ast::util::lev_distance::find_best_match_for_name;
use rustc_ast::{ast::ParamKindOrd, util::lev_distance::find_best_match_for_name};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::ErrorReported;
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId, FatalError};
Expand All @@ -27,7 +26,7 @@ use rustc_middle::ty::{GenericParamDef, GenericParamDefKind};
use rustc_session::lint::builtin::{AMBIGUOUS_ASSOCIATED_ITEMS, LATE_BOUND_LIFETIME_ARGUMENTS};
use rustc_session::parse::feature_err;
use rustc_session::Session;
use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{MultiSpan, Span, DUMMY_SP};
use rustc_target::spec::abi;
use rustc_trait_selection::traits;
Expand Down Expand Up @@ -475,7 +474,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {

/// Report an error that a generic argument did not match the generic parameter that was
/// expected.
fn generic_arg_mismatch_err(sess: &Session, arg: &GenericArg<'_>, kind: &'static str) {
fn generic_arg_mismatch_err(
sess: &Session,
arg: &GenericArg<'_>,
kind: &'static str,
help: Option<&str>,
) {
let mut err = struct_span_err!(
sess,
arg.span(),
Expand Down Expand Up @@ -503,6 +507,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let (first, last) =
if kind_ord < arg_ord { (kind, arg.descr()) } else { (arg.descr(), kind) };
err.note(&format!("{} arguments must be provided before {} arguments", first, last));

if let Some(help) = help {
err.help(help);
}
err.emit();
}

Expand Down Expand Up @@ -648,7 +656,60 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
if arg_count.correct.is_ok()
&& arg_count.explicit_late_bound == ExplicitLateBound::No
{
Self::generic_arg_mismatch_err(tcx.sess, arg, kind.descr());
// We're going to iterate over the parameters to sort them out, and
// show that order to the user as a possible order for the parameters
let mut param_types_present = defs
.params
.clone()
.into_iter()
.map(|param| {
(
match param.kind {
GenericParamDefKind::Lifetime => {
ParamKindOrd::Lifetime
}
GenericParamDefKind::Type { .. } => {
ParamKindOrd::Type
}
GenericParamDefKind::Const => {
ParamKindOrd::Const
}
},
param,
)
})
.collect::<Vec<(ParamKindOrd, GenericParamDef)>>();
param_types_present.sort_by_key(|(ord, _)| *ord);
let (mut param_types_present, ordered_params): (
Vec<ParamKindOrd>,
Vec<GenericParamDef>,
) = param_types_present.into_iter().unzip();
param_types_present.dedup();

Self::generic_arg_mismatch_err(
tcx.sess,
arg,
kind.descr(),
Some(&format!(
"reorder the arguments: {}: `<{}>`",
param_types_present
.into_iter()
.map(|ord| format!("{}s", ord.to_string()))
.collect::<Vec<String>>()
.join(", then "),
ordered_params
.into_iter()
.filter_map(|param| {
if param.name == kw::SelfUpper {
None
} else {
Some(param.name.to_string())
}
})
.collect::<Vec<String>>()
.join(", ")
)),
);
}

// We've reported the error, but we want to make sure that this
Expand Down Expand Up @@ -680,7 +741,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
assert_eq!(kind, "lifetime");
let provided =
force_infer_lt.expect("lifetimes ought to have been inferred");
Self::generic_arg_mismatch_err(tcx.sess, provided, kind);
Self::generic_arg_mismatch_err(tcx.sess, provided, kind, None);
rakshith-ravi marked this conversation as resolved.
Show resolved Hide resolved
}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ LL | fn foo<const N: usize>() -> Array<N, ()> {
| ^
|
= note: type arguments must be provided before constant arguments
= help: reorder the arguments: types, then consts: `<T, N>`

error: aborting due to previous error; 1 warning emitted

Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/suggestions/suggest-move-types.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ LL | struct Cl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime<T, 'a, A=(), B=(), C=()
| ^^
|
= note: lifetime arguments must be provided before type arguments
= help: reorder the arguments: lifetimes, then types: `<'a, 'b, 'c, T, U, V>`

error[E0747]: lifetime provided when a type was expected
--> $DIR/suggest-move-types.rs:82:56
Expand All @@ -133,6 +134,7 @@ LL | struct Dl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime<T, 'a, A=(), B=(), U, '
| ^^
|
= note: lifetime arguments must be provided before type arguments
= help: reorder the arguments: lifetimes, then types: `<'a, 'b, 'c, T, U, V>`

error: aborting due to 12 previous errors

Expand Down