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

Rollup of 5 pull requests #125649

Merged
merged 23 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0d42cf7
Stabilise ip_bits feature
clarfonthey May 25, 2024
5ad4ad7
non_local_defs: move out from `#[derive(LintDiagnostic)]` to manual impl
Urgau May 13, 2024
06c6a2d
non_local_defs: switch to more friendly primary message
Urgau May 13, 2024
de1c122
non_local_defs: improve some notes around trait, bounds, consts
Urgau May 13, 2024
26b873d
non_local_defs: use span of the impl def and not the impl block
Urgau May 14, 2024
22095fb
non_local_defs: use labels to indicate what may need to be moved
Urgau May 14, 2024
402580b
non_local_defs: improve exception note for impl and macro_rules!
Urgau May 15, 2024
d3dfe14
non_local_defs: be more precise about what needs to be moved
Urgau May 15, 2024
ab23fd8
non_local_defs: improve main without a trait note
Urgau May 15, 2024
b719529
non_local_defs: suggest removing leading ref/ptr to make the impl local
Urgau May 15, 2024
98273ec
non_local_defs: point to Self and Trait to give more context
Urgau May 15, 2024
c7d3004
non_local_defs: point the parent item when appropriate
Urgau May 18, 2024
87599dd
add debug_assert to alias-relate
lcnr May 21, 2024
4d5a9bc
change selection test to run-pass
lcnr May 21, 2024
13ce229
refactor analyse visitor to instantiate states in order
lcnr May 21, 2024
98bfd54
eagerly normalize when adding goals
lcnr May 21, 2024
27cdc0d
Don't suggest turning non-char-literal exprs of ty `char` into string…
fmease May 28, 2024
402a649
update tracking issue for lazy_cell_consume
tspiteri May 28, 2024
8e89f83
Rollup merge of #125089 - Urgau:non_local_def-suggestions, r=estebank
workingjubilee May 28, 2024
fb95fda
Rollup merge of #125343 - lcnr:eagerly-normalize-added-goals, r=compi…
workingjubilee May 28, 2024
941bf8b
Rollup merge of #125551 - clarfonthey:ip-bits, r=jhpratt
workingjubilee May 28, 2024
01aa2e8
Rollup merge of #125640 - fmease:plz-no-stringify, r=estebank
workingjubilee May 28, 2024
4aaf9f6
Rollup merge of #125647 - tspiteri:track-lazy_cell_consume, r=working…
workingjubilee May 28, 2024
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
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4082,6 +4082,7 @@ dependencies = [
"rustc_feature",
"rustc_fluent_macro",
"rustc_hir",
"rustc_hir_pretty",
"rustc_index",
"rustc_infer",
"rustc_macros",
Expand Down
13 changes: 9 additions & 4 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2102,10 +2102,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
// If a string was expected and the found expression is a character literal,
// perhaps the user meant to write `"s"` to specify a string literal.
(ty::Ref(_, r, _), ty::Char) if r.is_str() => {
suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral {
start: span.with_hi(span.lo() + BytePos(1)),
end: span.with_lo(span.hi() - BytePos(1)),
})
if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span)
&& code.starts_with("'")
&& code.ends_with("'")
{
suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral {
start: span.with_hi(span.lo() + BytePos(1)),
end: span.with_lo(span.hi() - BytePos(1)),
});
}
}
// For code `if Some(..) = expr `, the type mismatch may be expected `bool` but found `()`,
// we try to suggest to add the missing `let` for `if let Some(..) = expr`
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ rustc_errors = { path = "../rustc_errors" }
rustc_feature = { path = "../rustc_feature" }
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
rustc_hir = { path = "../rustc_hir" }
rustc_hir_pretty = { path = "../rustc_hir_pretty" }
rustc_index = { path = "../rustc_index" }
rustc_infer = { path = "../rustc_infer" }
rustc_macros = { path = "../rustc_macros" }
Expand Down
29 changes: 19 additions & 10 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -542,17 +542,21 @@ lint_non_local_definitions_cargo_update = the {$macro_kind} `{$macro_name}` may

lint_non_local_definitions_deprecation = this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>

lint_non_local_definitions_impl = non-local `impl` definition, they should be avoided as they go against expectation
.help =
move this `impl` block outside the of the current {$body_kind_descr} {$depth ->
[one] `{$body_name}`
*[other] `{$body_name}` and up {$depth} bodies
}
.non_local = an `impl` definition is non-local if it is nested inside an item and may impact type checking outside of that item. This can be the case if neither the trait or the self type are at the same nesting level as the `impl`
.exception = one exception to the rule are anon-const (`const _: () = {"{"} ... {"}"}`) at top-level module and anon-const at the same nesting as the trait or type
lint_non_local_definitions_impl = non-local `impl` definition, `impl` blocks should be written at the same level as their item
.remove_help = remove `{$may_remove_part}` to make the `impl` local
.without_trait = methods and associated constants are still usable outside the current expression, only `impl Local` and `impl dyn Local` can ever be private, and only if the type is nested in the same item as the `impl`
.with_trait = an `impl` is never scoped, even when it is nested inside an item, as it may impact type checking outside of that item, which can be the case if neither the trait or the self type are at the same nesting level as the `impl`
.bounds = `impl` may be usable in bounds, etc. from outside the expression, which might e.g. make something constructible that previously wasn't, because it's still on a publicly-visible type
.exception = items in an anonymous const item (`const _: () = {"{"} ... {"}"}`) are treated as in the same scope as the anonymous const's declaration
.const_anon = use a const-anon item to suppress this lint

lint_non_local_definitions_macro_rules = non-local `macro_rules!` definition, they should be avoided as they go against expectation
lint_non_local_definitions_impl_move_help =
move the `impl` block outside of this {$body_kind_descr} {$depth ->
[one] `{$body_name}`
*[other] `{$body_name}` and up {$depth} bodies
}

lint_non_local_definitions_macro_rules = non-local `macro_rules!` definition, `#[macro_export]` macro should be written at top level module
.help =
remove the `#[macro_export]` or move this `macro_rules!` outside the of the current {$body_kind_descr} {$depth ->
[one] `{$body_name}`
Expand All @@ -561,7 +565,12 @@ lint_non_local_definitions_macro_rules = non-local `macro_rules!` definition, th
.help_doctest =
remove the `#[macro_export]` or make this doc-test a standalone test with its own `fn main() {"{"} ... {"}"}`
.non_local = a `macro_rules!` definition is non-local if it is nested inside an item and has a `#[macro_export]` attribute
.exception = one exception to the rule are anon-const (`const _: () = {"{"} ... {"}"}`) at top-level module

lint_non_local_definitions_may_move = may need to be moved as well

lint_non_local_definitions_of_trait_not_local = `{$of_trait_str}` is not local

lint_non_local_definitions_self_ty_not_local = `{$self_ty_str}` is not local

lint_non_snake_case = {$sort} `{$name}` should have a snake case name
.rename_or_convert_suggestion = rename the identifier or convert it to a snake case raw identifier
Expand Down
122 changes: 104 additions & 18 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::errors::RequestedLevel;
use crate::fluent_generated as fluent;
use rustc_errors::{
codes::*, Applicability, Diag, DiagArgValue, DiagMessage, DiagStyledString,
ElidedLifetimeInPathSubdiag, EmissionGuarantee, LintDiagnostic, SubdiagMessageOp,
ElidedLifetimeInPathSubdiag, EmissionGuarantee, LintDiagnostic, MultiSpan, SubdiagMessageOp,
Subdiagnostic, SuggestionStyle,
};
use rustc_hir::{def::Namespace, def_id::DefId};
Expand Down Expand Up @@ -1329,40 +1329,126 @@ pub struct SuspiciousDoubleRefCloneDiag<'a> {
}

// non_local_defs.rs
#[derive(LintDiagnostic)]
pub enum NonLocalDefinitionsDiag {
#[diag(lint_non_local_definitions_impl)]
#[help]
#[note(lint_non_local)]
#[note(lint_exception)]
#[note(lint_non_local_definitions_deprecation)]
Impl {
depth: u32,
body_kind_descr: &'static str,
body_name: String,
#[subdiagnostic]
cargo_update: Option<NonLocalDefinitionsCargoUpdateNote>,
#[suggestion(lint_const_anon, code = "_", applicability = "machine-applicable")]
const_anon: Option<Span>,
const_anon: Option<Option<Span>>,
move_to: Option<(Span, Vec<Span>)>,
may_remove: Option<(Span, String)>,
has_trait: bool,
self_ty_str: String,
of_trait_str: Option<String>,
},
#[diag(lint_non_local_definitions_macro_rules)]
MacroRules {
depth: u32,
body_kind_descr: &'static str,
body_name: String,
#[help]
help: Option<()>,
#[help(lint_help_doctest)]
doctest_help: Option<()>,
#[note(lint_non_local)]
#[note(lint_exception)]
#[note(lint_non_local_definitions_deprecation)]
notes: (),
#[subdiagnostic]
cargo_update: Option<NonLocalDefinitionsCargoUpdateNote>,
},
}

impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, ()>) {
match self {
NonLocalDefinitionsDiag::Impl {
depth,
body_kind_descr,
body_name,
cargo_update,
const_anon,
move_to,
may_remove,
has_trait,
self_ty_str,
of_trait_str,
} => {
diag.primary_message(fluent::lint_non_local_definitions_impl);
diag.arg("depth", depth);
diag.arg("body_kind_descr", body_kind_descr);
diag.arg("body_name", body_name);
diag.arg("self_ty_str", self_ty_str);
if let Some(of_trait_str) = of_trait_str {
diag.arg("of_trait_str", of_trait_str);
}

if has_trait {
diag.note(fluent::lint_bounds);
diag.note(fluent::lint_with_trait);
} else {
diag.note(fluent::lint_without_trait);
}

if let Some((move_help, may_move)) = move_to {
let mut ms = MultiSpan::from_span(move_help);
for sp in may_move {
ms.push_span_label(sp, fluent::lint_non_local_definitions_may_move);
}
diag.span_help(ms, fluent::lint_non_local_definitions_impl_move_help);
}

if let Some((span, part)) = may_remove {
diag.arg("may_remove_part", part);
diag.span_suggestion(
span,
fluent::lint_remove_help,
"",
Applicability::MaybeIncorrect,
);
}

if let Some(cargo_update) = cargo_update {
diag.subdiagnostic(&diag.dcx, cargo_update);
}
if let Some(const_anon) = const_anon {
diag.note(fluent::lint_exception);
if let Some(const_anon) = const_anon {
diag.span_suggestion(
const_anon,
fluent::lint_const_anon,
"_",
Applicability::MachineApplicable,
);
}
}

diag.note(fluent::lint_non_local_definitions_deprecation);
}
NonLocalDefinitionsDiag::MacroRules {
depth,
body_kind_descr,
body_name,
help,
doctest_help,
cargo_update,
} => {
diag.primary_message(fluent::lint_non_local_definitions_macro_rules);
diag.arg("depth", depth);
diag.arg("body_kind_descr", body_kind_descr);
diag.arg("body_name", body_name);

if let Some(()) = help {
diag.help(fluent::lint_help);
}
if let Some(()) = doctest_help {
diag.help(fluent::lint_help_doctest);
}

diag.note(fluent::lint_non_local);
diag.note(fluent::lint_non_local_definitions_deprecation);

if let Some(cargo_update) = cargo_update {
diag.subdiagnostic(&diag.dcx, cargo_update);
}
}
}
}
}

#[derive(Subdiagnostic)]
#[note(lint_non_local_definitions_cargo_update)]
pub struct NonLocalDefinitionsCargoUpdateNote {
Expand Down
Loading
Loading