Skip to content

Commit 906b399

Browse files
authored
Rollup merge of #69942 - estebank:sized-verbose-sugg, r=matthewjasper
Increase verbosity when suggesting subtle code changes Do not suggest changes that are actually quite small inline, to minimize the likelihood of confusion. Fix #69243.
2 parents 61a56fb + 9175940 commit 906b399

File tree

137 files changed

+818
-663
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+818
-663
lines changed

src/librustc_infer/infer/error_reporting/need_type_info.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::infer::InferCtxt;
33
use rustc::hir::map::Map;
44
use rustc::ty::print::Print;
55
use rustc::ty::{self, DefIdTree, Infer, Ty, TyVar};
6-
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
6+
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
77
use rustc_hir as hir;
88
use rustc_hir::def::{DefKind, Namespace};
99
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
@@ -462,24 +462,19 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
462462
e: &Expr<'_>,
463463
err: &mut DiagnosticBuilder<'_>,
464464
) {
465-
if let (Ok(snippet), Some(tables), None) = (
466-
self.tcx.sess.source_map().span_to_snippet(segment.ident.span),
467-
self.in_progress_tables,
468-
&segment.args,
469-
) {
465+
if let (Some(tables), None) = (self.in_progress_tables, &segment.args) {
470466
let borrow = tables.borrow();
471467
if let Some((DefKind::AssocFn, did)) = borrow.type_dependent_def(e.hir_id) {
472468
let generics = self.tcx.generics_of(did);
473469
if !generics.params.is_empty() {
474-
err.span_suggestion(
475-
segment.ident.span,
470+
err.span_suggestion_verbose(
471+
segment.ident.span.shrink_to_hi(),
476472
&format!(
477473
"consider specifying the type argument{} in the method call",
478-
if generics.params.len() > 1 { "s" } else { "" },
474+
pluralize!(generics.params.len()),
479475
),
480476
format!(
481-
"{}::<{}>",
482-
snippet,
477+
"::<{}>",
483478
generics
484479
.params
485480
.iter()

src/librustc_privacy/lib.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ impl<'a, 'tcx> NamePrivacyVisitor<'a, 'tcx> {
10381038
def.variant_descr(),
10391039
self.tcx.def_path_str(def.did)
10401040
)
1041-
.span_label(span, format!("field `{}` is private", field.ident))
1041+
.span_label(span, "private field")
10421042
.emit();
10431043
}
10441044
}
@@ -1180,7 +1180,11 @@ impl<'a, 'tcx> TypePrivacyVisitor<'a, 'tcx> {
11801180
fn check_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool {
11811181
let is_error = !self.item_is_accessible(def_id);
11821182
if is_error {
1183-
self.tcx.sess.span_err(self.span, &format!("{} `{}` is private", kind, descr));
1183+
self.tcx
1184+
.sess
1185+
.struct_span_err(self.span, &format!("{} `{}` is private", kind, descr))
1186+
.span_label(self.span, &format!("private {}", kind))
1187+
.emit();
11841188
}
11851189
is_error
11861190
}
@@ -1313,8 +1317,12 @@ impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
13131317
hir::QPath::Resolved(_, ref path) => path.to_string(),
13141318
hir::QPath::TypeRelative(_, ref segment) => segment.ident.to_string(),
13151319
};
1316-
let msg = format!("{} `{}` is private", kind.descr(def_id), name);
1317-
self.tcx.sess.span_err(span, &msg);
1320+
let kind = kind.descr(def_id);
1321+
self.tcx
1322+
.sess
1323+
.struct_span_err(span, &format!("{} `{}` is private", kind, name))
1324+
.span_label(span, &format!("private {}", kind))
1325+
.emit();
13181326
return;
13191327
}
13201328
}

src/librustc_resolve/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ impl<'a> Resolver<'a> {
951951
let descr = get_descr(binding);
952952
let mut err =
953953
struct_span_err!(self.session, ident.span, E0603, "{} `{}` is private", descr, ident);
954-
err.span_label(ident.span, &format!("this {} is private", descr));
954+
err.span_label(ident.span, &format!("private {}", descr));
955955
if let Some(span) = ctor_fields_span {
956956
err.span_label(span, "a constructor is private if any of the fields is private");
957957
}

src/librustc_resolve/late/diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,10 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
506506

507507
match (res, source) {
508508
(Res::Def(DefKind::Macro(MacroKind::Bang), _), _) => {
509-
err.span_suggestion(
510-
span,
509+
err.span_suggestion_verbose(
510+
span.shrink_to_hi(),
511511
"use `!` to invoke the macro",
512-
format!("{}!", path_str),
512+
"!".to_string(),
513513
Applicability::MaybeIncorrect,
514514
);
515515
if path_str == "try" && span.rust_2015() {

src/librustc_trait_selection/traits/error_reporting/mod.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -815,11 +815,11 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
815815
// For example, if `expected_args_length` is 2, suggest `|_, _|`.
816816
if found_args.is_empty() && is_closure {
817817
let underscores = vec!["_"; expected_args.len()].join(", ");
818-
err.span_suggestion(
818+
err.span_suggestion_verbose(
819819
pipe_span,
820820
&format!(
821821
"consider changing the closure to take and ignore the expected argument{}",
822-
if expected_args.len() < 2 { "" } else { "s" }
822+
pluralize!(expected_args.len())
823823
),
824824
format!("|{}|", underscores),
825825
Applicability::MachineApplicable,
@@ -833,7 +833,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
833833
.map(|(name, _)| name.to_owned())
834834
.collect::<Vec<String>>()
835835
.join(", ");
836-
err.span_suggestion(
836+
err.span_suggestion_verbose(
837837
found_span,
838838
"change the closure to take multiple arguments instead of a single tuple",
839839
format!("|{}|", sugg),
@@ -870,7 +870,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
870870
String::new()
871871
},
872872
);
873-
err.span_suggestion(
873+
err.span_suggestion_verbose(
874874
found_span,
875875
"change the closure to accept a tuple instead of individual arguments",
876876
sugg,
@@ -1420,15 +1420,14 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
14201420
// |
14211421
// = note: cannot resolve `_: Tt`
14221422

1423-
err.span_suggestion(
1424-
span,
1423+
err.span_suggestion_verbose(
1424+
span.shrink_to_hi(),
14251425
&format!(
14261426
"consider specifying the type argument{} in the function call",
1427-
if generics.params.len() > 1 { "s" } else { "" },
1427+
pluralize!(generics.params.len()),
14281428
),
14291429
format!(
1430-
"{}::<{}>",
1431-
snippet,
1430+
"::<{}>",
14321431
generics
14331432
.params
14341433
.iter()
@@ -1590,7 +1589,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
15901589
[] => (span.shrink_to_hi(), ":"),
15911590
[.., bound] => (bound.span().shrink_to_hi(), " + "),
15921591
};
1593-
err.span_suggestion(
1592+
err.span_suggestion_verbose(
15941593
span,
15951594
"consider relaxing the implicit `Sized` restriction",
15961595
format!("{} ?Sized", separator),

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
390390
}
391391
let hir = self.tcx.hir();
392392
// Get the name of the callable and the arguments to be used in the suggestion.
393-
let snippet = match hir.get_if_local(def_id) {
393+
let (snippet, sugg) = match hir.get_if_local(def_id) {
394394
Some(hir::Node::Expr(hir::Expr {
395395
kind: hir::ExprKind::Closure(_, decl, _, span, ..),
396396
..
@@ -401,7 +401,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
401401
None => return,
402402
};
403403
let args = decl.inputs.iter().map(|_| "_").collect::<Vec<_>>().join(", ");
404-
format!("{}({})", name, args)
404+
let sugg = format!("({})", args);
405+
(format!("{}{}", name, sugg), sugg)
405406
}
406407
Some(hir::Node::Item(hir::Item {
407408
ident,
@@ -422,7 +423,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
422423
})
423424
.collect::<Vec<_>>()
424425
.join(", ");
425-
format!("{}({})", ident, args)
426+
let sugg = format!("({})", args);
427+
(format!("{}{}", ident, sugg), sugg)
426428
}
427429
_ => return,
428430
};
@@ -431,10 +433,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
431433
// an argument, the `obligation.cause.span` points at the expression
432434
// of the argument, so we can provide a suggestion. This is signaled
433435
// by `points_at_arg`. Otherwise, we give a more general note.
434-
err.span_suggestion(
435-
obligation.cause.span,
436+
err.span_suggestion_verbose(
437+
obligation.cause.span.shrink_to_hi(),
436438
&msg,
437-
snippet,
439+
sugg,
438440
Applicability::HasPlaceholders,
439441
);
440442
} else {
@@ -619,7 +621,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
619621
.source_map()
620622
.span_take_while(span, |c| c.is_whitespace() || *c == '&');
621623
if points_at_arg && mutability == hir::Mutability::Not && refs_number > 0 {
622-
err.span_suggestion(
624+
err.span_suggestion_verbose(
623625
sp,
624626
"consider changing this borrow's mutability",
625627
"&mut ".to_string(),

src/librustc_typeck/astconv.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1452,8 +1452,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14521452
.expect("missing associated type");
14531453

14541454
if !assoc_ty.vis.is_accessible_from(def_scope, tcx) {
1455-
let msg = format!("associated type `{}` is private", binding.item_name);
1456-
tcx.sess.span_err(binding.span, &msg);
1455+
tcx.sess
1456+
.struct_span_err(
1457+
binding.span,
1458+
&format!("associated type `{}` is private", binding.item_name),
1459+
)
1460+
.span_label(binding.span, "private associated type")
1461+
.emit();
14571462
}
14581463
tcx.check_stability(assoc_ty.def_id, Some(hir_ref_id), binding.span);
14591464

@@ -2315,8 +2320,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
23152320

23162321
let kind = DefKind::AssocTy;
23172322
if !item.vis.is_accessible_from(def_scope, tcx) {
2318-
let msg = format!("{} `{}` is private", kind.descr(item.def_id), assoc_ident);
2319-
tcx.sess.span_err(span, &msg);
2323+
let kind = kind.descr(item.def_id);
2324+
let msg = format!("{} `{}` is private", kind, assoc_ident);
2325+
tcx.sess
2326+
.struct_span_err(span, &msg)
2327+
.span_label(span, &format!("private {}", kind))
2328+
.emit();
23202329
}
23212330
tcx.check_stability(item.def_id, Some(hir_ref_id), span);
23222331

src/librustc_typeck/check/expr.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1580,13 +1580,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15801580
};
15811581
let mut err = struct_span_err!(
15821582
self.tcx().sess,
1583-
expr.span,
1583+
field.span,
15841584
E0616,
15851585
"field `{}` of {} `{}` is private",
15861586
field,
15871587
kind_name,
15881588
struct_path
15891589
);
1590+
err.span_label(field.span, "private field");
15901591
// Also check if an accessible method exists, which is often what is meant.
15911592
if self.method_exists(field, expr_t, expr.hir_id, false) && !self.expr_in_place(expr.hir_id)
15921593
{
@@ -1611,7 +1612,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16111612
field,
16121613
expr_t
16131614
);
1614-
1615+
err.span_label(field.span, "method, not a field");
16151616
if !self.expr_in_place(expr.hir_id) {
16161617
self.suggest_method_call(
16171618
&mut err,

src/librustc_typeck/check/method/mod.rs

+11-17
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
137137
self_ty: Ty<'tcx>,
138138
call_expr: &hir::Expr<'_>,
139139
) {
140-
let has_params = self
140+
let params = self
141141
.probe_for_name(
142142
method_name.span,
143143
probe::Mode::MethodCall,
@@ -147,26 +147,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
147147
call_expr.hir_id,
148148
ProbeScope::TraitsInScope,
149149
)
150-
.and_then(|pick| {
150+
.map(|pick| {
151151
let sig = self.tcx.fn_sig(pick.item.def_id);
152-
Ok(sig.inputs().skip_binder().len() > 1)
153-
});
152+
sig.inputs().skip_binder().len().saturating_sub(1)
153+
})
154+
.unwrap_or(0);
154155

155156
// Account for `foo.bar<T>`;
156-
let sugg_span = method_name.span.with_hi(call_expr.span.hi());
157-
let snippet = self
158-
.tcx
159-
.sess
160-
.source_map()
161-
.span_to_snippet(sugg_span)
162-
.unwrap_or_else(|_| method_name.to_string());
163-
let (suggestion, applicability) = if has_params.unwrap_or_default() {
164-
(format!("{}(...)", snippet), Applicability::HasPlaceholders)
165-
} else {
166-
(format!("{}()", snippet), Applicability::MaybeIncorrect)
167-
};
157+
let sugg_span = call_expr.span.shrink_to_hi();
158+
let (suggestion, applicability) = (
159+
format!("({})", (0..params).map(|_| "_").collect::<Vec<_>>().join(", ")),
160+
if params > 0 { Applicability::HasPlaceholders } else { Applicability::MaybeIncorrect },
161+
);
168162

169-
err.span_suggestion(sugg_span, msg, suggestion, applicability);
163+
err.span_suggestion_verbose(sugg_span, msg, suggestion, applicability);
170164
}
171165

172166
/// Performs method lookup. If lookup is successful, it will return the callee

src/librustc_typeck/check/method/suggest.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -758,25 +758,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
758758
MethodError::Ambiguity(sources) => {
759759
let mut err = struct_span_err!(
760760
self.sess(),
761-
span,
761+
item_name.span,
762762
E0034,
763763
"multiple applicable items in scope"
764764
);
765-
err.span_label(span, format!("multiple `{}` found", item_name));
765+
err.span_label(item_name.span, format!("multiple `{}` found", item_name));
766766

767767
report_candidates(span, &mut err, sources, sugg_span);
768768
err.emit();
769769
}
770770

771771
MethodError::PrivateMatch(kind, def_id, out_of_scope_traits) => {
772+
let kind = kind.descr(def_id);
772773
let mut err = struct_span_err!(
773774
self.tcx.sess,
774-
span,
775+
item_name.span,
775776
E0624,
776777
"{} `{}` is private",
777-
kind.descr(def_id),
778+
kind,
778779
item_name
779780
);
781+
err.span_label(item_name.span, &format!("private {}", kind));
780782
self.suggest_valid_traits(&mut err, out_of_scope_traits);
781783
err.emit();
782784
}

src/librustc_typeck/check/mod.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -4939,15 +4939,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
49394939
}
49404940
_ => {}
49414941
}
4942-
if let Ok(code) = self.sess().source_map().span_to_snippet(expr.span) {
4943-
err.span_suggestion(
4944-
expr.span,
4945-
&format!("use parentheses to {}", msg),
4946-
format!("{}({})", code, sugg_call),
4947-
applicability,
4948-
);
4949-
return true;
4950-
}
4942+
err.span_suggestion_verbose(
4943+
expr.span.shrink_to_hi(),
4944+
&format!("use parentheses to {}", msg),
4945+
format!("({})", sugg_call),
4946+
applicability,
4947+
);
4948+
return true;
49514949
}
49524950
false
49534951
}

0 commit comments

Comments
 (0)