Skip to content
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

Use structured suggestion for disambiguating method calls #67127

Merged
merged 2 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
review comments
  • Loading branch information
estebank committed Dec 12, 2019
commit 8c4f1d5f875e29e9a6e063744b71dc0a3c7deb6d
134 changes: 76 additions & 58 deletions src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rustc::traits::Obligation;
use rustc::ty::{self, Ty, TyCtxt, ToPolyTraitRef, ToPredicate, TypeFoldable};
use rustc::ty::print::with_crate_prefix;
use syntax_pos::{Span, FileName};
use syntax::ast;
use syntax::{ast, source_map};
use syntax::util::lev_distance;

use rustc_error_codes::*;
Expand Down Expand Up @@ -79,61 +79,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return None;
}

let print_disambiguation_help = |
err: &mut DiagnosticBuilder<'_>,
trait_name: String,
rcvr_ty: Ty<'_>,
kind: ty::AssocKind,
span: Span,
candidate: Option<usize>,
| {
let mut applicability = Applicability::MachineApplicable;
let sugg_args = if let ty::AssocKind::Method = kind {
format!(
"({}{})",
if rcvr_ty.is_region_ptr() && args.is_some() {
if rcvr_ty.is_mutable_ptr() {
"&mut "
} else {
"&"
}
} else {
""
},
args.map(|arg| arg
.iter()
.map(|arg| self.tcx.sess.source_map().span_to_snippet(arg.span)
.unwrap_or_else(|_| {
applicability = Applicability::HasPlaceholders;
"...".to_owned()
}))
.collect::<Vec<_>>()
.join(", ")
).unwrap_or_else(|| {
applicability = Applicability::HasPlaceholders;
"...".to_owned()
}),
)
} else {
String::new()
};
let sugg = format!("{}::{}{}", trait_name, item_name, sugg_args);
err.span_suggestion(
span,
&format!(
"disambiguate the {} for {}",
kind.suggestion_descr(),
if let Some(candidate) = candidate {
format!("candidate #{}", candidate)
} else {
"the candidate".to_string()
},
),
sugg,
applicability,
);
};

let report_candidates = |
span: Span,
err: &mut DiagnosticBuilder<'_>,
Expand Down Expand Up @@ -215,7 +160,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.map(|ty| *ty)
.unwrap_or(rcvr_ty),
};
print_disambiguation_help(err, path, ty, item.kind, sugg_span, idx);
print_disambiguation_help(
item_name,
args,
err,
path,
ty,
item.kind,
sugg_span,
idx,
self.tcx.sess.source_map(),
);
}
}
CandidateSource::TraitSource(trait_did) => {
Expand Down Expand Up @@ -244,7 +199,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
None
};
let path = self.tcx.def_path_str(trait_did);
print_disambiguation_help(err, path, rcvr_ty, item.kind, sugg_span, idx);
print_disambiguation_help(
item_name,
args,
err,
path,
rcvr_ty,
item.kind,
sugg_span,
idx,
self.tcx.sess.source_map(),
);
}
}
}
Expand Down Expand Up @@ -1180,3 +1145,56 @@ impl hir::intravisit::Visitor<'tcx> for UsePlacementFinder<'tcx> {
hir::intravisit::NestedVisitorMap::None
}
}

fn print_disambiguation_help(
item_name: ast::Ident,
args: Option<&'tcx [hir::Expr]>,
err: &mut DiagnosticBuilder<'_>,
trait_name: String,
rcvr_ty: Ty<'_>,
kind: ty::AssocKind,
span: Span,
candidate: Option<usize>,
source_map: &source_map::SourceMap,
) {
let mut applicability = Applicability::MachineApplicable;
let sugg_args = if let (ty::AssocKind::Method, Some(args)) = (kind, args) {
format!(
"({}{})",
if rcvr_ty.is_region_ptr() {
if rcvr_ty.is_mutable_ptr() {
"&mut "
} else {
"&"
}
} else {
""
},
args.iter()
.map(|arg| source_map.span_to_snippet(arg.span)
.unwrap_or_else(|_| {
applicability = Applicability::HasPlaceholders;
"_".to_owned()
}))
.collect::<Vec<_>>()
.join(", "),
)
} else {
String::new()
};
let sugg = format!("{}::{}{}", trait_name, item_name, sugg_args);
err.span_suggestion(
span,
&format!(
"disambiguate the {} for {}",
kind.suggestion_descr(),
if let Some(candidate) = candidate {
format!("candidate #{}", candidate)
} else {
"the candidate".to_string()
},
),
sugg,
applicability,
);
}
8 changes: 4 additions & 4 deletions src/test/ui/error-codes/E0034.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ LL | fn foo() {}
| ^^^^^^^^
help: disambiguate the method call for candidate #1
|
LL | Trait1::foo(...)()
| ^^^^^^^^^^^^^^^^
LL | Trait1::foo()
| ^^^^^^^^^^^
help: disambiguate the method call for candidate #2
|
LL | Trait2::foo(...)()
| ^^^^^^^^^^^^^^^^
LL | Trait2::foo()
| ^^^^^^^^^^^

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ LL | fn foo() {}
| ^^^^^^^^
help: disambiguate the method call for candidate #1
|
LL | A::foo(...)();
| ^^^^^^^^^^^
LL | A::foo();
| ^^^^^^
help: disambiguate the method call for candidate #2
|
LL | B::foo(...)();
| ^^^^^^^^^^^
LL | B::foo();
| ^^^^^^

error: aborting due to previous error

Expand Down