Further tweaks to the output

- Properly address Variant Ctors
- Show signature if span of trait method without `self` is not available
This commit is contained in:
Esteban Küber 2018-01-15 05:18:06 -08:00
parent 445f339ba9
commit 4a7691692c
14 changed files with 433 additions and 102 deletions

View file

@ -2726,23 +2726,27 @@ impl<'a> Resolver<'a> {
}
return (err, candidates);
}
(Def::VariantCtor(_, CtorKind::Fictive), _) if ns == ValueNS => {
let block = match ctor_kind {
CtorKind::Fn => "(/* fields */)",
CtorKind::Const => "",
CtorKind::Fictive => " { /* fields */ }",
def => bug!("found def `{:?}` when looking for a ctor",
def),
};
err.span_label(span, format!("did you mean `{}{}`?",
path_str,
block));
(Def::VariantCtor(_, CtorKind::Const), _) => {
err.span_label(span, format!("did you mean `{}`?", path_str));
return (err, candidates);
}
(Def::SelfTy(..), _) if ns == ValueNS {
(Def::VariantCtor(_, CtorKind::Fn), _) => {
err.span_label(span, format!("did you mean `{}( /* fields */ )`?",
path_str));
return (err, candidates);
}
(Def::VariantCtor(_, CtorKind::Fictive), _) => {
err.span_label(span, format!("did you mean `{} {{ /* fields */ }}`?",
path_str));
return (err, candidates);
}
(Def::SelfTy(..), _) if ns == ValueNS => {
err.note("can't use `Self` as a constructor, you must use the \
implemented struct");
}
(Def::TyAlias(_def_id), _) => {
err.note("can't use a type alias as a constructor");
}
_ => {}
}
}
@ -3994,13 +3998,6 @@ impl<'a> Resolver<'a> {
}
}
fn is_struct_like(def: Def) -> bool {
match def {
Def::VariantCtor(_, CtorKind::Fictive) => true,
_ => PathSource::Struct.is_expected(def),
}
}
fn is_self_type(path: &[SpannedIdent], namespace: Namespace) -> bool {
namespace == TypeNS && path.len() == 1 && path[0].node.name == keywords::SelfType.name()
}

View file

@ -210,15 +210,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
}
}
let mut err = type_error_struct!(self.tcx.sess, call_expr.span, callee_ty, E0618,
"expected function, found `{}`",
if let Some(ref path) = unit_variant {
path.to_string()
} else {
callee_ty.to_string()
});
if let Some(path) = unit_variant {
err.help(&format!("did you mean to write `{}`?", path));
let mut err = type_error_struct!(
self.tcx.sess,
call_expr.span,
callee_ty,
E0618,
"expected function, found {}",
match unit_variant {
Some(ref path) => format!("enum variant `{}`", path),
None => format!("`{}`", callee_ty),
});
err.span_label(call_expr.span, "not a function");
if let Some(ref path) = unit_variant {
err.span_suggestion(call_expr.span,
&format!("`{}` is a unit variant, you need to write it \
without the parenthesis", path),
path.to_string());
}
if let hir::ExprCall(ref expr, _) = call_expr.node {
@ -235,7 +245,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
_ => self.tcx.hir.span_if_local(def.def_id())
};
if let Some(span) = def_span {
err.span_note(span, "defined here");
let name = match unit_variant {
Some(path) => path,
None => callee_ty.to_string(),
};
err.span_label(span, format!("`{}` defined here", name));
}
}

View file

@ -518,7 +518,10 @@ fn compare_self_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
self_descr);
err.span_label(impl_m_span, format!("`{}` used in impl", self_descr));
if let Some(span) = tcx.hir.span_if_local(trait_m.def_id) {
err.span_label(span, format!("trait declared without `{}`", self_descr));
err.span_label(span, format!("trait method declared without `{}`", self_descr));
} else {
err.note_trait_signature(trait_m.name.to_string(),
trait_m.signature(&tcx));
}
err.emit();
return Err(ErrorReported);
@ -533,8 +536,7 @@ fn compare_self_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
not in the impl",
trait_m.name,
self_descr);
err.span_label(impl_m_span,
format!("expected `{}` in impl", self_descr));
err.span_label(impl_m_span, format!("expected `{}` in impl", self_descr));
if let Some(span) = tcx.hir.span_if_local(trait_m.def_id) {
err.span_label(span, format!("`{}` used in trait", self_descr));
} else {

View file

@ -9,14 +9,16 @@
// except according to those terms.
trait Foo {
fn foo(); //~ trait declared without `&self`
fn foo();
//~^ NOTE trait method declared without `&self`
}
struct Bar;
impl Foo for Bar {
fn foo(&self) {} //~ ERROR E0185
//~^ `&self` used in impl
fn foo(&self) {}
//~^ ERROR E0185
//~| NOTE `&self` used in impl
}
fn main() {

View file

@ -13,7 +13,9 @@ enum X {
}
fn main() {
X::Entry(); //~ ERROR expected function, found `X::Entry` [E0618]
X::Entry();
//~^ ERROR expected function, found enum variant `X::Entry` [E0618]
let x = 0i32;
x(); //~ ERROR expected function, found `i32` [E0618]
x();
//~^ ERROR expected function, found `i32` [E0618]
}

View file

@ -13,7 +13,7 @@ error[E0618]: expected function, found `()`
--> $DIR/issue-20862.rs:17:13
|
17 | let x = foo(5)(2);
| ^^^^^^^^^
| ^^^^^^^^^ not a function
error: aborting due to 2 previous errors

View file

@ -24,8 +24,8 @@ enum E {
fn main() {
let e2 = Empty2(); //~ ERROR expected function, found `Empty2`
let e4 = E::Empty4();
//~^ ERROR expected function, found `E::Empty4` [E0618]
//~^ ERROR expected function, found enum variant `E::Empty4` [E0618]
let xe2 = XEmpty2(); //~ ERROR expected function, found `empty_struct::XEmpty2`
let xe4 = XE::XEmpty4();
//~^ ERROR expected function, found `XE::XEmpty4` [E0618]
//~^ ERROR expected function, found enum variant `XE::XEmpty4` [E0618]
}

View file

@ -1,41 +1,40 @@
error[E0618]: expected function, found `Empty2`
--> $DIR/empty-struct-unit-expr.rs:25:14
|
25 | let e2 = Empty2(); //~ ERROR expected function, found `Empty2`
| ^^^^^^^^
|
note: defined here
--> $DIR/empty-struct-unit-expr.rs:18:1
|
18 | struct Empty2;
| ^^^^^^^^^^^^^^
| -------------- `Empty2` defined here
...
25 | let e2 = Empty2(); //~ ERROR expected function, found `Empty2`
| ^^^^^^^^ not a function
error[E0618]: expected function, found `E::Empty4`
error[E0618]: expected function, found enum variant `E::Empty4`
--> $DIR/empty-struct-unit-expr.rs:26:14
|
26 | let e4 = E::Empty4();
| ^^^^^^^^^^^
|
= help: did you mean to write `E::Empty4`?
note: defined here
--> $DIR/empty-struct-unit-expr.rs:21:5
|
21 | Empty4
| ^^^^^^
| ------ `E::Empty4` defined here
...
26 | let e4 = E::Empty4();
| ^^^^^^^^^^^ not a function
help: `E::Empty4` is a unit variant, you need to write it without the parenthesis
|
26 | let e4 = E::Empty4;
| ^^^^^^^^^
error[E0618]: expected function, found `empty_struct::XEmpty2`
--> $DIR/empty-struct-unit-expr.rs:28:15
|
28 | let xe2 = XEmpty2(); //~ ERROR expected function, found `empty_struct::XEmpty2`
| ^^^^^^^^^
| ^^^^^^^^^ not a function
error[E0618]: expected function, found `XE::XEmpty4`
error[E0618]: expected function, found enum variant `XE::XEmpty4`
--> $DIR/empty-struct-unit-expr.rs:29:15
|
29 | let xe4 = XE::XEmpty4();
| ^^^^^^^^^^^^^
| ^^^^^^^^^^^^^ not a function
help: `XE::XEmpty4` is a unit variant, you need to write it without the parenthesis
|
= help: did you mean to write `XE::XEmpty4`?
29 | let xe4 = XE::XEmpty4;
| ^^^^^^^^^^^
error: aborting due to 4 previous errors

View file

@ -1,26 +1,18 @@
error[E0618]: expected function, found `i32`
--> $DIR/issue-10969.rs:12:5
|
12 | i(); //~ERROR expected function, found `i32`
| ^^^
|
note: defined here
--> $DIR/issue-10969.rs:11:9
|
11 | fn func(i: i32) {
| ^
| - `i32` defined here
12 | i(); //~ERROR expected function, found `i32`
| ^^^ not a function
error[E0618]: expected function, found `i32`
--> $DIR/issue-10969.rs:16:5
|
16 | i(); //~ERROR expected function, found `i32`
| ^^^
|
note: defined here
--> $DIR/issue-10969.rs:15:9
|
15 | let i = 0i32;
| ^
| - `i32` defined here
16 | i(); //~ERROR expected function, found `i32`
| ^^^ not a function
error: aborting due to 2 previous errors

View file

@ -0,0 +1,81 @@
// Copyright 2016 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.
mod m {
pub enum E {
Fn(u8),
Struct {
s: u8,
},
Unit,
}
pub mod n {
pub(in m) enum Z {
Fn(u8),
Struct {
s: u8,
},
Unit,
}
}
use m::n::Z; // OK, only the type is imported
fn f() {
n::Z;
//~^ ERROR expected value, found enum `n::Z`
Z;
//~^ ERROR expected value, found enum `Z`
let _: Z = Z::Fn;
//~^ ERROR mismatched types
let _: Z = Z::Struct;
//~^ ERROR expected value, found struct variant `Z::Struct`
let _ = Z::Unit();
//~^ ERROR expected function, found enum variant `Z::Unit`
let _ = Z::Unit {};
// This is ok, it is equivalent to not having braces
}
}
use m::E; // OK, only the type is imported
fn main() {
let _: E = m::E;
//~^ ERROR expected value, found enum `m::E`
let _: E = m::E::Fn;
//~^ ERROR mismatched types
let _: E = m::E::Struct;
//~^ ERROR expected value, found struct variant `m::E::Struct`
let _: E = m::E::Unit();
//~^ ERROR expected function, found enum variant `m::E::Unit`
let _: E = E;
//~^ ERROR expected value, found enum `E`
let _: E = E::Fn;
//~^ ERROR mismatched types
let _: E = E::Struct;
//~^ ERROR expected value, found struct variant `E::Struct`
let _: E = E::Unit();
//~^ ERROR expected function, found enum variant `E::Unit`
let _: Z = m::n::Z;
//~^ ERROR cannot find type `Z` in this scope
//~| ERROR expected value, found enum `m::n::Z`
//~| ERROR enum `Z` is private
let _: Z = m::n::Z::Fn;
//~^ ERROR cannot find type `Z` in this scope
//~| ERROR enum `Z` is private
let _: Z = m::n::Z::Struct;
//~^ ERROR cannot find type `Z` in this scope
//~| ERROR expected value, found struct variant `m::n::Z::Struct`
//~| ERROR enum `Z` is private
let _: Z = m::n::Z::Unit {};
//~^ ERROR cannot find type `Z` in this scope
//~| ERROR enum `Z` is private
}

View file

@ -0,0 +1,225 @@
error[E0423]: expected value, found enum `n::Z`
--> $DIR/privacy-enum-ctor.rs:33:9
|
33 | n::Z;
| ^^^^
|
= note: did you mean to use one of the following variants?
- `m::Z::Fn`
- `m::Z::Struct`
- `m::Z::Unit`
error[E0423]: expected value, found enum `Z`
--> $DIR/privacy-enum-ctor.rs:35:9
|
35 | Z;
| ^ did you mean `f`?
|
= note: did you mean to use one of the following variants?
- `m::Z::Fn`
- `m::Z::Struct`
- `m::Z::Unit`
error[E0423]: expected value, found struct variant `Z::Struct`
--> $DIR/privacy-enum-ctor.rs:39:20
|
39 | let _: Z = Z::Struct;
| ^^^^^^^^^ did you mean `Z::Struct { /* fields */ }`?
error[E0423]: expected value, found enum `m::E`
--> $DIR/privacy-enum-ctor.rs:51:16
|
51 | let _: E = m::E;
| ^^^-
| |
| did you mean `f`?
|
= note: did you mean to use one of the following variants?
- `E::Fn`
- `E::Struct`
- `E::Unit`
help: possible better candidates are found in other modules, you can import them into scope
|
48 | use std::f32::consts::E;
|
48 | use std::f64::consts::E;
|
error[E0423]: expected value, found struct variant `m::E::Struct`
--> $DIR/privacy-enum-ctor.rs:55:16
|
55 | let _: E = m::E::Struct;
| ^^^^^^^^^^^^ did you mean `m::E::Struct { /* fields */ }`?
error[E0423]: expected value, found enum `E`
--> $DIR/privacy-enum-ctor.rs:59:16
|
59 | let _: E = E;
| ^
|
= note: did you mean to use one of the following variants?
- `E::Fn`
- `E::Struct`
- `E::Unit`
help: possible better candidates are found in other modules, you can import them into scope
|
48 | use std::f32::consts::E;
|
48 | use std::f64::consts::E;
|
error[E0423]: expected value, found struct variant `E::Struct`
--> $DIR/privacy-enum-ctor.rs:63:16
|
63 | let _: E = E::Struct;
| ^^^^^^^^^ did you mean `E::Struct { /* fields */ }`?
error[E0412]: cannot find type `Z` in this scope
--> $DIR/privacy-enum-ctor.rs:67:12
|
67 | let _: Z = m::n::Z;
| ^ did you mean `E`?
help: possible candidate is found in another module, you can import it into scope
|
48 | use m::n::Z;
|
error[E0423]: expected value, found enum `m::n::Z`
--> $DIR/privacy-enum-ctor.rs:67:16
|
67 | let _: Z = m::n::Z;
| ^^^^^^^
|
= note: did you mean to use one of the following variants?
- `m::Z::Fn`
- `m::Z::Struct`
- `m::Z::Unit`
error[E0412]: cannot find type `Z` in this scope
--> $DIR/privacy-enum-ctor.rs:71:12
|
71 | let _: Z = m::n::Z::Fn;
| ^ did you mean `E`?
help: possible candidate is found in another module, you can import it into scope
|
48 | use m::n::Z;
|
error[E0412]: cannot find type `Z` in this scope
--> $DIR/privacy-enum-ctor.rs:74:12
|
74 | let _: Z = m::n::Z::Struct;
| ^ did you mean `E`?
help: possible candidate is found in another module, you can import it into scope
|
48 | use m::n::Z;
|
error[E0423]: expected value, found struct variant `m::n::Z::Struct`
--> $DIR/privacy-enum-ctor.rs:74:16
|
74 | let _: Z = m::n::Z::Struct;
| ^^^^^^^^^^^^^^^ did you mean `m::n::Z::Struct { /* fields */ }`?
error[E0412]: cannot find type `Z` in this scope
--> $DIR/privacy-enum-ctor.rs:78:12
|
78 | let _: Z = m::n::Z::Unit {};
| ^ did you mean `E`?
help: possible candidate is found in another module, you can import it into scope
|
48 | use m::n::Z;
|
error[E0603]: enum `Z` is private
--> $DIR/privacy-enum-ctor.rs:67:16
|
67 | let _: Z = m::n::Z;
| ^^^^^^^
error[E0603]: enum `Z` is private
--> $DIR/privacy-enum-ctor.rs:71:16
|
71 | let _: Z = m::n::Z::Fn;
| ^^^^^^^^^^^
error[E0603]: enum `Z` is private
--> $DIR/privacy-enum-ctor.rs:74:16
|
74 | let _: Z = m::n::Z::Struct;
| ^^^^^^^^^^^^^^^
error[E0603]: enum `Z` is private
--> $DIR/privacy-enum-ctor.rs:78:16
|
78 | let _: Z = m::n::Z::Unit {};
| ^^^^^^^^^^^^^
error[E0308]: mismatched types
--> $DIR/privacy-enum-ctor.rs:37:20
|
37 | let _: Z = Z::Fn;
| ^^^^^ expected enum `m::n::Z`, found fn item
|
= note: expected type `m::n::Z`
found type `fn(u8) -> m::n::Z {m::n::Z::Fn}`
error[E0618]: expected function, found enum variant `Z::Unit`
--> $DIR/privacy-enum-ctor.rs:41:17
|
26 | Unit,
| ---- `Z::Unit` defined here
...
41 | let _ = Z::Unit();
| ^^^^^^^^^ not a function
help: `Z::Unit` is a unit variant, you need to write it without the parenthesis
|
41 | let _ = Z::Unit;
| ^^^^^^^
error[E0308]: mismatched types
--> $DIR/privacy-enum-ctor.rs:53:16
|
53 | let _: E = m::E::Fn;
| ^^^^^^^^ expected enum `m::E`, found fn item
|
= note: expected type `m::E`
found type `fn(u8) -> m::E {m::E::Fn}`
error[E0618]: expected function, found enum variant `m::E::Unit`
--> $DIR/privacy-enum-ctor.rs:57:16
|
17 | Unit,
| ---- `m::E::Unit` defined here
...
57 | let _: E = m::E::Unit();
| ^^^^^^^^^^^^ not a function
help: `m::E::Unit` is a unit variant, you need to write it without the parenthesis
|
57 | let _: E = m::E::Unit;
| ^^^^^^^^^^
error[E0308]: mismatched types
--> $DIR/privacy-enum-ctor.rs:61:16
|
61 | let _: E = E::Fn;
| ^^^^^ expected enum `m::E`, found fn item
|
= note: expected type `m::E`
found type `fn(u8) -> m::E {m::E::Fn}`
error[E0618]: expected function, found enum variant `E::Unit`
--> $DIR/privacy-enum-ctor.rs:65:16
|
17 | Unit,
| ---- `E::Unit` defined here
...
65 | let _: E = E::Unit();
| ^^^^^^^^^ not a function
help: `E::Unit` is a unit variant, you need to write it without the parenthesis
|
65 | let _: E = E::Unit;
| ^^^^^^^
error: aborting due to 23 previous errors

View file

@ -25,7 +25,8 @@ mod m {
use m::n::Z; // OK, only the type is imported
fn f() {
n::Z; //~ ERROR tuple struct `Z` is private
n::Z;
//~^ ERROR tuple struct `Z` is private
Z;
//~^ ERROR expected value, found struct `Z`
}
@ -35,16 +36,22 @@ use m::S; // OK, only the type is imported
use m::S2; // OK, only the type is imported
fn main() {
m::S; //~ ERROR tuple struct `S` is private
m::S;
//~^ ERROR tuple struct `S` is private
let _: S = m::S(2);
//~^ ERROR tuple struct `S` is private
S;
//~^ ERROR expected value, found struct `S`
m::n::Z; //~ ERROR tuple struct `Z` is private
m::n::Z;
//~^ ERROR tuple struct `Z` is private
S2;
//~^ ERROR expected value, found struct `S2`
xcrate::m::S; //~ ERROR tuple struct `S` is private
xcrate::m::S;
//~^ ERROR tuple struct `S` is private
xcrate::S;
//~^ ERROR expected value, found struct `xcrate::S`
xcrate::m::n::Z; //~ ERROR tuple struct `Z` is private
xcrate::m::n::Z;
//~^ ERROR tuple struct `Z` is private
}

View file

@ -1,7 +1,7 @@
error[E0423]: expected value, found struct `Z`
--> $DIR/privacy-struct-ctor.rs:29:9
--> $DIR/privacy-struct-ctor.rs:30:9
|
29 | Z;
30 | Z;
| ^
| |
| did you mean `S`?
@ -12,60 +12,66 @@ help: possible better candidate is found in another module, you can import it in
|
error[E0423]: expected value, found struct `S`
--> $DIR/privacy-struct-ctor.rs:39:5
--> $DIR/privacy-struct-ctor.rs:43:5
|
39 | S;
43 | S;
| ^ constructor is not visible here due to private fields
help: possible better candidate is found in another module, you can import it into scope
|
34 | use m::S;
35 | use m::S;
|
error[E0423]: expected value, found struct `S2`
--> $DIR/privacy-struct-ctor.rs:43:5
--> $DIR/privacy-struct-ctor.rs:48:5
|
43 | S2;
48 | S2;
| ^^ did you mean `S2 { /* fields */ }`?
error[E0423]: expected value, found struct `xcrate::S`
--> $DIR/privacy-struct-ctor.rs:47:5
--> $DIR/privacy-struct-ctor.rs:53:5
|
47 | xcrate::S;
53 | xcrate::S;
| ^^^^^^^^^ constructor is not visible here due to private fields
help: possible better candidate is found in another module, you can import it into scope
|
34 | use m::S;
35 | use m::S;
|
error[E0603]: tuple struct `Z` is private
--> $DIR/privacy-struct-ctor.rs:28:9
|
28 | n::Z; //~ ERROR tuple struct `Z` is private
28 | n::Z;
| ^^^^
error[E0603]: tuple struct `S` is private
--> $DIR/privacy-struct-ctor.rs:38:5
--> $DIR/privacy-struct-ctor.rs:39:5
|
38 | m::S; //~ ERROR tuple struct `S` is private
39 | m::S;
| ^^^^
error[E0603]: tuple struct `Z` is private
--> $DIR/privacy-struct-ctor.rs:41:5
error[E0603]: tuple struct `S` is private
--> $DIR/privacy-struct-ctor.rs:41:16
|
41 | m::n::Z; //~ ERROR tuple struct `Z` is private
41 | let _: S = m::S(2);
| ^^^^
error[E0603]: tuple struct `Z` is private
--> $DIR/privacy-struct-ctor.rs:45:5
|
45 | m::n::Z;
| ^^^^^^^
error[E0603]: tuple struct `S` is private
--> $DIR/privacy-struct-ctor.rs:46:5
--> $DIR/privacy-struct-ctor.rs:51:5
|
46 | xcrate::m::S; //~ ERROR tuple struct `S` is private
51 | xcrate::m::S;
| ^^^^^^^^^^^^
error[E0603]: tuple struct `Z` is private
--> $DIR/privacy-struct-ctor.rs:49:5
--> $DIR/privacy-struct-ctor.rs:55:5
|
49 | xcrate::m::n::Z; //~ ERROR tuple struct `Z` is private
55 | xcrate::m::n::Z;
| ^^^^^^^^^^^^^^^
error: aborting due to 9 previous errors
error: aborting due to 10 previous errors

View file

@ -4,7 +4,7 @@ error[E0423]: expected function, found self type `Self`
16 | let s = Self(0, 1); //~ ERROR expected function
| ^^^^ not a function
|
= note: can't instantiate `Self`, you must use the implemented struct directly
= note: can't use `Self` as a constructor, you must use the implemented struct
error[E0532]: expected tuple struct/variant, found self type `Self`
--> $DIR/tuple-struct-alias.rs:18:13
@ -12,19 +12,23 @@ error[E0532]: expected tuple struct/variant, found self type `Self`
18 | Self(..) => {} //~ ERROR expected tuple struct/variant
| ^^^^ not a tuple struct/variant
|
= note: can't instantiate `Self`, you must use the implemented struct directly
= note: can't use `Self` as a constructor, you must use the implemented struct
error[E0423]: expected function, found type alias `A`
--> $DIR/tuple-struct-alias.rs:24:13
|
24 | let s = A(0, 1); //~ ERROR expected function
| ^ did you mean `S`?
|
= note: can't use a type alias as a constructor
error[E0532]: expected tuple struct/variant, found type alias `A`
--> $DIR/tuple-struct-alias.rs:26:9
|
26 | A(..) => {} //~ ERROR expected tuple struct/variant
| ^ did you mean `S`?
|
= note: can't use a type alias as a constructor
error: aborting due to 4 previous errors