Skip to content

Commit 0f665e2

Browse files
committed
Point to every relevant types in the main diag
1 parent 00a6ebf commit 0f665e2

File tree

9 files changed

+76
-124
lines changed

9 files changed

+76
-124
lines changed

compiler/rustc_lint/messages.ftl

-4
Original file line numberDiff line numberDiff line change
@@ -616,10 +616,6 @@ lint_non_local_definitions_macro_rules = non-local `macro_rules!` definition, `#
616616
remove the `#[macro_export]` or make this doc-test a standalone test with its own `fn main() {"{"} ... {"}"}`
617617
.non_local = a `macro_rules!` definition is non-local if it is nested inside an item and has a `#[macro_export]` attribute
618618
619-
lint_non_local_definitions_of_trait_not_local = `{$of_trait_str}` is not local
620-
621-
lint_non_local_definitions_self_ty_not_local = `{$self_ty_str}` is not local
622-
623619
lint_non_snake_case = {$sort} `{$name}` should have a snake case name
624620
.rename_or_convert_suggestion = rename the identifier or convert it to a snake case raw identifier
625621
.cannot_convert_note = `{$sc}` cannot be used as a raw identifier

compiler/rustc_lint/src/lints.rs

-8
Original file line numberDiff line numberDiff line change
@@ -1377,8 +1377,6 @@ pub(crate) enum NonLocalDefinitionsDiag {
13771377
const_anon: Option<Option<Span>>,
13781378
doctest: bool,
13791379
has_trait: bool,
1380-
self_ty_str: String,
1381-
of_trait_str: Option<String>,
13821380
macro_to_change: Option<(String, &'static str)>,
13831381
},
13841382
MacroRules {
@@ -1401,18 +1399,12 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
14011399
const_anon,
14021400
doctest,
14031401
has_trait,
1404-
self_ty_str,
1405-
of_trait_str,
14061402
macro_to_change,
14071403
} => {
14081404
diag.primary_message(fluent::lint_non_local_definitions_impl);
14091405
diag.arg("depth", depth);
14101406
diag.arg("body_kind_descr", body_kind_descr);
14111407
diag.arg("body_name", body_name);
1412-
diag.arg("self_ty_str", self_ty_str);
1413-
if let Some(of_trait_str) = of_trait_str {
1414-
diag.arg("of_trait_str", of_trait_str);
1415-
}
14161408

14171409
if let Some((macro_to_change, macro_kind)) = macro_to_change {
14181410
diag.arg("macro_to_change", macro_to_change);

compiler/rustc_lint/src/non_local_def.rs

+17-55
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use rustc_errors::MultiSpan;
2-
use rustc_hir::def::DefKind;
2+
use rustc_hir::def::{DefKind, Res};
33
use rustc_hir::intravisit::{self, Visitor};
4-
use rustc_hir::{Body, HirId, Item, ItemKind, Node, Path, QPath, TyKind};
4+
use rustc_hir::{Body, HirId, Item, ItemKind, Node, Path, TyKind};
55
use rustc_middle::ty::TyCtxt;
66
use rustc_session::{declare_lint, impl_lint_pass};
77
use rustc_span::def_id::{DefId, LOCAL_CRATE};
88
use rustc_span::symbol::kw;
9-
use rustc_span::{ExpnKind, MacroKind, Span, Symbol, sym, sym};
9+
use rustc_span::{ExpnKind, MacroKind, Span, sym};
1010

1111
use crate::lints::{NonLocalDefinitionsCargoUpdateNote, NonLocalDefinitionsDiag};
1212
use crate::{LateContext, LateLintPass, LintContext, fluent_generated as fluent};
@@ -142,6 +142,12 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
142142
collector.visit_trait_ref(of_trait);
143143
}
144144

145+
// 1.5. Remove any path that doesn't resolve to a `DefId` or if it resolve to a
146+
// type-param (e.g. `T`).
147+
collector.paths.retain(
148+
|p| matches!(p.res, Res::Def(def_kind, _) if def_kind != DefKind::TyParam),
149+
);
150+
145151
// 2. We check if any of path reference a "local" parent and if that the case
146152
// we bail out as asked by T-lang, even though this isn't correct from a
147153
// type-system point of view, as inference exists and could still leak the impl.
@@ -174,23 +180,16 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
174180
let impl_span = item.span.shrink_to_lo().to(impl_.self_ty.span);
175181
let mut ms = MultiSpan::from_span(impl_span);
176182

177-
let (self_ty_span, self_ty_str) =
178-
self_ty_kind_for_diagnostic(&impl_.self_ty, cx.tcx);
179-
180-
ms.push_span_label(
181-
self_ty_span,
182-
fluent::lint_non_local_definitions_self_ty_not_local,
183-
);
184-
185-
let of_trait_str = if let Some(of_trait) = &impl_.of_trait {
183+
for path in &collector.paths {
184+
// FIXME: While a translatable diagnostic message can have an argument
185+
// we (currently) have no way to set different args per diag msg with
186+
// `MultiSpan::push_span_label`.
187+
#[allow(rustc::untranslatable_diagnostic)]
186188
ms.push_span_label(
187-
path_span_without_args(&of_trait.path),
188-
fluent::lint_non_local_definitions_of_trait_not_local,
189+
path_span_without_args(path),
190+
format!("`{}` is not local", path_name_to_string(path)),
189191
);
190-
Some(path_name_to_string(&of_trait.path))
191-
} else {
192-
None
193-
};
192+
}
194193

195194
let doctest = is_at_toplevel_doctest();
196195

@@ -216,8 +215,6 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
216215
.unwrap_or_else(|| "<unnameable>".to_string()),
217216
cargo_update: cargo_update(),
218217
const_anon,
219-
self_ty_str,
220-
of_trait_str,
221218
doctest,
222219
has_trait: impl_.of_trait.is_some(),
223220
macro_to_change,
@@ -312,38 +309,3 @@ fn path_span_without_args(path: &Path<'_>) -> Span {
312309
fn path_name_to_string(path: &Path<'_>) -> String {
313310
path.segments.last().unwrap().ident.name.to_ident_string()
314311
}
315-
316-
/// Compute the `Span` and visual representation for the `Self` we want to point at;
317-
/// It follows part of the actual logic of non-local, and if possible return the least
318-
/// amount possible for the span and representation.
319-
fn self_ty_kind_for_diagnostic(ty: &rustc_hir::Ty<'_>, tcx: TyCtxt<'_>) -> (Span, String) {
320-
match ty.kind {
321-
TyKind::Path(QPath::Resolved(_, ty_path)) => (
322-
path_span_without_args(ty_path),
323-
ty_path
324-
.res
325-
.opt_def_id()
326-
.map(|did| tcx.opt_item_name(did))
327-
.flatten()
328-
.as_ref()
329-
.map(|s| Symbol::as_str(s))
330-
.unwrap_or("<unnameable>")
331-
.to_string(),
332-
),
333-
TyKind::TraitObject([principle_poly_trait_ref, ..], _, _) => {
334-
let path = &principle_poly_trait_ref.0.trait_ref.path;
335-
(
336-
path_span_without_args(path),
337-
path.res
338-
.opt_def_id()
339-
.map(|did| tcx.opt_item_name(did))
340-
.flatten()
341-
.as_ref()
342-
.map(|s| Symbol::as_str(s))
343-
.unwrap_or("<unnameable>")
344-
.to_string(),
345-
)
346-
}
347-
_ => (ty.span, rustc_hir_pretty::ty_to_string(&tcx, ty)),
348-
}
349-
}

tests/ui/lint/non-local-defs/consts.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ LL | const Z: () = {
88
| move the `impl` block outside of this constant `Z`
99
...
1010
LL | impl Uto for &Test {}
11-
| ^^^^^---^^^^^-----
12-
| | |
13-
| | `&'_ Test` is not local
11+
| ^^^^^---^^^^^^----
12+
| | |
13+
| | `Test` is not local
1414
| `Uto` is not local
1515
|
1616
= note: `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

tests/ui/lint/non-local-defs/exhaustive-trait.stderr

+17-15
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ LL | fn main() {
2121
| --------- move the `impl` block outside of this function `main`
2222
...
2323
LL | impl PartialEq<()> for &Dog {
24-
| ^^^^^---------^^^^^^^^^----
25-
| | |
26-
| | `&'_ Dog` is not local
24+
| ^^^^^---------^^^^^^^^^^---
25+
| | |
26+
| | `Dog` is not local
2727
| `PartialEq` is not local
2828
|
2929
= note: `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
@@ -37,9 +37,9 @@ LL | fn main() {
3737
| --------- move the `impl` block outside of this function `main`
3838
...
3939
LL | impl PartialEq<Dog> for () {
40-
| ^^^^^---------^^^^^^^^^^--
41-
| | |
42-
| | `()` is not local
40+
| ^^^^^---------^---^^^^^^^^
41+
| | |
42+
| | `Dog` is not local
4343
| `PartialEq` is not local
4444
|
4545
= note: `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
@@ -53,9 +53,9 @@ LL | fn main() {
5353
| --------- move the `impl` block outside of this function `main`
5454
...
5555
LL | impl PartialEq<&Dog> for () {
56-
| ^^^^^---------^^^^^^^^^^^--
57-
| | |
58-
| | `()` is not local
56+
| ^^^^^---------^^---^^^^^^^^
57+
| | |
58+
| | `Dog` is not local
5959
| `PartialEq` is not local
6060
|
6161
= note: `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
@@ -69,9 +69,10 @@ LL | fn main() {
6969
| --------- move the `impl` block outside of this function `main`
7070
...
7171
LL | impl PartialEq<Dog> for &Dog {
72-
| ^^^^^---------^^^^^^^^^^----
73-
| | |
74-
| | `&'_ Dog` is not local
72+
| ^^^^^---------^---^^^^^^^---
73+
| | | |
74+
| | | `Dog` is not local
75+
| | `Dog` is not local
7576
| `PartialEq` is not local
7677
|
7778
= note: `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
@@ -85,9 +86,10 @@ LL | fn main() {
8586
| --------- move the `impl` block outside of this function `main`
8687
...
8788
LL | impl PartialEq<&Dog> for &Dog {
88-
| ^^^^^---------^^^^^^^^^^^----
89-
| | |
90-
| | `&'_ Dog` is not local
89+
| ^^^^^---------^^---^^^^^^^---
90+
| | | |
91+
| | | `Dog` is not local
92+
| | `Dog` is not local
9193
| `PartialEq` is not local
9294
|
9395
= note: `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

tests/ui/lint/non-local-defs/exhaustive.stderr

+21-21
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ LL | fn main() {
6565
| --------- move the `impl` block outside of this function `main`
6666
...
6767
LL | impl Trait for &dyn Trait {}
68-
| ^^^^^-----^^^^^----------
69-
| | |
70-
| | `&'_ dyn Trait` is not local
68+
| ^^^^^-----^^^^^^^^^^-----
69+
| | |
70+
| | `Trait` is not local
7171
| `Trait` is not local
7272
|
7373
= note: `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
@@ -81,9 +81,9 @@ LL | fn main() {
8181
| --------- move the `impl` block outside of this function `main`
8282
...
8383
LL | impl Trait for *mut Test {}
84-
| ^^^^^-----^^^^^---------
85-
| | |
86-
| | `*mut Test` is not local
84+
| ^^^^^-----^^^^^^^^^^----
85+
| | |
86+
| | `Test` is not local
8787
| `Trait` is not local
8888
|
8989
= note: `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
@@ -97,9 +97,9 @@ LL | fn main() {
9797
| --------- move the `impl` block outside of this function `main`
9898
...
9999
LL | impl Trait for *mut [Test] {}
100-
| ^^^^^-----^^^^^-----------
101-
| | |
102-
| | `*mut [Test]` is not local
100+
| ^^^^^-----^^^^^^^^^^^----^
101+
| | |
102+
| | `Test` is not local
103103
| `Trait` is not local
104104
|
105105
= note: `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
@@ -113,9 +113,9 @@ LL | fn main() {
113113
| --------- move the `impl` block outside of this function `main`
114114
...
115115
LL | impl Trait for [Test; 8] {}
116-
| ^^^^^-----^^^^^---------
117-
| | |
118-
| | `[Test; 8]` is not local
116+
| ^^^^^-----^^^^^^----^^^^
117+
| | |
118+
| | `Test` is not local
119119
| `Trait` is not local
120120
|
121121
= note: `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
@@ -129,9 +129,9 @@ LL | fn main() {
129129
| --------- move the `impl` block outside of this function `main`
130130
...
131131
LL | impl Trait for (Test,) {}
132-
| ^^^^^-----^^^^^-------
133-
| | |
134-
| | `(Test,)` is not local
132+
| ^^^^^-----^^^^^^----^^
133+
| | |
134+
| | `Test` is not local
135135
| `Trait` is not local
136136
|
137137
= note: `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
@@ -145,9 +145,9 @@ LL | fn main() {
145145
| --------- move the `impl` block outside of this function `main`
146146
...
147147
LL | impl Trait for fn(Test) -> () {}
148-
| ^^^^^-----^^^^^--------------
149-
| | |
150-
| | `fn(: Test) -> ()` is not local
148+
| ^^^^^-----^^^^^^^^----^^^^^^^
149+
| | |
150+
| | `Test` is not local
151151
| `Trait` is not local
152152
|
153153
= note: `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
@@ -161,9 +161,9 @@ LL | fn main() {
161161
| --------- move the `impl` block outside of this function `main`
162162
...
163163
LL | impl Trait for fn() -> Test {}
164-
| ^^^^^-----^^^^^------------
165-
| | |
166-
| | `fn() -> Test` is not local
164+
| ^^^^^-----^^^^^^^^^^^^^----
165+
| | |
166+
| | `Test` is not local
167167
| `Trait` is not local
168168
|
169169
= note: `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

tests/ui/lint/non-local-defs/from-local-for-global.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ warning: non-local `impl` definition, `impl` blocks should be written at the sam
44
LL | fn main() {
55
| --------- move the `impl` block outside of this function `main`
66
LL | impl From<Cat> for () {
7-
| ^^^^^----^^^^^^^^^^--
8-
| | |
9-
| | `()` is not local
7+
| ^^^^^----^---^^^^^^^^
8+
| | |
9+
| | `Cat` is not local
1010
| `From` is not local
1111
|
1212
= note: `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

tests/ui/lint/non-local-defs/generics.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ LL | fn bad() {
3838
| -------- move the `impl` block outside of this function `bad`
3939
...
4040
LL | impl<T> Uto8 for T {}
41-
| ^^^^^^^^----^^^^^-
42-
| | |
43-
| | `T` is not local
41+
| ^^^^^^^^----^^^^^^
42+
| |
4443
| `Uto8` is not local
4544
|
4645
= note: `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

tests/ui/lint/non-local-defs/weird-exprs.stderr

+13-12
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ warning: non-local `impl` definition, `impl` blocks should be written at the sam
44
LL | type A = [u32; {
55
| ________________-
66
LL | | impl Uto for *mut Test {}
7-
| | ^^^^^---^^^^^---------
8-
| | | |
9-
| | | `*mut Test` is not local
7+
| | ^^^^^---^^^^^^^^^^----
8+
| | | |
9+
| | | `Test` is not local
1010
| | `Uto` is not local
1111
LL | |
1212
... |
@@ -62,9 +62,9 @@ warning: non-local `impl` definition, `impl` blocks should be written at the sam
6262
LL | type A = [u32; {
6363
| ____________________-
6464
LL | | impl Uto for &Test {}
65-
| | ^^^^^---^^^^^-----
66-
| | | |
67-
| | | `&'_ Test` is not local
65+
| | ^^^^^---^^^^^^----
66+
| | | |
67+
| | | `Test` is not local
6868
| | `Uto` is not local
6969
LL | |
7070
... |
@@ -81,9 +81,9 @@ warning: non-local `impl` definition, `impl` blocks should be written at the sam
8181
LL | fn a(_: [u32; {
8282
| ___________________-
8383
LL | | impl Uto for &(Test,) {}
84-
| | ^^^^^---^^^^^--------
85-
| | | |
86-
| | | `&'_ (Test,)` is not local
84+
| | ^^^^^---^^^^^^^----^^
85+
| | | |
86+
| | | `Test` is not local
8787
| | `Uto` is not local
8888
LL | |
8989
... |
@@ -100,9 +100,10 @@ warning: non-local `impl` definition, `impl` blocks should be written at the sam
100100
LL | fn b() -> [u32; {
101101
| _____________________-
102102
LL | | impl Uto for &(Test,Test) {}
103-
| | ^^^^^---^^^^^------------
104-
| | | |
105-
| | | `&'_ (Test, Test)` is not local
103+
| | ^^^^^---^^^^^^^----^----^
104+
| | | | |
105+
| | | | `Test` is not local
106+
| | | `Test` is not local
106107
| | `Uto` is not local
107108
LL | |
108109
... |

0 commit comments

Comments
 (0)