Skip to content

Commit

Permalink
Suggestion moving types before associated types.
Browse files Browse the repository at this point in the history
This commit extends existing suggestions to move lifetimes before types
in generic arguments to also suggest moving types behind associated type
bindings.
  • Loading branch information
davidtwco committed Jan 25, 2019
1 parent 095b44c commit 463e623
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 15 deletions.
67 changes: 52 additions & 15 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5530,22 +5530,31 @@ impl<'a> Parser<'a> {
fn parse_generic_args(&mut self) -> PResult<'a, (Vec<GenericArg>, Vec<TypeBinding>)> {
let mut args = Vec::new();
let mut bindings = Vec::new();

let mut seen_type = false;
let mut seen_binding = false;

let mut last_comma_span = None;
let mut first_type_or_binding_span: Option<Span> = None;
let mut first_binding_span: Option<Span> = None;

let mut bad_lifetime_pos = vec![];
let mut last_comma_span = None;
let mut suggestions = vec![];
let mut bad_type_pos = vec![];

let mut lifetime_suggestions = vec![];
let mut type_suggestions = vec![];
loop {
if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) {
// Parse lifetime argument.
args.push(GenericArg::Lifetime(self.expect_lifetime()));

if seen_type || seen_binding {
let remove_sp = last_comma_span.unwrap_or(self.prev_span).to(self.prev_span);
bad_lifetime_pos.push(self.prev_span);

if let Ok(snippet) = self.sess.source_map().span_to_snippet(self.prev_span) {
suggestions.push((remove_sp, String::new()));
suggestions.push((
lifetime_suggestions.push((remove_sp, String::new()));
lifetime_suggestions.push((
first_type_or_binding_span.unwrap().shrink_to_lo(),
format!("{}, ", snippet)));
}
Expand All @@ -5563,24 +5572,29 @@ impl<'a> Parser<'a> {
ty,
span,
});

seen_binding = true;
if first_type_or_binding_span.is_none() {
first_type_or_binding_span = Some(span);
}
if first_binding_span.is_none() {
first_binding_span = Some(span);
}
} else if self.check_type() {
// Parse type argument.
let ty_param = self.parse_ty()?;
if seen_binding {
self.struct_span_err(
ty_param.span,
"type parameters must be declared prior to associated type bindings"
)
.span_label(
ty_param.span,
"must be declared prior to associated type bindings",
)
.emit();
let remove_sp = last_comma_span.unwrap_or(self.prev_span).to(self.prev_span);
bad_type_pos.push(self.prev_span);

if let Ok(snippet) = self.sess.source_map().span_to_snippet(self.prev_span) {
type_suggestions.push((remove_sp, String::new()));
type_suggestions.push((
first_binding_span.unwrap().shrink_to_lo(),
format!("{}, ", snippet)));
}
}

if first_type_or_binding_span.is_none() {
first_type_or_binding_span = Some(ty_param.span);
}
Expand All @@ -5596,6 +5610,7 @@ impl<'a> Parser<'a> {
last_comma_span = Some(self.prev_span);
}
}

if !bad_lifetime_pos.is_empty() {
let mut err = self.struct_span_err(
bad_lifetime_pos.clone(),
Expand All @@ -5604,18 +5619,40 @@ impl<'a> Parser<'a> {
for sp in &bad_lifetime_pos {
err.span_label(*sp, "must be declared prior to type parameters");
}
if !suggestions.is_empty() {
if !lifetime_suggestions.is_empty() {
err.multipart_suggestion_with_applicability(
&format!(
"move the lifetime parameter{} prior to the first type parameter",
if bad_lifetime_pos.len() > 1 { "s" } else { "" },
),
suggestions,
lifetime_suggestions,
Applicability::MachineApplicable,
);
}
err.emit();
}

if !bad_type_pos.is_empty() {
let mut err = self.struct_span_err(
bad_type_pos.clone(),
"type parameters must be declared prior to associated type bindings"
);
for sp in &bad_type_pos {
err.span_label(*sp, "must be declared prior to associated type bindings");
}
if !type_suggestions.is_empty() {
err.multipart_suggestion_with_applicability(
&format!(
"move the type parameter{} prior to the first associated type binding",
if bad_type_pos.len() > 1 { "s" } else { "" },
),
type_suggestions,
Applicability::MachineApplicable,
);
}
err.emit();
}

Ok((args, bindings))
}

Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/parser/issue-32214.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ error: type parameters must be declared prior to associated type bindings
|
LL | pub fn test<W, I: Trait<Item=(), W> >() {}
| ^ must be declared prior to associated type bindings
help: move the type parameter prior to the first associated type binding
|
LL | pub fn test<W, I: Trait<W, Item=()> >() {}
| ^^ --

error: aborting due to previous error

42 changes: 42 additions & 0 deletions src/test/ui/suggestions/suggest-move-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#![allow(warnings)]

// This test verifies that the suggestion to move types before associated type bindings
// is correct.

trait One<T> {
type A;
}

trait Three<T, U, V> {
type A;
type B;
type C;
}

struct A<T, M: One<A=(), T>> { //~ ERROR type parameters must be declared
m: M,
t: T,
}

struct B<T, U, V, M: Three<A=(), B=(), C=(), T, U, V>> { //~ ERROR type parameters must be declared
m: M,
t: T,
u: U,
v: V,
}

struct C<T, U, V, M: Three<T, A=(), B=(), C=(), U, V>> { //~ ERROR type parameters must be declared
m: M,
t: T,
u: U,
v: V,
}

struct D<T, U, V, M: Three<T, A=(), B=(), U, C=(), V>> { //~ ERROR type parameters must be declared
m: M,
t: T,
u: U,
v: V,
}

fn main() {}
49 changes: 49 additions & 0 deletions src/test/ui/suggestions/suggest-move-types.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
error: type parameters must be declared prior to associated type bindings
--> $DIR/suggest-move-types.rs:16:26
|
LL | struct A<T, M: One<A=(), T>> { //~ ERROR type parameters must be declared
| ^ must be declared prior to associated type bindings
help: move the type parameter prior to the first associated type binding
|
LL | struct A<T, M: One<T, A=()>> { //~ ERROR type parameters must be declared
| ^^ --

error: type parameters must be declared prior to associated type bindings
--> $DIR/suggest-move-types.rs:21:46
|
LL | struct B<T, U, V, M: Three<A=(), B=(), C=(), T, U, V>> { //~ ERROR type parameters must be declared
| ^ ^ ^ must be declared prior to associated type bindings
| | |
| | must be declared prior to associated type bindings
| must be declared prior to associated type bindings
help: move the type parameters prior to the first associated type binding
|
LL | struct B<T, U, V, M: Three<T, U, V, A=(), B=(), C=()>> { //~ ERROR type parameters must be declared
| ^^ ^^ ^^ --

error: type parameters must be declared prior to associated type bindings
--> $DIR/suggest-move-types.rs:28:49
|
LL | struct C<T, U, V, M: Three<T, A=(), B=(), C=(), U, V>> { //~ ERROR type parameters must be declared
| ^ ^ must be declared prior to associated type bindings
| |
| must be declared prior to associated type bindings
help: move the type parameters prior to the first associated type binding
|
LL | struct C<T, U, V, M: Three<T, U, V, A=(), B=(), C=()>> { //~ ERROR type parameters must be declared
| ^^ ^^ --

error: type parameters must be declared prior to associated type bindings
--> $DIR/suggest-move-types.rs:35:43
|
LL | struct D<T, U, V, M: Three<T, A=(), B=(), U, C=(), V>> { //~ ERROR type parameters must be declared
| ^ ^ must be declared prior to associated type bindings
| |
| must be declared prior to associated type bindings
help: move the type parameters prior to the first associated type binding
|
LL | struct D<T, U, V, M: Three<T, U, V, A=(), B=(), C=()>> { //~ ERROR type parameters must be declared
| ^^ ^^ -- --

error: aborting due to 4 previous errors

0 comments on commit 463e623

Please sign in to comment.