Skip to content

Commit

Permalink
auto merge of #19690 : barosl/rust/struct-variant-as-a-function-ice, …
Browse files Browse the repository at this point in the history
…r=alexcrichton

Unlike a tuple variant constructor which can be called as a function, a struct variant constructor is not a function, so cannot be called.

If the user tries to assign the constructor to a variable, an ICE occurs, because there is no way to use it later. So we should stop the constructor from being used like that.

A similar mechanism already exists for a normal struct, as it prohibits a struct from being resolved. This commit does the same for a struct variant.

This commit also includes some changes to the existing tests.

Fixes #19452.
  • Loading branch information
bors committed Dec 14, 2014
2 parents 10ac5b7 + cfee5b7 commit 3a9305c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
34 changes: 24 additions & 10 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5807,16 +5807,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 @@ -5829,12 +5843,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 @@ -5851,7 +5865,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 @@ -5863,18 +5877,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 @@ -5884,7 +5898,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
}

0 comments on commit 3a9305c

Please sign in to comment.