1
Fork 0

Improve error for self: Box<self>

This commit is contained in:
clubby789 2023-05-11 12:25:01 +01:00
parent 4d941cd981
commit 3851a4bb91
9 changed files with 63 additions and 25 deletions

View file

@ -1253,7 +1253,7 @@ pub enum ExplicitSelf<'tcx> {
impl<'tcx> ExplicitSelf<'tcx> { impl<'tcx> ExplicitSelf<'tcx> {
/// Categorizes an explicit self declaration like `self: SomeType` /// Categorizes an explicit self declaration like `self: SomeType`
/// into either `self`, `&self`, `&mut self`, `Box<self>`, or /// into either `self`, `&self`, `&mut self`, `Box<Self>`, or
/// `Other`. /// `Other`.
/// This is mainly used to require the arbitrary_self_types feature /// This is mainly used to require the arbitrary_self_types feature
/// in the case of `Other`, to improve error messages in the common cases, /// in the case of `Other`, to improve error messages in the common cases,

View file

@ -199,6 +199,10 @@ resolve_invalid_asm_sym =
.label = is a local variable .label = is a local variable
.help = `sym` operands must refer to either a function or a static .help = `sym` operands must refer to either a function or a static
resolve_lowercase_self =
attempt to use a non-constant value in a constant
.suggestion = try using `Self`
resolve_trait_impl_duplicate = resolve_trait_impl_duplicate =
duplicate definitions with name `{$name}`: duplicate definitions with name `{$name}`:
.label = duplicate definition .label = duplicate definition

View file

@ -948,6 +948,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
ResolutionError::InvalidAsmSym => { ResolutionError::InvalidAsmSym => {
self.tcx.sess.create_err(errs::InvalidAsmSym { span }) self.tcx.sess.create_err(errs::InvalidAsmSym { span })
} }
ResolutionError::LowercaseSelf => {
self.tcx.sess.create_err(errs::LowercaseSelf { span })
}
} }
} }

View file

@ -442,6 +442,14 @@ pub(crate) struct InvalidAsmSym {
pub(crate) span: Span, pub(crate) span: Span,
} }
#[derive(Diagnostic)]
#[diag(resolve_lowercase_self)]
pub(crate) struct LowercaseSelf {
#[primary_span]
#[suggestion(code = "Self", applicability = "maybe-incorrect", style = "short")]
pub(crate) span: Span,
}
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(resolve_trait_impl_duplicate, code = "E0201")] #[diag(resolve_trait_impl_duplicate, code = "E0201")]
pub(crate) struct TraitImplDuplicate { pub(crate) struct TraitImplDuplicate {

View file

@ -15,8 +15,7 @@ use std::ptr;
use crate::errors::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst}; use crate::errors::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst};
use crate::late::{ use crate::late::{
ConstantHasGenerics, ConstantItemKind, HasGenericParams, NoConstantGenericsReason, PathSource, ConstantHasGenerics, HasGenericParams, NoConstantGenericsReason, PathSource, Rib, RibKind,
Rib, RibKind,
}; };
use crate::macros::{sub_namespace_match, MacroRulesScope}; use crate::macros::{sub_namespace_match, MacroRulesScope};
use crate::{errors, AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize}; use crate::{errors, AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
@ -1127,28 +1126,25 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
RibKind::ConstantItem(_, item) => { RibKind::ConstantItem(_, item) => {
// Still doesn't deal with upvars // Still doesn't deal with upvars
if let Some(span) = finalize { if let Some(span) = finalize {
let (span, resolution_error) = let (span, resolution_error) = match item {
if let Some((ident, constant_item_kind)) = item { None if rib_ident.as_str() == "self" => (span, LowercaseSelf),
let kind_str = match constant_item_kind { None => (
ConstantItemKind::Const => "const", rib_ident.span,
ConstantItemKind::Static => "static", AttemptToUseNonConstantValueInConstant(
}; original_rib_ident_def,
( "const",
span, "let",
AttemptToUseNonConstantValueInConstant( ),
ident, "let", kind_str, ),
), Some((ident, kind)) => (
) span,
} else { AttemptToUseNonConstantValueInConstant(
( ident,
rib_ident.span, "let",
AttemptToUseNonConstantValueInConstant( kind.as_str(),
original_rib_ident_def, ),
"const", ),
"let", };
),
)
};
self.report_error(span, resolution_error); self.report_error(span, resolution_error);
} }
return Res::Err; return Res::Err;

View file

@ -150,6 +150,15 @@ pub(crate) enum ConstantItemKind {
Static, Static,
} }
impl ConstantItemKind {
pub(crate) fn as_str(&self) -> &'static str {
match self {
Self::Const => "const",
Self::Static => "static",
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum RecordPartialRes { enum RecordPartialRes {
Yes, Yes,

View file

@ -251,6 +251,8 @@ enum ResolutionError<'a> {
TraitImplDuplicate { name: Symbol, trait_item_span: Span, old_span: Span }, TraitImplDuplicate { name: Symbol, trait_item_span: Span, old_span: Span },
/// Inline asm `sym` operand must refer to a `fn` or `static`. /// Inline asm `sym` operand must refer to a `fn` or `static`.
InvalidAsmSym, InvalidAsmSym,
/// `self` used instead of `Self` in a generic parameter
LowercaseSelf,
} }
enum VisResolutionError<'a> { enum VisResolutionError<'a> {

View file

@ -0,0 +1,8 @@
struct Foo;
impl Foo {
fn do_nothing(self: Box<self>) {} //~ ERROR attempt to use a non-constant value in a constant
//~^ HELP try using `Self`
}
fn main() {}

View file

@ -0,0 +1,8 @@
error: attempt to use a non-constant value in a constant
--> $DIR/explicit-self-lowercase-param.rs:4:29
|
LL | fn do_nothing(self: Box<self>) {}
| ^^^^ help: try using `Self`
error: aborting due to previous error