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

Correctly display raw identifier in notes and help messages #68963

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub fn deprecation_suggestion(
diag.span_suggestion(
span,
"replace the use of the deprecated item",
suggestion.to_string(),
suggestion.to_ident_string(),
Applicability::MachineApplicable,
);
}
Expand Down
24 changes: 16 additions & 8 deletions src/librustc_infer/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use rustc_hir::def::{DefKind, Namespace};
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::{Body, Expr, ExprKind, FnRetTy, HirId, Local, Pat};
use rustc_span::source_map::DesugaringKind;
use rustc_span::symbol::kw;
use rustc_span::Span;
use rustc_span::{symbol::kw, Symbol};
use std::borrow::Cow;

struct FindLocalByTypeVisitor<'a, 'tcx> {
Expand Down Expand Up @@ -111,7 +111,7 @@ fn closure_return_type_suggestion(
output: &FnRetTy<'_>,
body: &Body<'_>,
descr: &str,
name: &str,
name: Symbol,
ret: &str,
parent_name: Option<String>,
parent_descr: Option<&str>,
Expand All @@ -132,7 +132,7 @@ fn closure_return_type_suggestion(
suggestion,
Applicability::HasPlaceholders,
);
err.span_label(span, InferCtxt::missing_type_msg(&name, &descr, parent_name, parent_descr));
err.span_label(span, InferCtxt::missing_type_msg(name, &descr, parent_name, parent_descr));
}

/// Given a closure signature, return a `String` containing a list of all its argument types.
Expand Down Expand Up @@ -323,7 +323,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
&decl.output,
&body,
&descr,
&name,
Symbol::intern(&name),
&ret,
parent_name,
parent_descr,
Expand Down Expand Up @@ -447,7 +447,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
// Avoid multiple labels pointing at `span`.
err.span_label(
span,
InferCtxt::missing_type_msg(&name, &descr, parent_name, parent_descr),
InferCtxt::missing_type_msg(
Symbol::intern(&name),
&descr,
parent_name,
parent_descr,
),
);
}

Expand Down Expand Up @@ -521,17 +526,20 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
"type inside {} must be known in this context",
kind,
);
err.span_label(span, InferCtxt::missing_type_msg(&name, &descr, parent_name, parent_descr));
err.span_label(
span,
InferCtxt::missing_type_msg(Symbol::intern(&name), &descr, parent_name, parent_descr),
);
err
}

fn missing_type_msg(
type_name: &str,
type_name: Symbol,
descr: &str,
parent_name: Option<String>,
parent_descr: Option<&str>,
) -> Cow<'static, str> {
if type_name == "_" {
if type_name == kw::Underscore {
"cannot infer type".into()
} else {
let parent_desc = if let Some(parent_name) = parent_name {
Expand Down
7 changes: 5 additions & 2 deletions src/librustc_infer/infer/error_reporting/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
}
infer::ReborrowUpvar(span, ref upvar_id) => {
let var_name = self.tcx.hir().name(upvar_id.var_path.hir_id);
err.span_note(span, &format!("...so that closure can access `{}`", var_name));
err.span_note(
span,
&format!("...so that closure can access `{}`", var_name.to_ident_string()),
);
}
infer::InfStackClosure(span) => {
err.span_note(span, "...so that closure does not outlive its stack frame");
Expand All @@ -53,7 +56,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
&format!(
"...so that captured variable `{}` does not outlive the \
enclosing closure",
self.tcx.hir().name(id)
self.tcx.hir().name(id).to_ident_string()
),
);
}
Expand Down
38 changes: 25 additions & 13 deletions src/librustc_infer/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::{QPath, TyKind, WhereBoundPredicate, WherePredicate};
use rustc_span::source_map::SourceMap;
use rustc_span::{ExpnKind, Span, DUMMY_SP};
use rustc_span::{ExpnKind, Span, Symbol, DUMMY_SP};
use std::fmt;

impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
Expand Down Expand Up @@ -1421,7 +1421,7 @@ pub fn suggest_constraining_type_param(
tcx: TyCtxt<'_>,
generics: &hir::Generics<'_>,
err: &mut DiagnosticBuilder<'_>,
param_name: &str,
param_name: Symbol,
constraint: &str,
source_map: &SourceMap,
span: Span,
Expand All @@ -1431,7 +1431,7 @@ pub fn suggest_constraining_type_param(
const MSG_RESTRICT_TYPE: &str = "consider restricting this type parameter with";
const MSG_RESTRICT_TYPE_FURTHER: &str = "consider further restricting this type parameter with";

let param = generics.params.iter().find(|p| p.name.ident().as_str() == param_name);
let param = generics.params.iter().find(|p| p.name.ident().as_str() == param_name.as_str());

let param = if let Some(param) = param {
param
Expand All @@ -1445,7 +1445,7 @@ pub fn suggest_constraining_type_param(
return true;
}

if param_name.starts_with("impl ") {
if param_name.as_str().starts_with("impl ") {
// If there's an `impl Trait` used in argument position, suggest
// restricting it:
//
Expand Down Expand Up @@ -1501,7 +1501,7 @@ pub fn suggest_constraining_type_param(
err.tool_only_span_suggestion(
span_with_colon,
MSG_RESTRICT_BOUND_FURTHER,
format!("{}: {} + ", param_name, constraint),
format!("{}: {} + ", param_name.to_ident_string(), constraint),
Applicability::MachineApplicable,
);
}
Expand All @@ -1513,13 +1513,18 @@ pub fn suggest_constraining_type_param(

err.span_help(
param.span,
&format!("{} `{}: {}`", MSG_RESTRICT_TYPE, param_name, constraint),
&format!(
"{} `{}: {}`",
MSG_RESTRICT_TYPE,
param_name.to_ident_string(),
constraint
),
);

err.tool_only_span_suggestion(
param.span,
MSG_RESTRICT_TYPE,
format!("{}: {}", param_name, constraint),
format!("{}: {}", param_name.to_ident_string(), constraint),
Applicability::MachineApplicable,
);
}
Expand Down Expand Up @@ -1575,7 +1580,7 @@ pub fn suggest_constraining_type_param(
{
if let TyKind::Path(QPath::Resolved(_, path)) = &bounded_ty.kind {
if let Some(segment) = path.segments.first() {
if segment.ident.to_string() == param_name {
if segment.ident.to_string() == param_name.to_ident_string() {
param_spans.push(span);
}
}
Expand All @@ -1590,13 +1595,18 @@ pub fn suggest_constraining_type_param(
&[] => {
err.span_help(
param.span,
&format!("{} `where {}: {}`", MSG_RESTRICT_TYPE, param_name, constraint),
&format!(
"{} `where {}: {}`",
MSG_RESTRICT_TYPE,
param_name.to_ident_string(),
constraint
),
);

err.tool_only_span_suggestion(
where_clause_span,
MSG_RESTRICT_TYPE,
format!(", {}: {}", param_name, constraint),
format!(", {}: {}", param_name.to_ident_string(), constraint),
Applicability::MachineApplicable,
);
}
Expand All @@ -1614,7 +1624,7 @@ pub fn suggest_constraining_type_param(
err.tool_only_span_suggestion(
span_with_colon,
MSG_RESTRICT_BOUND_FURTHER,
format!("{}: {} +", param_name, constraint),
format!("{}: {} +", param_name.to_ident_string(), constraint),
Applicability::MachineApplicable,
);
}
Expand All @@ -1625,14 +1635,16 @@ pub fn suggest_constraining_type_param(
param.span,
&format!(
"{} `where {}: {}`",
MSG_RESTRICT_TYPE_FURTHER, param_name, constraint,
MSG_RESTRICT_TYPE_FURTHER,
param_name.to_ident_string(),
constraint,
),
);

err.tool_only_span_suggestion(
where_clause_span,
MSG_RESTRICT_BOUND_FURTHER,
format!(", {}: {}", param_name, constraint),
format!(", {}: {}", param_name.to_ident_string(), constraint),
Applicability::MachineApplicable,
);
}
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_infer/traits/error_reporting/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::Visitor;
use rustc_hir::Node;
use rustc_span::symbol::{kw, sym};
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::{MultiSpan, Span, DUMMY_SP};
use std::fmt;

Expand Down Expand Up @@ -142,12 +142,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
{
// Missing generic type parameter bound.
let param_name = self_ty.to_string();
let param = Symbol::intern(&param_name);
let constraint = trait_ref.print_only_trait_path().to_string();
if suggest_constraining_type_param(
self.tcx,
generics,
&mut err,
&param_name,
param,
&constraint,
self.tcx.sess.source_map(),
*span,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
tcx,
generics,
&mut err,
&param.name.as_str(),
param.name,
"Copy",
tcx.sess.source_map(),
span,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/borrow_check/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
&format!(
"closure cannot be invoked more than once because it moves the \
variable `{}` out of its environment",
name,
name.to_ident_string(),
),
);
return;
Expand All @@ -129,7 +129,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
&format!(
"closure cannot be moved more than once as it is not `Copy` due to \
moving the variable `{}` out of its environment",
name
name.to_ident_string()
),
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_parse/parser/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_ast::attr;
use rustc_ast::token::{self, TokenKind};
use rustc_errors::PResult;
use rustc_span::source_map::{FileName, SourceMap, Span, DUMMY_SP};
use rustc_span::symbol::sym;
use rustc_span::symbol::{sym, Symbol};

use std::path::{self, Path, PathBuf};

Expand Down Expand Up @@ -170,7 +170,7 @@ impl<'a> Parser<'a> {
&format!(
"... or maybe `use` the module `{}` instead \
of possibly redeclaring it",
paths.name
Symbol::intern(&paths.name).to_ident_string()
),
);
}
Expand Down
24 changes: 15 additions & 9 deletions src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl<'a> Resolver<'a> {
let help_msg = format!(
"if you meant to match on a variant or a `const` item, consider \
making the path in the pattern qualified: `?::{}`",
name,
name.to_ident_string(),
);
err.span_help(span, &help_msg);
}
Expand Down Expand Up @@ -779,12 +779,6 @@ impl<'a> Resolver<'a> {
suggestion.res.article(),
suggestion.res.descr()
);
err.span_suggestion(
span,
&msg,
suggestion.candidate.to_string(),
Applicability::MaybeIncorrect,
);
let def_span = suggestion.res.opt_def_id().and_then(|def_id| match def_id.krate {
LOCAL_CRATE => self.definitions.opt_span(def_id),
_ => Some(
Expand All @@ -793,16 +787,24 @@ impl<'a> Resolver<'a> {
.def_span(self.cstore().get_span_untracked(def_id, self.session)),
),
});
let candidate = def_span
.as_ref()
.map(|span| Ident::new(suggestion.candidate, *span).to_string())
.unwrap_or_else(|| suggestion.candidate.to_ident_string());

err.span_suggestion(span, &msg, candidate.clone(), Applicability::MaybeIncorrect);

if let Some(span) = def_span {
err.span_label(
span,
&format!(
"similarly named {} `{}` defined here",
suggestion.res.descr(),
suggestion.candidate.as_str(),
candidate,
),
);
}

return true;
}
false
Expand Down Expand Up @@ -1442,7 +1444,11 @@ crate fn show_candidates(
// produce an additional newline to separate the new use statement
// from the directly following item.
let additional_newline = if found_use { "" } else { "\n" };
*candidate = format!("use {};\n{}", candidate, additional_newline);
*candidate = format!(
"use {};\n{}",
Symbol::intern(candidate).to_ident_string(),
additional_newline
);
}

err.span_suggestions(span, &msg, path_strings.into_iter(), Applicability::Unspecified);
Expand Down
7 changes: 5 additions & 2 deletions src/librustc_typeck/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,11 +1199,14 @@ fn report_bivariance(tcx: TyCtxt<'_>, span: Span, param_name: ast::Name) {
let msg = if let Some(def_id) = suggested_marker_id {
format!(
"consider removing `{}`, referring to it in a field, or using a marker such as `{}`",
param_name,
param_name.to_ident_string(),
tcx.def_path_str(def_id),
)
} else {
format!("consider removing `{}` or referring to it in a field", param_name)
format!(
"consider removing `{}` or referring to it in a field",
param_name.to_ident_string()
)
};
err.help(&msg);
err.emit();
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/issues/issue-69053.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
struct r#struct<r#fn>;
//~^ WARNING should have an upper camel case name
//~^^ WARNING should have an upper camel case name
//~^^^ ERROR parameter `fn` is never used

fn main() {}
Loading