1
Fork 0

Check if E0530 is rustc_resolve::late::PatternSource::Match to emit suggestion

This commit is contained in:
Obei Sideg 2022-07-08 14:01:30 +03:00
parent ea46e7a47e
commit c2436d54d0
3 changed files with 14 additions and 11 deletions

View file

@ -28,7 +28,7 @@ use rustc_span::{BytePos, Span};
use tracing::debug; use tracing::debug;
use crate::imports::{Import, ImportKind, ImportResolver}; use crate::imports::{Import, ImportKind, ImportResolver};
use crate::late::Rib; use crate::late::{PatternSource, Rib};
use crate::path_names_to_string; use crate::path_names_to_string;
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingError, Finalize}; use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingError, Finalize};
use crate::{HasGenericParams, MacroRulesScope, Module, ModuleKind, ModuleOrUniformRoot}; use crate::{HasGenericParams, MacroRulesScope, Module, ModuleKind, ModuleOrUniformRoot};
@ -896,7 +896,7 @@ impl<'a> Resolver<'a> {
err err
} }
ResolutionError::BindingShadowsSomethingUnacceptable { ResolutionError::BindingShadowsSomethingUnacceptable {
shadowing_binding_descr, shadowing_binding,
name, name,
participle, participle,
article, article,
@ -909,15 +909,18 @@ impl<'a> Resolver<'a> {
span, span,
E0530, E0530,
"{}s cannot shadow {}s", "{}s cannot shadow {}s",
shadowing_binding_descr, shadowing_binding.descr(),
shadowed_binding_descr, shadowed_binding_descr,
); );
err.span_label( err.span_label(
span, span,
format!("cannot be named the same as {} {}", article, shadowed_binding_descr), format!("cannot be named the same as {} {}", article, shadowed_binding_descr),
); );
match shadowed_binding { match (shadowing_binding, shadowed_binding) {
Res::Def(DefKind::Ctor(CtorOf::Variant | CtorOf::Struct, CtorKind::Fn), _) => { (
PatternSource::Match,
Res::Def(DefKind::Ctor(CtorOf::Variant | CtorOf::Struct, CtorKind::Fn), _),
) => {
err.span_suggestion( err.span_suggestion(
span, span,
"try specify the pattern arguments", "try specify the pattern arguments",

View file

@ -50,7 +50,7 @@ struct BindingInfo {
} }
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
enum PatternSource { pub enum PatternSource {
Match, Match,
Let, Let,
For, For,
@ -64,7 +64,7 @@ enum IsRepeatExpr {
} }
impl PatternSource { impl PatternSource {
fn descr(self) -> &'static str { pub fn descr(self) -> &'static str {
match self { match self {
PatternSource::Match => "match binding", PatternSource::Match => "match binding",
PatternSource::Let => "let binding", PatternSource::Let => "let binding",
@ -2845,7 +2845,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.report_error( self.report_error(
ident.span, ident.span,
ResolutionError::BindingShadowsSomethingUnacceptable { ResolutionError::BindingShadowsSomethingUnacceptable {
shadowing_binding_descr: pat_src.descr(), shadowing_binding: pat_src,
name: ident.name, name: ident.name,
participle: if binding.is_import() { "imported" } else { "defined" }, participle: if binding.is_import() { "imported" } else { "defined" },
article: binding.res().article(), article: binding.res().article(),
@ -2861,7 +2861,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.report_error( self.report_error(
ident.span, ident.span,
ResolutionError::BindingShadowsSomethingUnacceptable { ResolutionError::BindingShadowsSomethingUnacceptable {
shadowing_binding_descr: pat_src.descr(), shadowing_binding: pat_src,
name: ident.name, name: ident.name,
participle: "defined", participle: "defined",
article: res.article(), article: res.article(),

View file

@ -61,7 +61,7 @@ use tracing::debug;
use diagnostics::{ImportSuggestion, LabelSuggestion, Suggestion}; use diagnostics::{ImportSuggestion, LabelSuggestion, Suggestion};
use imports::{Import, ImportKind, ImportResolver, NameResolution}; use imports::{Import, ImportKind, ImportResolver, NameResolution};
use late::{HasGenericParams, PathSource}; use late::{HasGenericParams, PathSource, PatternSource};
use macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef}; use macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
use crate::access_levels::AccessLevelsVisitor; use crate::access_levels::AccessLevelsVisitor;
@ -230,7 +230,7 @@ enum ResolutionError<'a> {
), ),
/// Error E0530: `X` bindings cannot shadow `Y`s. /// Error E0530: `X` bindings cannot shadow `Y`s.
BindingShadowsSomethingUnacceptable { BindingShadowsSomethingUnacceptable {
shadowing_binding_descr: &'static str, shadowing_binding: PatternSource,
name: Symbol, name: Symbol,
participle: &'static str, participle: &'static str,
article: &'static str, article: &'static str,