Suggest calling ctor when trait is unimplemented
This commit is contained in:
parent
a24a020e6d
commit
63be7a2424
@ -817,7 +817,14 @@ fn suggest_fn_call(
|
||||
|
||||
let (def_id, output_ty, callable) = match *self_ty.kind() {
|
||||
ty::Closure(def_id, substs) => (def_id, substs.as_closure().sig().output(), "closure"),
|
||||
ty::FnDef(def_id, _) => (def_id, self_ty.fn_sig(self.tcx).output(), "function"),
|
||||
ty::FnDef(def_id, _) => (
|
||||
def_id,
|
||||
self_ty.fn_sig(self.tcx).output(),
|
||||
match self.tcx.def_kind(def_id) {
|
||||
DefKind::Ctor(..) => "constructor",
|
||||
_ => "function",
|
||||
},
|
||||
),
|
||||
_ => return false,
|
||||
};
|
||||
let msg = format!("use parentheses to call the {}", callable);
|
||||
@ -878,6 +885,16 @@ fn suggest_fn_call(
|
||||
let sugg = format!("({})", args);
|
||||
(format!("{}{}", ident, sugg), sugg)
|
||||
}
|
||||
Some(hir::Node::Ctor(data)) => {
|
||||
let name = self.tcx.def_path_str(def_id);
|
||||
err.span_label(
|
||||
self.tcx.def_span(def_id),
|
||||
format!("consider calling the constructor for `{}`", name),
|
||||
);
|
||||
let args = data.fields().iter().map(|_| "_").collect::<Vec<_>>().join(", ");
|
||||
let sugg = format!("({})", args);
|
||||
(format!("{name}{sugg}"), sugg)
|
||||
}
|
||||
_ => return false,
|
||||
};
|
||||
if matches!(obligation.cause.code(), ObligationCauseCode::FunctionArgumentObligation { .. })
|
||||
|
17
src/test/ui/suggestions/call-on-unimplemented-ctor.rs
Normal file
17
src/test/ui/suggestions/call-on-unimplemented-ctor.rs
Normal file
@ -0,0 +1,17 @@
|
||||
fn main() {
|
||||
insert_resource(Marker);
|
||||
insert_resource(Time);
|
||||
//~^ ERROR the trait bound `fn(u32) -> Time {Time}: Resource` is not satisfied
|
||||
//~| HELP use parentheses to call the constructor
|
||||
}
|
||||
|
||||
trait Resource {}
|
||||
|
||||
fn insert_resource<R: Resource>(resource: R) {}
|
||||
|
||||
struct Marker;
|
||||
impl Resource for Marker {}
|
||||
|
||||
struct Time(u32);
|
||||
|
||||
impl Resource for Time {}
|
24
src/test/ui/suggestions/call-on-unimplemented-ctor.stderr
Normal file
24
src/test/ui/suggestions/call-on-unimplemented-ctor.stderr
Normal file
@ -0,0 +1,24 @@
|
||||
error[E0277]: the trait bound `fn(u32) -> Time {Time}: Resource` is not satisfied
|
||||
--> $DIR/call-on-unimplemented-ctor.rs:3:21
|
||||
|
|
||||
LL | insert_resource(Time);
|
||||
| --------------- ^^^^ the trait `Resource` is not implemented for fn item `fn(u32) -> Time {Time}`
|
||||
| |
|
||||
| required by a bound introduced by this call
|
||||
...
|
||||
LL | struct Time(u32);
|
||||
| ----------- consider calling the constructor for `Time`
|
||||
|
|
||||
note: required by a bound in `insert_resource`
|
||||
--> $DIR/call-on-unimplemented-ctor.rs:10:23
|
||||
|
|
||||
LL | fn insert_resource<R: Resource>(resource: R) {}
|
||||
| ^^^^^^^^ required by this bound in `insert_resource`
|
||||
help: use parentheses to call the constructor
|
||||
|
|
||||
LL | insert_resource(Time(_));
|
||||
| +++
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0277`.
|
Loading…
Reference in New Issue
Block a user