diff --git a/src/librustc/middle/stability.rs b/src/librustc/middle/stability.rs index 9d3df9623bd62..6f07e39fe26f9 100644 --- a/src/librustc/middle/stability.rs +++ b/src/librustc/middle/stability.rs @@ -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, ); } diff --git a/src/librustc_infer/infer/error_reporting/need_type_info.rs b/src/librustc_infer/infer/error_reporting/need_type_info.rs index a1e6a0a325ada..48669db3212c7 100644 --- a/src/librustc_infer/infer/error_reporting/need_type_info.rs +++ b/src/librustc_infer/infer/error_reporting/need_type_info.rs @@ -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> { @@ -111,7 +111,7 @@ fn closure_return_type_suggestion( output: &FnRetTy<'_>, body: &Body<'_>, descr: &str, - name: &str, + name: Symbol, ret: &str, parent_name: Option, parent_descr: Option<&str>, @@ -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. @@ -323,7 +323,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { &decl.output, &body, &descr, - &name, + Symbol::intern(&name), &ret, parent_name, parent_descr, @@ -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, + ), ); } @@ -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, 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 { diff --git a/src/librustc_infer/infer/error_reporting/note.rs b/src/librustc_infer/infer/error_reporting/note.rs index 5c0caa48d0e77..f252c10d61b14 100644 --- a/src/librustc_infer/infer/error_reporting/note.rs +++ b/src/librustc_infer/infer/error_reporting/note.rs @@ -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"); @@ -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() ), ); } diff --git a/src/librustc_infer/traits/error_reporting/mod.rs b/src/librustc_infer/traits/error_reporting/mod.rs index 63c3f827c2ecb..09a72815812d5 100644 --- a/src/librustc_infer/traits/error_reporting/mod.rs +++ b/src/librustc_infer/traits/error_reporting/mod.rs @@ -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> { @@ -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, @@ -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 @@ -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: // @@ -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, ); } @@ -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, ); } @@ -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); } } @@ -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, ); } @@ -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, ); } @@ -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, ); } diff --git a/src/librustc_infer/traits/error_reporting/suggestions.rs b/src/librustc_infer/traits/error_reporting/suggestions.rs index ed6cfa51cdf18..fbfcf81eb6c3f 100644 --- a/src/librustc_infer/traits/error_reporting/suggestions.rs +++ b/src/librustc_infer/traits/error_reporting/suggestions.rs @@ -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; @@ -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(¶m_name); let constraint = trait_ref.print_only_trait_path().to_string(); if suggest_constraining_type_param( self.tcx, generics, &mut err, - ¶m_name, + param, &constraint, self.tcx.sess.source_map(), *span, diff --git a/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs b/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs index ca51d16f9f269..cf6f13db27bd3 100644 --- a/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs +++ b/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs @@ -220,7 +220,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { tcx, generics, &mut err, - ¶m.name.as_str(), + param.name, "Copy", tcx.sess.source_map(), span, diff --git a/src/librustc_mir/borrow_check/diagnostics/mod.rs b/src/librustc_mir/borrow_check/diagnostics/mod.rs index cd6834a5a4d00..d59fce4fa9f9b 100644 --- a/src/librustc_mir/borrow_check/diagnostics/mod.rs +++ b/src/librustc_mir/borrow_check/diagnostics/mod.rs @@ -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; @@ -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() ), ); } diff --git a/src/librustc_parse/parser/module.rs b/src/librustc_parse/parser/module.rs index b436f1969bb48..3bd51450181ca 100644 --- a/src/librustc_parse/parser/module.rs +++ b/src/librustc_parse/parser/module.rs @@ -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}; @@ -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() ), ); } diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 18192a18cef36..0700a5969d1c9 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -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); } @@ -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( @@ -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 @@ -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); diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index e8e34a4e8f079..796fdc00aef84 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -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(); diff --git a/src/test/ui/issues/issue-69053.rs b/src/test/ui/issues/issue-69053.rs new file mode 100644 index 0000000000000..8bc847d8747db --- /dev/null +++ b/src/test/ui/issues/issue-69053.rs @@ -0,0 +1,6 @@ +struct r#struct; +//~^ WARNING should have an upper camel case name +//~^^ WARNING should have an upper camel case name +//~^^^ ERROR parameter `fn` is never used + +fn main() {} diff --git a/src/test/ui/issues/issue-69053.stderr b/src/test/ui/issues/issue-69053.stderr new file mode 100644 index 0000000000000..ff29f6df3f36b --- /dev/null +++ b/src/test/ui/issues/issue-69053.stderr @@ -0,0 +1,25 @@ +warning: type `struct` should have an upper camel case name + --> $DIR/issue-69053.rs:1:8 + | +LL | struct r#struct; + | ^^^^^^^^ help: convert the identifier to upper camel case: `Struct` + | + = note: `#[warn(non_camel_case_types)]` on by default + +warning: type parameter `fn` should have an upper camel case name + --> $DIR/issue-69053.rs:1:17 + | +LL | struct r#struct; + | ^^^^ help: convert the identifier to upper camel case: `Fn` + +error[E0392]: parameter `fn` is never used + --> $DIR/issue-69053.rs:1:17 + | +LL | struct r#struct; + | ^^^^ unused parameter + | + = help: consider removing `r#fn`, referring to it in a field, or using a marker such as `std::marker::PhantomData` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0392`. diff --git a/src/test/ui/span/issue-68962.rs b/src/test/ui/span/issue-68962.rs new file mode 100644 index 0000000000000..0b581308f6628 --- /dev/null +++ b/src/test/ui/span/issue-68962.rs @@ -0,0 +1,11 @@ +fn r#fn() {} + +fn main() { + let r#final = 1; + + // Should correctly suggest variable defined using raw identifier. + fina; //~ ERROR cannot find value + + // Should correctly suggest function defined using raw identifier. + f(); //~ ERROR cannot find function +} diff --git a/src/test/ui/span/issue-68962.stderr b/src/test/ui/span/issue-68962.stderr new file mode 100644 index 0000000000000..ca4c154fb07eb --- /dev/null +++ b/src/test/ui/span/issue-68962.stderr @@ -0,0 +1,18 @@ +error[E0425]: cannot find value `fina` in this scope + --> $DIR/issue-68962.rs:7:5 + | +LL | fina; + | ^^^^ help: a local variable with a similar name exists: `r#final` + +error[E0425]: cannot find function `f` in this scope + --> $DIR/issue-68962.rs:10:5 + | +LL | fn r#fn() {} + | ------------ similarly named function `r#fn` defined here +... +LL | f(); + | ^ help: a function with a similar name exists: `r#fn` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0425`.