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

Rollup of 5 pull requests #71958

Merged
merged 28 commits into from
May 7, 2020
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9e43b00
Turn of rustc-dev-guide toolstate for now
mark-i-m May 1, 2020
75f066d
Handle binop on unbound type param
estebank Apr 7, 2020
1473a66
Suggest restricting type param when it doesn't satisfy projection
estebank Apr 7, 2020
3453db7
review comments: use or-pattern
estebank Apr 7, 2020
d8d02f8
On incorrect equality constraint likely to be assoc type, suggest app…
estebank Apr 8, 2020
c93c660
review comments and rebase fix
estebank Apr 17, 2020
5d64e91
review comment: use `body_id`
estebank Apr 19, 2020
b13f234
fix rebase
estebank May 4, 2020
b17b20c
Add docstring to `deny_equality_constraints`
estebank May 4, 2020
ab7360d
refactor suggest_traits_to_import
lcnr May 4, 2020
e17f36b
Replace title "Methods" with "Implementations"
GuillaumeGomez May 5, 2020
cf18482
Update tests
GuillaumeGomez May 5, 2020
4ade6eb
Add test for new implementations section title
GuillaumeGomez May 5, 2020
758519c
Index IDs already used by rustdoc template
GuillaumeGomez May 5, 2020
e0320b5
validation: port more checks to the pattern-based macro (and give it …
RalfJung May 5, 2020
aa2eaca
add test for insufficiently aligned vtable
RalfJung May 5, 2020
837c16b
comment out rustc-dev-guide in NIGHTLY_TOOLS
mark-i-m May 6, 2020
19bd72e
convert remaining try_validation to new macro
RalfJung May 6, 2020
a64d643
Update librustdoc ID tests
GuillaumeGomez May 6, 2020
441419a
properly catch invalid-drop-fn errors
RalfJung May 6, 2020
7c44226
convert throw_validation_failure macro to same syntax as try_validation
RalfJung May 6, 2020
8998c7a
try_validation: handle multi-branching, and use macro for most remain…
RalfJung May 6, 2020
0e2a712
more precise vtable errors
RalfJung May 6, 2020
ce14d6d
Rollup merge of #70908 - estebank:suggest-add, r=nikomatsakis
Dylan-DPC May 6, 2020
7fc579f
Rollup merge of #71731 - mark-i-m:guide-toolstate-off-for-now, r=kennytm
Dylan-DPC May 6, 2020
f7c3b0c
Rollup merge of #71888 - lcnr:refactor-suggest_traits_to_import, r=es…
Dylan-DPC May 6, 2020
d33180e
Rollup merge of #71918 - GuillaumeGomez:rename-methods-section, r=Dyl…
Dylan-DPC May 6, 2020
066eb08
Rollup merge of #71950 - RalfJung:try-validation-cleanup, r=oli-obk
Dylan-DPC May 6, 2020
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
Prev Previous commit
Next Next commit
On incorrect equality constraint likely to be assoc type, suggest app…
…ropriate syntax

When encountering `where <A as Foo>::Bar = B`, it is possible that `Bar`
is an associated type. If so, suggest `where A: Foo<Bar = B>`.

CC #20041.
estebank committed May 4, 2020
commit d8d02f8f1806b564603982d8cf25795db744e0ff
94 changes: 83 additions & 11 deletions src/librustc_ast_passes/ast_validation.rs
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ use rustc_session::Session;
use rustc_span::symbol::{kw, sym};
use rustc_span::Span;
use std::mem;
use std::ops::DerefMut;

const MORE_EXTERN: &str =
"for more information, visit https://doc.rust-lang.org/std/keyword.extern.html";
@@ -1113,17 +1114,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {

for predicate in &generics.where_clause.predicates {
if let WherePredicate::EqPredicate(ref predicate) = *predicate {
self.err_handler()
.struct_span_err(
predicate.span,
"equality constraints are not yet supported in `where` clauses",
)
.span_label(predicate.span, "not supported")
.note(
"see issue #20041 <https://github.com/rust-lang/rust/issues/20041> \
for more information",
)
.emit();
deny_equality_constraints(self, predicate, generics);
}
}

@@ -1300,6 +1291,87 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}
}

fn deny_equality_constraints(
this: &mut AstValidator<'_>,
predicate: &WhereEqPredicate,
generics: &Generics,
) {
let mut err = this.err_handler().struct_span_err(
predicate.span,
"equality constraints are not yet supported in `where` clauses",
);
err.span_label(predicate.span, "not supported");

// Given `<A as Foo>::Bar = RhsTy`, suggest `A: Foo<Bar = RhsTy>`.
if let TyKind::Path(Some(qself), full_path) = &predicate.lhs_ty.kind {
if let TyKind::Path(None, path) = &qself.ty.kind {
match &path.segments[..] {
[PathSegment { ident, args: None, .. }] => {
for param in &generics.params {
if param.ident == *ident {
let param = ident;
match &full_path.segments[qself.position..] {
[PathSegment { ident, .. }] => {
// Make a new `Path` from `foo::Bar` to `Foo<Bar = RhsTy>`.
let mut assoc_path = full_path.clone();
// Remove `Bar` from `Foo::Bar`.
assoc_path.segments.pop();
let len = assoc_path.segments.len() - 1;
// Build `<Bar = RhsTy>`.
let arg = AngleBracketedArg::Constraint(AssocTyConstraint {
id: rustc_ast::node_id::DUMMY_NODE_ID,
ident: *ident,
kind: AssocTyConstraintKind::Equality {
ty: predicate.rhs_ty.clone(),
},
span: ident.span,
});
// Add `<Bar = RhsTy>` to `Foo`.
match &mut assoc_path.segments[len].args {
Some(args) => match args.deref_mut() {
GenericArgs::Parenthesized(_) => continue,
GenericArgs::AngleBracketed(args) => {
args.args.push(arg);
}
},
empty_args => {
*empty_args = AngleBracketedArgs {
span: ident.span,
args: vec![arg],
}
.into();
}
}
err.span_suggestion_verbose(
predicate.span,
&format!(
"if `{}` is an associated type you're trying to set, \
use the associated type binding syntax",
ident
),
format!(
"{}: {}",
param,
pprust::path_to_string(&assoc_path)
),
Applicability::MaybeIncorrect,
);
}
_ => {}
};
}
}
}
_ => {}
}
}
}
err.note(
"see issue #20041 <https://github.com/rust-lang/rust/issues/20041> for more information",
);
err.emit();
}

pub fn check_crate(session: &Session, krate: &Crate, lints: &mut LintBuffer) -> bool {
let mut validator = AstValidator {
session,
11 changes: 11 additions & 0 deletions src/test/ui/generic-associated-types/missing-bounds.fixed
Original file line number Diff line number Diff line change
@@ -32,4 +32,15 @@ impl<B: std::ops::Add<Output = B>> Add for D<B> {
}
}

struct E<B>(B);

impl<B: Add> Add for E<B> where B: Add<Output = B>, B: std::ops::Add<Output = B> {
//~^ ERROR equality constraints are not yet supported in `where` clauses
type Output = Self;

fn add(self, rhs: Self) -> Self {
Self(self.0 + rhs.0) //~ ERROR mismatched types
}
}

fn main() {}
11 changes: 11 additions & 0 deletions src/test/ui/generic-associated-types/missing-bounds.rs
Original file line number Diff line number Diff line change
@@ -32,4 +32,15 @@ impl<B> Add for D<B> {
}
}

struct E<B>(B);

impl<B: Add> Add for E<B> where <B as Add>::Output = B {
//~^ ERROR equality constraints are not yet supported in `where` clauses
type Output = Self;

fn add(self, rhs: Self) -> Self {
Self(self.0 + rhs.0) //~ ERROR mismatched types
}
}

fn main() {}
30 changes: 29 additions & 1 deletion src/test/ui/generic-associated-types/missing-bounds.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
error: equality constraints are not yet supported in `where` clauses
--> $DIR/missing-bounds.rs:37:33
|
LL | impl<B: Add> Add for E<B> where <B as Add>::Output = B {
| ^^^^^^^^^^^^^^^^^^^^^^ not supported
|
= note: see issue #20041 <https://github.com/rust-lang/rust/issues/20041> for more information
help: if `Output` is an associated type you're trying to set, use the associated type binding syntax
|
LL | impl<B: Add> Add for E<B> where B: Add<Output = B> {
| ^^^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
--> $DIR/missing-bounds.rs:11:11
|
@@ -43,7 +55,23 @@ help: consider restricting type parameter `B`
LL | impl<B: std::ops::Add<Output = B>> Add for D<B> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors
error[E0308]: mismatched types
--> $DIR/missing-bounds.rs:42:14
|
LL | impl<B: Add> Add for E<B> where <B as Add>::Output = B {
| - this type parameter
...
LL | Self(self.0 + rhs.0)
| ^^^^^^^^^^^^^^ expected type parameter `B`, found associated type
|
= note: expected type parameter `B`
found associated type `<B as std::ops::Add>::Output`
help: consider further restricting type parameter `B`
|
LL | impl<B: Add> Add for E<B> where <B as Add>::Output = B, B: std::ops::Add<Output = B> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 5 previous errors

Some errors have detailed explanations: E0308, E0369.
For more information about an error, try `rustc --explain E0308`.