Skip to content

Fix an ICE when trying to resolve a struct variant #19690

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

Closed
Closed
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
34 changes: 24 additions & 10 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5769,16 +5769,30 @@ impl<'a> Resolver<'a> {
// This is a local path in the value namespace. Walk through
// scopes looking for it.

let path_name = self.path_names_to_string(path);

match self.resolve_path(expr.id, path, ValueNS, true) {
// Check if struct variant
Some((DefVariant(_, _, true), _)) => {
self.resolve_error(expr.span,
format!("`{}` is a struct variant name, but \
this expression \
uses it like a function name",
path_name).as_slice());

self.session.span_help(expr.span,
format!("Did you mean to write: \
`{} {{ /* fields */ }}`?",
path_name).as_slice());
}
Some(def) => {
// Write the result into the def map.
debug!("(resolving expr) resolved `{}`",
self.path_names_to_string(path));
path_name);

self.record_def(expr.id, def);
}
None => {
let wrong_name = self.path_names_to_string(path);
// Be helpful if the name refers to a struct
// (The pattern matching def_tys where the id is in self.structs
// matches on regular structs while excluding tuple- and enum-like
Expand All @@ -5791,12 +5805,12 @@ impl<'a> Resolver<'a> {
format!("`{}` is a structure name, but \
this expression \
uses it like a function name",
wrong_name).as_slice());
path_name).as_slice());

self.session.span_help(expr.span,
format!("Did you mean to write: \
`{} {{ /* fields */ }}`?",
wrong_name).as_slice());
path_name).as_slice());

}
_ => {
Expand All @@ -5813,7 +5827,7 @@ impl<'a> Resolver<'a> {
});

if method_scope && token::get_name(self.self_name).get()
== wrong_name {
== path_name {
self.resolve_error(
expr.span,
"`self` is not available \
Expand All @@ -5825,18 +5839,18 @@ impl<'a> Resolver<'a> {
NoSuggestion => {
// limit search to 5 to reduce the number
// of stupid suggestions
self.find_best_match_for_name(wrong_name.as_slice(), 5)
self.find_best_match_for_name(path_name.as_slice(), 5)
.map_or("".to_string(),
|x| format!("`{}`", x))
}
Field =>
format!("`self.{}`", wrong_name),
format!("`self.{}`", path_name),
Method
| TraitItem =>
format!("to call `self.{}`", wrong_name),
format!("to call `self.{}`", path_name),
TraitMethod(path_str)
| StaticMethod(path_str) =>
format!("to call `{}::{}`", path_str, wrong_name)
format!("to call `{}::{}`", path_str, path_name)
};

if msg.len() > 0 {
Expand All @@ -5846,7 +5860,7 @@ impl<'a> Resolver<'a> {
self.resolve_error(
expr.span,
format!("unresolved name `{}`{}",
wrong_name,
path_name,
msg).as_slice());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-18252.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ enum Foo {
}

fn main() {
let f = Foo::Variant(42u); //~ ERROR expected function, found `Foo`
let f = Foo::Variant(42u); //~ ERROR uses it like a function
}
17 changes: 17 additions & 0 deletions src/test/compile-fail/issue-19452.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

enum Homura {
Madoka { age: u32 }
}

fn main() {
let homura = Homura::Madoka; //~ ERROR uses it like a function
}