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

Make new type param suggestion more targetted #73320

Merged
merged 3 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
module_path.push(Segment {
ident: Ident { name: kw::PathRoot, span: source.ident.span },
id: Some(self.r.next_node_id()),
has_args: false,
});
source.ident.name = crate_name;
}
Expand Down
41 changes: 33 additions & 8 deletions src/librustc_resolve/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,20 +919,45 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
&self,
path: &[Segment],
) -> Option<(Span, &'static str, String, Applicability)> {
let ident = match path {
[segment] => segment.ident,
let (ident, span) = match path {
[segment] if !segment.has_args => (segment.ident.to_string(), segment.ident.span),
_ => return None,
};
match (
self.diagnostic_metadata.current_item,
self.diagnostic_metadata.currently_processing_generics,
) {
(Some(Item { kind: ItemKind::Fn(..), ident, .. }), true) if ident.name == sym::main => {
let mut iter = ident.chars().map(|c| c.is_uppercase());
let single_uppercase_char =
matches!(iter.next(), Some(true)) && matches!(iter.next(), None);
if !self.diagnostic_metadata.currently_processing_generics && !single_uppercase_char {
return None;
}
match (self.diagnostic_metadata.current_item, single_uppercase_char) {
(Some(Item { kind: ItemKind::Fn(..), ident, .. }), _) if ident.name == sym::main => {
// Ignore `fn main()` as we don't want to suggest `fn main<T>()`
}
(Some(Item { kind, .. }), true) => {
(
Some(Item {
kind:
kind @ ItemKind::Fn(..)
| kind @ ItemKind::Enum(..)
| kind @ ItemKind::Struct(..)
| kind @ ItemKind::Union(..),
..
}),
true,
)
| (Some(Item { kind, .. }), false) => {
// Likely missing type parameter.
if let Some(generics) = kind.generics() {
if span.overlaps(generics.span) {
// Avoid the following:
// error[E0405]: cannot find trait `A` in this scope
// --> $DIR/typo-suggestion-named-underscore.rs:CC:LL
// |
// L | fn foo<T: A>(x: T) {} // Shouldn't suggest underscore
// | ^- help: you might be missing a type parameter: `, A`
// | |
// | not found in this scope
return None;
}
let msg = "you might be missing a type parameter";
let (span, sugg) = if let [.., param] = &generics.params[..] {
let span = if let [.., bound] = &param.bounds[..] {
Expand Down
14 changes: 8 additions & 6 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,15 @@ enum VisResolutionError<'a> {
ModuleOnly(Span),
}

// A minimal representation of a path segment. We use this in resolve because
// we synthesize 'path segments' which don't have the rest of an AST or HIR
// `PathSegment`.
/// A minimal representation of a path segment. We use this in resolve because we synthesize 'path
/// segments' which don't have the rest of an AST or HIR `PathSegment`.
#[derive(Clone, Copy, Debug)]
pub struct Segment {
ident: Ident,
id: Option<NodeId>,
/// Signals whether this `PathSegment` has generic arguments. Used to avoid providing
/// nonsensical suggestions.
has_args: bool,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
has_args: bool,
has_generic_args: bool,

Might make this a little bit more clear.

}

impl Segment {
Expand All @@ -240,7 +242,7 @@ impl Segment {
}

fn from_ident(ident: Ident) -> Segment {
Segment { ident, id: None }
Segment { ident, id: None, has_args: false }
}

fn names_to_string(segments: &[Segment]) -> String {
Expand All @@ -250,7 +252,7 @@ impl Segment {

impl<'a> From<&'a ast::PathSegment> for Segment {
fn from(seg: &'a ast::PathSegment) -> Segment {
Segment { ident: seg.ident, id: Some(seg.id) }
Segment { ident: seg.ident, id: Some(seg.id), has_args: seg.args.is_some() }
}
}

Expand Down Expand Up @@ -2017,7 +2019,7 @@ impl<'a> Resolver<'a> {
path, opt_ns, record_used, path_span, crate_lint,
);

for (i, &Segment { ident, id }) in path.iter().enumerate() {
for (i, &Segment { ident, id, has_args: _ }) in path.iter().enumerate() {
debug!("resolve_path ident {} {:?} {:?}", i, ident, id);
let record_segment_res = |this: &mut Self, res| {
if record_used {
Expand Down
11 changes: 10 additions & 1 deletion src/test/ui/associated-types/associated-types-eq-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ error[E0412]: cannot find type `A` in this scope
LL | fn foo2<I: Foo>(x: I) {
| - similarly named type parameter `I` defined here
LL | let _: A = x.boo();
| ^ help: a type parameter with a similar name exists: `I`
| ^
|
help: a type parameter with a similar name exists
|
LL | let _: I = x.boo();
| ^
help: you might be missing a type parameter
|
LL | fn foo2<I: Foo, A>(x: I) {
| ^^^
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Opened #73321 for handling this more gracefully.


error: aborting due to previous error

Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/suggestions/type-not-found-in-adt-field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
struct Struct {
m: Vec<Someunknownname<String, ()>>, //~ ERROR cannot find type `Someunknownname` in this scope
//~^ NOTE not found in this scope
}
struct OtherStruct { //~ HELP you might be missing a type parameter
m: K, //~ ERROR cannot find type `K` in this scope
//~^ NOTE not found in this scope
}
fn main() {}
17 changes: 17 additions & 0 deletions src/test/ui/suggestions/type-not-found-in-adt-field.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0412]: cannot find type `Someunknownname` in this scope
--> $DIR/type-not-found-in-adt-field.rs:2:12
|
LL | m: Vec<Someunknownname<String, ()>>,
| ^^^^^^^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `K` in this scope
--> $DIR/type-not-found-in-adt-field.rs:6:8
|
LL | struct OtherStruct {
| - help: you might be missing a type parameter: `<K>`
LL | m: K,
| ^ not found in this scope

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0412`.