Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4bf6116

Browse files
committedFeb 22, 2020
Add to_stringified_ident_guess to Symbol and suggest correct raw identifier in notes and help messages
1 parent 477dac3 commit 4bf6116

File tree

14 files changed

+144
-72
lines changed

14 files changed

+144
-72
lines changed
 

‎src/librustc/infer/error_reporting/need_type_info.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use rustc_hir::def::{DefKind, Namespace};
99
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
1010
use rustc_hir::{Body, Expr, ExprKind, FunctionRetTy, HirId, Local, Pat};
1111
use rustc_span::source_map::DesugaringKind;
12-
use rustc_span::symbol::kw;
1312
use rustc_span::Span;
13+
use rustc_span::{symbol::kw, Symbol};
1414
use std::borrow::Cow;
1515

1616
struct FindLocalByTypeVisitor<'a, 'tcx> {
@@ -108,7 +108,7 @@ fn closure_return_type_suggestion(
108108
output: &FunctionRetTy<'_>,
109109
body: &Body<'_>,
110110
descr: &str,
111-
name: &str,
111+
name: Symbol,
112112
ret: &str,
113113
parent_name: Option<String>,
114114
parent_descr: Option<&str>,
@@ -129,7 +129,7 @@ fn closure_return_type_suggestion(
129129
suggestion,
130130
Applicability::HasPlaceholders,
131131
);
132-
err.span_label(span, InferCtxt::missing_type_msg(&name, &descr, parent_name, parent_descr));
132+
err.span_label(span, InferCtxt::missing_type_msg(name, &descr, parent_name, parent_descr));
133133
}
134134

135135
/// Given a closure signature, return a `String` containing a list of all its argument types.
@@ -320,7 +320,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
320320
&decl.output,
321321
&body,
322322
&descr,
323-
&name,
323+
Symbol::intern(&name),
324324
&ret,
325325
parent_name,
326326
parent_descr,
@@ -444,7 +444,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
444444
// Avoid multiple labels pointing at `span`.
445445
err.span_label(
446446
span,
447-
InferCtxt::missing_type_msg(&name, &descr, parent_name, parent_descr),
447+
InferCtxt::missing_type_msg(
448+
Symbol::intern(&name),
449+
&descr,
450+
parent_name,
451+
parent_descr,
452+
),
448453
);
449454
}
450455

@@ -518,17 +523,20 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
518523
"type inside {} must be known in this context",
519524
kind,
520525
);
521-
err.span_label(span, InferCtxt::missing_type_msg(&name, &descr, parent_name, parent_descr));
526+
err.span_label(
527+
span,
528+
InferCtxt::missing_type_msg(Symbol::intern(&name), &descr, parent_name, parent_descr),
529+
);
522530
err
523531
}
524532

525533
fn missing_type_msg(
526-
type_name: &str,
534+
type_name: Symbol,
527535
descr: &str,
528536
parent_name: Option<String>,
529537
parent_descr: Option<&str>,
530538
) -> Cow<'static, str> {
531-
if type_name == "_" {
539+
if type_name == kw::Underscore {
532540
"cannot infer type".into()
533541
} else {
534542
let parent_desc = if let Some(parent_name) = parent_name {

‎src/librustc/infer/error_reporting/note.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
3636
}
3737
infer::ReborrowUpvar(span, ref upvar_id) => {
3838
let var_name = self.tcx.hir().name(upvar_id.var_path.hir_id);
39-
err.span_note(span, &format!("...so that closure can access `{}`", var_name));
39+
err.span_note(
40+
span,
41+
&format!(
42+
"...so that closure can access `{}`",
43+
var_name.to_stringified_ident_guess()
44+
),
45+
);
4046
}
4147
infer::InfStackClosure(span) => {
4248
err.span_note(span, "...so that closure does not outlive its stack frame");
@@ -53,7 +59,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
5359
&format!(
5460
"...so that captured variable `{}` does not outlive the \
5561
enclosing closure",
56-
self.tcx.hir().name(id)
62+
self.tcx.hir().name(id).to_stringified_ident_guess()
5763
),
5864
);
5965
}

‎src/librustc/middle/stability.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub fn deprecation_suggestion(
161161
diag.span_suggestion(
162162
span,
163163
"replace the use of the deprecated item",
164-
suggestion.to_string(),
164+
suggestion.to_stringified_ident_guess(),
165165
Applicability::MachineApplicable,
166166
);
167167
}

‎src/librustc/traits/error_reporting/suggestions.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_hir::def_id::DefId;
1717
use rustc_hir::intravisit::Visitor;
1818
use rustc_hir::Node;
1919
use rustc_span::source_map::SourceMap;
20-
use rustc_span::symbol::{kw, sym};
20+
use rustc_span::symbol::{kw, sym, Symbol};
2121
use rustc_span::{MultiSpan, Span, DUMMY_SP};
2222
use std::fmt;
2323

@@ -143,12 +143,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
143143
{
144144
// Missing generic type parameter bound.
145145
let param_name = self_ty.to_string();
146+
let param = Symbol::intern(&param_name);
146147
let constraint = trait_ref.print_only_trait_path().to_string();
147148
if suggest_constraining_type_param(
148149
self.tcx,
149150
generics,
150151
&mut err,
151-
&param_name,
152+
param,
152153
&constraint,
153154
self.tcx.sess.source_map(),
154155
*span,
@@ -1657,30 +1658,30 @@ pub fn suggest_constraining_type_param(
16571658
tcx: TyCtxt<'_>,
16581659
generics: &hir::Generics<'_>,
16591660
err: &mut DiagnosticBuilder<'_>,
1660-
param_name: &str,
1661+
param_name: Symbol,
16611662
constraint: &str,
16621663
source_map: &SourceMap,
16631664
span: Span,
16641665
def_id: Option<DefId>,
16651666
) -> bool {
16661667
let restrict_msg = "consider further restricting this bound";
16671668
if let Some(param) =
1668-
generics.params.iter().filter(|p| p.name.ident().as_str() == param_name).next()
1669+
generics.params.iter().filter(|p| p.name.ident().as_str() == param_name.as_str()).next()
16691670
{
16701671
if def_id == tcx.lang_items().sized_trait() {
16711672
// Type parameters are already `Sized` by default.
16721673
err.span_label(
16731674
param.span,
16741675
&format!("this type parameter needs to be `{}`", constraint),
16751676
);
1676-
} else if param_name.starts_with("impl ") {
1677+
} else if param_name.as_str().starts_with("impl ") {
16771678
// `impl Trait` in argument:
16781679
// `fn foo(x: impl Trait) {}` → `fn foo(t: impl Trait + Trait2) {}`
16791680
err.span_suggestion(
16801681
param.span,
16811682
restrict_msg,
16821683
// `impl CurrentTrait + MissingTrait`
1683-
format!("{} + {}", param_name, constraint),
1684+
format!("{} + {}", param_name.to_stringified_ident_guess(), constraint),
16841685
Applicability::MachineApplicable,
16851686
);
16861687
} else if generics.where_clause.predicates.is_empty() && param.bounds.is_empty() {
@@ -1690,7 +1691,7 @@ pub fn suggest_constraining_type_param(
16901691
err.span_suggestion(
16911692
param.span,
16921693
"consider restricting this bound",
1693-
format!("{}: {}", param_name, constraint),
1694+
format!("{}: {}", param_name.to_stringified_ident_guess(), constraint),
16941695
Applicability::MachineApplicable,
16951696
);
16961697
} else if !generics.where_clause.predicates.is_empty() {
@@ -1699,8 +1700,11 @@ pub fn suggest_constraining_type_param(
16991700
// `fn foo<T>(t: T) where T: Debug, T: Trait {}`
17001701
err.span_suggestion(
17011702
generics.where_clause.span().unwrap().shrink_to_hi(),
1702-
&format!("consider further restricting type parameter `{}`", param_name),
1703-
format!(", {}: {}", param_name, constraint),
1703+
&format!(
1704+
"consider further restricting type parameter `{}`",
1705+
param_name.to_stringified_ident_guess()
1706+
),
1707+
format!(", {}: {}", param_name.to_stringified_ident_guess(), constraint),
17041708
Applicability::MachineApplicable,
17051709
);
17061710
} else {
@@ -1716,13 +1720,17 @@ pub fn suggest_constraining_type_param(
17161720
err.span_suggestion(
17171721
span,
17181722
restrict_msg,
1719-
format!("{}: {} + ", param_name, constraint),
1723+
format!("{}: {} + ", param_name.to_stringified_ident_guess(), constraint),
17201724
Applicability::MachineApplicable,
17211725
);
17221726
} else {
17231727
err.span_label(
17241728
param.span,
1725-
&format!("consider adding a `where {}: {}` bound", param_name, constraint),
1729+
&format!(
1730+
"consider adding a `where {}: {}` bound",
1731+
param_name.to_stringified_ident_guess(),
1732+
constraint
1733+
),
17261734
);
17271735
}
17281736
}

‎src/librustc_lint/nonstandard_style.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ impl NonCamelCaseTypes {
107107
let name = &ident.name.as_str();
108108

109109
if !is_camel_case(name) {
110-
let msg = format!("{} `{}` should have an upper camel case name", sort, name);
110+
let msg =
111+
format!("{} `{}` should have an upper camel case name", sort, name.to_string());
111112
cx.struct_span_lint(NON_CAMEL_CASE_TYPES, ident.span, &msg)
112113
.span_suggestion(
113114
ident.span,

‎src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
220220
tcx,
221221
generics,
222222
&mut err,
223-
&param.name.as_str(),
223+
param.name,
224224
"Copy",
225225
tcx.sess.source_map(),
226226
span,

‎src/librustc_mir/borrow_check/diagnostics/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
107107
&format!(
108108
"closure cannot be invoked more than once because it moves the \
109109
variable `{}` out of its environment",
110-
name,
110+
name.to_stringified_ident_guess(),
111111
),
112112
);
113113
return;
@@ -129,7 +129,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
129129
&format!(
130130
"closure cannot be moved more than once as it is not `Copy` due to \
131131
moving the variable `{}` out of its environment",
132-
name
132+
name.to_stringified_ident_guess()
133133
),
134134
);
135135
}

‎src/librustc_parse/parser/module.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{new_sub_parser_from_file, DirectoryOwnership};
66

77
use rustc_errors::PResult;
88
use rustc_span::source_map::{FileName, SourceMap, Span, DUMMY_SP};
9-
use rustc_span::symbol::sym;
9+
use rustc_span::symbol::{sym, Symbol};
1010
use syntax::ast::{self, Attribute, Crate, Ident, ItemKind, Mod};
1111
use syntax::attr;
1212
use syntax::token::{self, TokenKind};
@@ -170,7 +170,7 @@ impl<'a> Parser<'a> {
170170
&format!(
171171
"... or maybe `use` the module `{}` instead \
172172
of possibly redeclaring it",
173-
paths.name
173+
Symbol::intern(&paths.name).to_stringified_ident_guess()
174174
),
175175
);
176176
}

0 commit comments

Comments
 (0)
Please sign in to comment.