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

Correct generic parameter ordering in error note for E0747 #72848

Merged
merged 3 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 17 additions & 1 deletion src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +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_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::ErrorReported;
Expand Down Expand Up @@ -483,8 +484,23 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
arg.descr(),
kind,
);

let kind_ord = match kind {
"lifetime" => ParamKindOrd::Lifetime,
"type" => ParamKindOrd::Type,
"constant" => ParamKindOrd::Const,
_ => panic!(),
Copy link
Member Author

@camelid camelid Jun 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the right way to trigger an ICE?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that bug! is the preferred way to trigger an ICE.

In this case, I would recommend changing opt_span_bug_fmt to take the original GenericParamDefKind, rather than its string description. That will allow you to write an exhaustive match, removing the need for an explicit panic!/bug!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tip! I'm new to contributing to rustc, so could you describe the change you're recommending in more detail?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On closer examination, it turns out that the two callers of this function actually have two different "original" types: GenericParamDefKind and GenericArg.

I believe your change is actually perfectly correct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went through the same thought process @Aaron1011. It would be a little cleaner to pass ParamKindOrd directly, but I'm not sure it's worth making more changes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth opening an issue?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be easy to change in this pull request if desired, but I'm not sure it's worth doing: it adds quite a bit of boilerplate code, and it's only used in this one case. If we ever find ourselves doing something similar again in the future, we should revisit it.

camelid marked this conversation as resolved.
Show resolved Hide resolved
};
let arg_ord = match arg {
GenericArg::Lifetime(_) => ParamKindOrd::Lifetime,
GenericArg::Type(_) => ParamKindOrd::Type,
GenericArg::Const(_) => ParamKindOrd::Const,
};

// This note will be true as long as generic parameters are strictly ordered by their kind.
err.note(&format!("{} arguments must be provided before {} arguments", kind, arg.descr()));
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));
err.emit();
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/suggestions/suggest-move-types.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ error[E0747]: lifetime provided when a type was expected
LL | struct Cl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime<T, 'a, A=(), B=(), C=(), U, 'b, V, 'c>> {
| ^^
|
= note: type arguments must be provided before lifetime arguments
= note: lifetime arguments must be provided before type arguments

error[E0747]: lifetime provided when a type was expected
--> $DIR/suggest-move-types.rs:82:56
|
LL | struct Dl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime<T, 'a, A=(), B=(), U, 'b, C=(), V, 'c>> {
| ^^
|
= note: type arguments must be provided before lifetime arguments
= note: lifetime arguments must be provided before type arguments

error: aborting due to 12 previous errors

Expand Down