Skip to content

Commit 6641b49

Browse files
Rollup merge of #111460 - clubby789:lowercase-box-self, r=compiler-errors
Improve suggestion for `self: Box<self>` Fixes #110642
2 parents eead6f4 + 3851a4b commit 6641b49

File tree

9 files changed

+63
-25
lines changed

9 files changed

+63
-25
lines changed

compiler/rustc_middle/src/ty/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ pub enum ExplicitSelf<'tcx> {
12531253

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

compiler/rustc_resolve/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ resolve_invalid_asm_sym =
199199
.label = is a local variable
200200
.help = `sym` operands must refer to either a function or a static
201201
202+
resolve_lowercase_self =
203+
attempt to use a non-constant value in a constant
204+
.suggestion = try using `Self`
205+
202206
resolve_trait_impl_duplicate =
203207
duplicate definitions with name `{$name}`:
204208
.label = duplicate definition

compiler/rustc_resolve/src/diagnostics.rs

+3
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
948948
ResolutionError::InvalidAsmSym => {
949949
self.tcx.sess.create_err(errs::InvalidAsmSym { span })
950950
}
951+
ResolutionError::LowercaseSelf => {
952+
self.tcx.sess.create_err(errs::LowercaseSelf { span })
953+
}
951954
}
952955
}
953956

compiler/rustc_resolve/src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,14 @@ pub(crate) struct InvalidAsmSym {
442442
pub(crate) span: Span,
443443
}
444444

445+
#[derive(Diagnostic)]
446+
#[diag(resolve_lowercase_self)]
447+
pub(crate) struct LowercaseSelf {
448+
#[primary_span]
449+
#[suggestion(code = "Self", applicability = "maybe-incorrect", style = "short")]
450+
pub(crate) span: Span,
451+
}
452+
445453
#[derive(Diagnostic)]
446454
#[diag(resolve_trait_impl_duplicate, code = "E0201")]
447455
pub(crate) struct TraitImplDuplicate {

compiler/rustc_resolve/src/ident.rs

+20-24
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ use std::ptr;
1515

1616
use crate::errors::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst};
1717
use crate::late::{
18-
ConstantHasGenerics, ConstantItemKind, HasGenericParams, NoConstantGenericsReason, PathSource,
19-
Rib, RibKind,
18+
ConstantHasGenerics, HasGenericParams, NoConstantGenericsReason, PathSource, Rib, RibKind,
2019
};
2120
use crate::macros::{sub_namespace_match, MacroRulesScope};
2221
use crate::{errors, AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
@@ -1127,28 +1126,25 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11271126
RibKind::ConstantItem(_, item) => {
11281127
// Still doesn't deal with upvars
11291128
if let Some(span) = finalize {
1130-
let (span, resolution_error) =
1131-
if let Some((ident, constant_item_kind)) = item {
1132-
let kind_str = match constant_item_kind {
1133-
ConstantItemKind::Const => "const",
1134-
ConstantItemKind::Static => "static",
1135-
};
1136-
(
1137-
span,
1138-
AttemptToUseNonConstantValueInConstant(
1139-
ident, "let", kind_str,
1140-
),
1141-
)
1142-
} else {
1143-
(
1144-
rib_ident.span,
1145-
AttemptToUseNonConstantValueInConstant(
1146-
original_rib_ident_def,
1147-
"const",
1148-
"let",
1149-
),
1150-
)
1151-
};
1129+
let (span, resolution_error) = match item {
1130+
None if rib_ident.as_str() == "self" => (span, LowercaseSelf),
1131+
None => (
1132+
rib_ident.span,
1133+
AttemptToUseNonConstantValueInConstant(
1134+
original_rib_ident_def,
1135+
"const",
1136+
"let",
1137+
),
1138+
),
1139+
Some((ident, kind)) => (
1140+
span,
1141+
AttemptToUseNonConstantValueInConstant(
1142+
ident,
1143+
"let",
1144+
kind.as_str(),
1145+
),
1146+
),
1147+
};
11521148
self.report_error(span, resolution_error);
11531149
}
11541150
return Res::Err;

compiler/rustc_resolve/src/late.rs

+9
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ pub(crate) enum ConstantItemKind {
150150
Static,
151151
}
152152

153+
impl ConstantItemKind {
154+
pub(crate) fn as_str(&self) -> &'static str {
155+
match self {
156+
Self::Const => "const",
157+
Self::Static => "static",
158+
}
159+
}
160+
}
161+
153162
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
154163
enum RecordPartialRes {
155164
Yes,

compiler/rustc_resolve/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ enum ResolutionError<'a> {
251251
TraitImplDuplicate { name: Symbol, trait_item_span: Span, old_span: Span },
252252
/// Inline asm `sym` operand must refer to a `fn` or `static`.
253253
InvalidAsmSym,
254+
/// `self` used instead of `Self` in a generic parameter
255+
LowercaseSelf,
254256
}
255257

256258
enum VisResolutionError<'a> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
struct Foo;
2+
3+
impl Foo {
4+
fn do_nothing(self: Box<self>) {} //~ ERROR attempt to use a non-constant value in a constant
5+
//~^ HELP try using `Self`
6+
}
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: attempt to use a non-constant value in a constant
2+
--> $DIR/explicit-self-lowercase-param.rs:4:29
3+
|
4+
LL | fn do_nothing(self: Box<self>) {}
5+
| ^^^^ help: try using `Self`
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)