Skip to content

Commit 8c4f1d5

Browse files
committed
review comments
1 parent 3980342 commit 8c4f1d5

File tree

3 files changed

+84
-66
lines changed

3 files changed

+84
-66
lines changed

src/librustc_typeck/check/method/suggest.rs

+76-58
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc::traits::Obligation;
1515
use rustc::ty::{self, Ty, TyCtxt, ToPolyTraitRef, ToPredicate, TypeFoldable};
1616
use rustc::ty::print::with_crate_prefix;
1717
use syntax_pos::{Span, FileName};
18-
use syntax::ast;
18+
use syntax::{ast, source_map};
1919
use syntax::util::lev_distance;
2020

2121
use rustc_error_codes::*;
@@ -79,61 +79,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7979
return None;
8080
}
8181

82-
let print_disambiguation_help = |
83-
err: &mut DiagnosticBuilder<'_>,
84-
trait_name: String,
85-
rcvr_ty: Ty<'_>,
86-
kind: ty::AssocKind,
87-
span: Span,
88-
candidate: Option<usize>,
89-
| {
90-
let mut applicability = Applicability::MachineApplicable;
91-
let sugg_args = if let ty::AssocKind::Method = kind {
92-
format!(
93-
"({}{})",
94-
if rcvr_ty.is_region_ptr() && args.is_some() {
95-
if rcvr_ty.is_mutable_ptr() {
96-
"&mut "
97-
} else {
98-
"&"
99-
}
100-
} else {
101-
""
102-
},
103-
args.map(|arg| arg
104-
.iter()
105-
.map(|arg| self.tcx.sess.source_map().span_to_snippet(arg.span)
106-
.unwrap_or_else(|_| {
107-
applicability = Applicability::HasPlaceholders;
108-
"...".to_owned()
109-
}))
110-
.collect::<Vec<_>>()
111-
.join(", ")
112-
).unwrap_or_else(|| {
113-
applicability = Applicability::HasPlaceholders;
114-
"...".to_owned()
115-
}),
116-
)
117-
} else {
118-
String::new()
119-
};
120-
let sugg = format!("{}::{}{}", trait_name, item_name, sugg_args);
121-
err.span_suggestion(
122-
span,
123-
&format!(
124-
"disambiguate the {} for {}",
125-
kind.suggestion_descr(),
126-
if let Some(candidate) = candidate {
127-
format!("candidate #{}", candidate)
128-
} else {
129-
"the candidate".to_string()
130-
},
131-
),
132-
sugg,
133-
applicability,
134-
);
135-
};
136-
13782
let report_candidates = |
13883
span: Span,
13984
err: &mut DiagnosticBuilder<'_>,
@@ -215,7 +160,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
215160
.map(|ty| *ty)
216161
.unwrap_or(rcvr_ty),
217162
};
218-
print_disambiguation_help(err, path, ty, item.kind, sugg_span, idx);
163+
print_disambiguation_help(
164+
item_name,
165+
args,
166+
err,
167+
path,
168+
ty,
169+
item.kind,
170+
sugg_span,
171+
idx,
172+
self.tcx.sess.source_map(),
173+
);
219174
}
220175
}
221176
CandidateSource::TraitSource(trait_did) => {
@@ -244,7 +199,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
244199
None
245200
};
246201
let path = self.tcx.def_path_str(trait_did);
247-
print_disambiguation_help(err, path, rcvr_ty, item.kind, sugg_span, idx);
202+
print_disambiguation_help(
203+
item_name,
204+
args,
205+
err,
206+
path,
207+
rcvr_ty,
208+
item.kind,
209+
sugg_span,
210+
idx,
211+
self.tcx.sess.source_map(),
212+
);
248213
}
249214
}
250215
}
@@ -1180,3 +1145,56 @@ impl hir::intravisit::Visitor<'tcx> for UsePlacementFinder<'tcx> {
11801145
hir::intravisit::NestedVisitorMap::None
11811146
}
11821147
}
1148+
1149+
fn print_disambiguation_help(
1150+
item_name: ast::Ident,
1151+
args: Option<&'tcx [hir::Expr]>,
1152+
err: &mut DiagnosticBuilder<'_>,
1153+
trait_name: String,
1154+
rcvr_ty: Ty<'_>,
1155+
kind: ty::AssocKind,
1156+
span: Span,
1157+
candidate: Option<usize>,
1158+
source_map: &source_map::SourceMap,
1159+
) {
1160+
let mut applicability = Applicability::MachineApplicable;
1161+
let sugg_args = if let (ty::AssocKind::Method, Some(args)) = (kind, args) {
1162+
format!(
1163+
"({}{})",
1164+
if rcvr_ty.is_region_ptr() {
1165+
if rcvr_ty.is_mutable_ptr() {
1166+
"&mut "
1167+
} else {
1168+
"&"
1169+
}
1170+
} else {
1171+
""
1172+
},
1173+
args.iter()
1174+
.map(|arg| source_map.span_to_snippet(arg.span)
1175+
.unwrap_or_else(|_| {
1176+
applicability = Applicability::HasPlaceholders;
1177+
"_".to_owned()
1178+
}))
1179+
.collect::<Vec<_>>()
1180+
.join(", "),
1181+
)
1182+
} else {
1183+
String::new()
1184+
};
1185+
let sugg = format!("{}::{}{}", trait_name, item_name, sugg_args);
1186+
err.span_suggestion(
1187+
span,
1188+
&format!(
1189+
"disambiguate the {} for {}",
1190+
kind.suggestion_descr(),
1191+
if let Some(candidate) = candidate {
1192+
format!("candidate #{}", candidate)
1193+
} else {
1194+
"the candidate".to_string()
1195+
},
1196+
),
1197+
sugg,
1198+
applicability,
1199+
);
1200+
}

src/test/ui/error-codes/E0034.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ LL | fn foo() {}
1616
| ^^^^^^^^
1717
help: disambiguate the method call for candidate #1
1818
|
19-
LL | Trait1::foo(...)()
20-
| ^^^^^^^^^^^^^^^^
19+
LL | Trait1::foo()
20+
| ^^^^^^^^^^^
2121
help: disambiguate the method call for candidate #2
2222
|
23-
LL | Trait2::foo(...)()
24-
| ^^^^^^^^^^^^^^^^
23+
LL | Trait2::foo()
24+
| ^^^^^^^^^^^
2525

2626
error: aborting due to previous error
2727

src/test/ui/methods/method-ambig-two-traits-from-impls2.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ LL | fn foo() {}
1616
| ^^^^^^^^
1717
help: disambiguate the method call for candidate #1
1818
|
19-
LL | A::foo(...)();
20-
| ^^^^^^^^^^^
19+
LL | A::foo();
20+
| ^^^^^^
2121
help: disambiguate the method call for candidate #2
2222
|
23-
LL | B::foo(...)();
24-
| ^^^^^^^^^^^
23+
LL | B::foo();
24+
| ^^^^^^
2525

2626
error: aborting due to previous error
2727

0 commit comments

Comments
 (0)