Skip to content

Commit f5cd2c5

Browse files
committed
Auto merge of #127117 - Urgau:non_local_def-syntactic, r=BoxyUwU
Rework `non_local_definitions` lint to only use a syntactic heuristic This PR reworks the `non_local_definitions` lint to only use a syntactic heuristic, i.e. not use a type-system logic for whenever an `impl` is local or not. Instead the new logic wanted by T-lang in #126768 (comment), which is to consider every paths in `Self` and `Trait` and to no longer use the type-system inference trick. `@rustbot` labels +L-non_local_definitions Fixes #126768
2 parents 35daf8b + 2423e9e commit f5cd2c5

31 files changed

+186
-956
lines changed

compiler/rustc_lint/messages.ftl

+1-10
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,7 @@ lint_non_local_definitions_cargo_update = the {$macro_kind} `{$macro_name}` may
592592
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>
593593
594594
lint_non_local_definitions_impl = non-local `impl` definition, `impl` blocks should be written at the same level as their item
595-
.remove_help = remove `{$may_remove_part}` to make the `impl` local
596-
.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`
597-
.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`
598-
.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
595+
.non_local = 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`
599596
.doctest = make this doc-test a standalone test with its own `fn main() {"{"} ... {"}"}`
600597
.exception = items in an anonymous const item (`const _: () = {"{"} ... {"}"}`) are treated as in the same scope as the anonymous const's declaration for the purpose of this lint
601598
.const_anon = use a const-anon item to suppress this lint
@@ -617,12 +614,6 @@ lint_non_local_definitions_macro_rules = non-local `macro_rules!` definition, `#
617614
remove the `#[macro_export]` or make this doc-test a standalone test with its own `fn main() {"{"} ... {"}"}`
618615
.non_local = a `macro_rules!` definition is non-local if it is nested inside an item and has a `#[macro_export]` attribute
619616
620-
lint_non_local_definitions_may_move = may need to be moved as well
621-
622-
lint_non_local_definitions_of_trait_not_local = `{$of_trait_str}` is not local
623-
624-
lint_non_local_definitions_self_ty_not_local = `{$self_ty_str}` is not local
625-
626617
lint_non_snake_case = {$sort} `{$name}` should have a snake case name
627618
.rename_or_convert_suggestion = rename the identifier or convert it to a snake case raw identifier
628619
.cannot_convert_note = `{$sc}` cannot be used as a raw identifier

compiler/rustc_lint/src/lints.rs

+1-37
Original file line numberDiff line numberDiff line change
@@ -1375,12 +1375,7 @@ pub(crate) enum NonLocalDefinitionsDiag {
13751375
body_name: String,
13761376
cargo_update: Option<NonLocalDefinitionsCargoUpdateNote>,
13771377
const_anon: Option<Option<Span>>,
1378-
move_to: Option<(Span, Vec<Span>)>,
13791378
doctest: bool,
1380-
may_remove: Option<(Span, String)>,
1381-
has_trait: bool,
1382-
self_ty_str: String,
1383-
of_trait_str: Option<String>,
13841379
macro_to_change: Option<(String, &'static str)>,
13851380
},
13861381
MacroRules {
@@ -1401,22 +1396,13 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
14011396
body_name,
14021397
cargo_update,
14031398
const_anon,
1404-
move_to,
14051399
doctest,
1406-
may_remove,
1407-
has_trait,
1408-
self_ty_str,
1409-
of_trait_str,
14101400
macro_to_change,
14111401
} => {
14121402
diag.primary_message(fluent::lint_non_local_definitions_impl);
14131403
diag.arg("depth", depth);
14141404
diag.arg("body_kind_descr", body_kind_descr);
14151405
diag.arg("body_name", body_name);
1416-
diag.arg("self_ty_str", self_ty_str);
1417-
if let Some(of_trait_str) = of_trait_str {
1418-
diag.arg("of_trait_str", of_trait_str);
1419-
}
14201406

14211407
if let Some((macro_to_change, macro_kind)) = macro_to_change {
14221408
diag.arg("macro_to_change", macro_to_change);
@@ -1427,34 +1413,12 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
14271413
diag.subdiagnostic(cargo_update);
14281414
}
14291415

1430-
if has_trait {
1431-
diag.note(fluent::lint_bounds);
1432-
diag.note(fluent::lint_with_trait);
1433-
} else {
1434-
diag.note(fluent::lint_without_trait);
1435-
}
1416+
diag.note(fluent::lint_non_local);
14361417

1437-
if let Some((move_help, may_move)) = move_to {
1438-
let mut ms = MultiSpan::from_span(move_help);
1439-
for sp in may_move {
1440-
ms.push_span_label(sp, fluent::lint_non_local_definitions_may_move);
1441-
}
1442-
diag.span_help(ms, fluent::lint_non_local_definitions_impl_move_help);
1443-
}
14441418
if doctest {
14451419
diag.help(fluent::lint_doctest);
14461420
}
14471421

1448-
if let Some((span, part)) = may_remove {
1449-
diag.arg("may_remove_part", part);
1450-
diag.span_suggestion(
1451-
span,
1452-
fluent::lint_remove_help,
1453-
"",
1454-
Applicability::MaybeIncorrect,
1455-
);
1456-
}
1457-
14581422
if let Some(const_anon) = const_anon {
14591423
diag.note(fluent::lint_exception);
14601424
if let Some(const_anon) = const_anon {

0 commit comments

Comments
 (0)