Skip to content

Commit 1910413

Browse files
committed
Point to every relevant types in the main diag
1 parent 8a584d6 commit 1910413

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
@@ -569,10 +569,6 @@ lint_non_local_definitions_macro_rules = non-local `macro_rules!` definition, `#
569569
remove the `#[macro_export]` or make this doc-test a standalone test with its own `fn main() {"{"} ... {"}"}`
570570
.non_local = a `macro_rules!` definition is non-local if it is nested inside an item and has a `#[macro_export]` attribute
571571
572-
lint_non_local_definitions_of_trait_not_local = `{$of_trait_str}` is not local
573-
574-
lint_non_local_definitions_self_ty_not_local = `{$self_ty_str}` is not local
575-
576572
lint_non_snake_case = {$sort} `{$name}` should have a snake case name
577573
.rename_or_convert_suggestion = rename the identifier or convert it to a snake case raw identifier
578574
.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
@@ -1359,8 +1359,6 @@ pub enum NonLocalDefinitionsDiag {
13591359
const_anon: Option<Option<Span>>,
13601360
doctest: bool,
13611361
has_trait: bool,
1362-
self_ty_str: String,
1363-
of_trait_str: Option<String>,
13641362
macro_to_change: Option<(String, &'static str)>,
13651363
},
13661364
MacroRules {
@@ -1383,18 +1381,12 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
13831381
const_anon,
13841382
doctest,
13851383
has_trait,
1386-
self_ty_str,
1387-
of_trait_str,
13881384
macro_to_change,
13891385
} => {
13901386
diag.primary_message(fluent::lint_non_local_definitions_impl);
13911387
diag.arg("depth", depth);
13921388
diag.arg("body_kind_descr", body_kind_descr);
13931389
diag.arg("body_name", body_name);
1394-
diag.arg("self_ty_str", self_ty_str);
1395-
if let Some(of_trait_str) = of_trait_str {
1396-
diag.arg("of_trait_str", of_trait_str);
1397-
}
13981390

13991391
if let Some((macro_to_change, macro_kind)) = macro_to_change {
14001392
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,13 +1,13 @@
11
use rustc_errors::MultiSpan;
2+
use rustc_hir::def::{DefKind, Res};
23
use rustc_hir::intravisit::{self, Visitor};
34
use rustc_hir::HirId;
4-
use rustc_hir::{def::DefKind, Body, Item, ItemKind, Node, TyKind};
5-
use rustc_hir::{Path, QPath};
5+
use rustc_hir::{Body, Item, ItemKind, Node, Path, TyKind};
66
use rustc_middle::ty::TyCtxt;
77
use rustc_session::{declare_lint, impl_lint_pass};
88
use rustc_span::def_id::{DefId, LOCAL_CRATE};
99
use rustc_span::Span;
10-
use rustc_span::{sym, symbol::kw, ExpnKind, MacroKind, Symbol};
10+
use rustc_span::{sym, symbol::kw, ExpnKind, MacroKind};
1111

1212
use crate::fluent_generated as fluent;
1313
use crate::lints::{NonLocalDefinitionsCargoUpdateNote, NonLocalDefinitionsDiag};
@@ -144,6 +144,12 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
144144
collector.visit_trait_ref(of_trait);
145145
}
146146

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

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

197196
let doctest = is_at_toplevel_doctest();
198197

@@ -221,8 +220,6 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
221220
.unwrap_or_else(|| "<unnameable>".to_string()),
222221
cargo_update: cargo_update(),
223222
const_anon,
224-
self_ty_str,
225-
of_trait_str,
226223
doctest,
227224
has_trait: impl_.of_trait.is_some(),
228225
macro_to_change,
@@ -318,38 +315,3 @@ fn path_span_without_args(path: &Path<'_>) -> Span {
318315
fn path_name_to_string(path: &Path<'_>) -> String {
319316
path.segments.last().unwrap().ident.name.to_ident_string()
320317
}
321-
322-
/// Compute the `Span` and visual representation for the `Self` we want to point at;
323-
/// It follows part of the actual logic of non-local, and if possible return the least
324-
/// amount possible for the span and representation.
325-
fn self_ty_kind_for_diagnostic(ty: &rustc_hir::Ty<'_>, tcx: TyCtxt<'_>) -> (Span, String) {
326-
match ty.kind {
327-
TyKind::Path(QPath::Resolved(_, ty_path)) => (
328-
path_span_without_args(ty_path),
329-
ty_path
330-
.res
331-
.opt_def_id()
332-
.map(|did| tcx.opt_item_name(did))
333-
.flatten()
334-
.as_ref()
335-
.map(|s| Symbol::as_str(s))
336-
.unwrap_or("<unnameable>")
337-
.to_string(),
338-
),
339-
TyKind::TraitObject([principle_poly_trait_ref, ..], _, _) => {
340-
let path = &principle_poly_trait_ref.trait_ref.path;
341-
(
342-
path_span_without_args(path),
343-
path.res
344-
.opt_def_id()
345-
.map(|did| tcx.opt_item_name(did))
346-
.flatten()
347-
.as_ref()
348-
.map(|s| Symbol::as_str(s))
349-
.unwrap_or("<unnameable>")
350-
.to_string(),
351-
)
352-
}
353-
_ => (ty.span, rustc_hir_pretty::ty_to_string(&tcx, ty)),
354-
}
355-
}

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)