Skip to content

Commit b8ce648

Browse files
committed
Scope Prints In Diagnostics - Draft
1 parent 8546a7a commit b8ce648

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

Diff for: text/0000-scope-prints-in-diagnostics.md

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
- Feature Name: Scope Prints In Diagnostics
2+
- Start Date: -
3+
- RFC PR:
4+
- Rust Issue:
5+
6+
# Summary
7+
[summary]: #summary
8+
9+
The diagnostics messages emitted by the Rust compiler make a very good effort
10+
for quoting relevent pieces of code when emitting about warnings and errors.
11+
Attached to these are annotated fragments of code, with filenames and line
12+
numbers.
13+
14+
However, the context in which the fragments reside is not mentioned, i.e.
15+
whether a function or another item. This proposal calls for adding that as
16+
well.
17+
18+
19+
# Motivation
20+
[motivation]: #motivation
21+
22+
In large source files where the number of lines goes to the thousands, it is
23+
easy to lose track. If the code is slightly repetitive, the annotations may not
24+
be enough to provide proper context.
25+
26+
Some developers use development environments where they look at warnings or
27+
errors emitted in console and only then go to the source. Having the context
28+
appear with the diagonstics may be beneficial to them.
29+
30+
31+
# Guide-level explanation
32+
[guide-level-explanation]: #guide-level-explanation
33+
34+
The changes proposed are two-fold:
35+
36+
### Item path next to line number
37+
38+
For each source position on which we already mention file name, line number and
39+
column, an item path is printed along with the type of the item. Two spaces
40+
separate it from the line number so that the line number remains apparent.
41+
42+
For example, the following will be printed:
43+
44+
```
45+
--> main.rs:14:13 in fn main
46+
```
47+
48+
Instead of:
49+
50+
```
51+
--> main.rs:14:13`
52+
```
53+
54+
### Context lines
55+
56+
To the source annotations, lines mentioning the context are added.
57+
58+
```
59+
warning: unused variable: `y`
60+
--> test.rs:13:13 in fn foo::bar
61+
|
62+
7 | mod foo {
63+
...
64+
12 | pub fn bar() {
65+
13 | let y = 1;
66+
| ^ help: consider prefixing with an underscore: `_y`
67+
|
68+
= note: `#[warn(unused_variables)]` on by default
69+
```
70+
71+
To prevent overloading the diagnostics with repeated information regarding
72+
contexts having multiple errors in them, each context line will be mentioned
73+
only once for that purpose. For example:
74+
75+
```
76+
warning: unused variable: `y`
77+
--> test.rs:13:13 in fn foo::bar
78+
|
79+
7 | mod foo {
80+
...
81+
12 | pub fn bar() {
82+
13 | let y = 1;
83+
| ^ help: consider prefixing with an underscore: `_y`
84+
|
85+
= note: `#[warn(unused_variables)]` on by default
86+
87+
warning: unused variable: `x`
88+
--> test.rs:14:13 in fn foo::bar
89+
|
90+
14 | let x = 1;
91+
| ^ help: consider prefixing with an underscore: `_x`
92+
```
93+
94+
95+
# Reference-level explanation
96+
[reference-level-explanation]: #reference-level-explanation
97+
98+
The information for the line is gathered by a mechanism to resolve a `Span` to
99+
the corresponding named scope, by using a Trait dynamic call from
100+
`librustc_error` back to `libsyntax`, in addition to what is done with
101+
`CodeMap`. The call does a quick DFS into the AST for the `Span` and collects
102+
the relevant `Ident` path, so it should handle nested scopes properly.
103+
104+
# Drawbacks
105+
[drawbacks]: #drawbacks
106+
107+
We may not want to do this because to prevent unnecessary noise in
108+
diagnositics. AFAIK, there were no loud complaints about this information
109+
missing thus far.
110+
111+
# Rationale and alternatives
112+
[alternatives]: #alternatives
113+
114+
Alternatively, we can choose not to this. However we would rely on IDEs
115+
being smart enough to assist developers in navigating through warnings and
116+
errors (i.e the common jump-to-source functionality).
117+
118+
# Prior art
119+
[prior-art]: #prior-art
120+
121+
Other compilers do this. For example, `gcc` emits `In function 'foo':`. Git
122+
diff adds context to unidiff hunk header information to increase readability.
123+
Clang does not do this, neither does Python. Java (OpenJDK) does so to some
124+
degree (`error: variable y is already defined in method x()`).
125+
126+
# Unresolved questions
127+
[unresolved]: #unresolved-questions
128+
129+
- Should we avoid adding the extra context lines altogether?
130+
- Should we avoid adding the item path next to the line number?
131+
- Should we handle more scoped items such as loop labels, and closures? What
132+
would be their path strings?

0 commit comments

Comments
 (0)