]> git.lizzy.rs Git - rust.git/blob - src/test/ui-fulldeps/session-derive-errors.rs
Auto merge of #82980 - tmiasko:import-cold-multiplier, r=michaelwoerister
[rust.git] / src / test / ui-fulldeps / session-derive-errors.rs
1 // check-fail
2 // Tests error conditions for specifying diagnostics using #[derive(SessionDiagnostic)]
3
4 #![feature(rustc_private)]
5 #![crate_type = "lib"]
6
7 extern crate rustc_span;
8 use rustc_span::Span;
9 use rustc_span::symbol::Ident;
10
11 extern crate rustc_macros;
12 use rustc_macros::SessionDiagnostic;
13
14 extern crate rustc_middle;
15 use rustc_middle::ty::Ty;
16
17 extern crate rustc_errors;
18 use rustc_errors::Applicability;
19
20 extern crate rustc_session;
21
22 #[derive(SessionDiagnostic)]
23 #[message = "Hello, world!"]
24 #[error = "E0123"]
25 struct Hello {}
26
27 #[derive(SessionDiagnostic)]
28 #[error = "E0123"]
29 //~^ ERROR `#[derive(SessionDiagnostic)]` can only be used on structs
30 enum SessionDiagnosticOnEnum {
31     Foo,
32     Bar,
33 }
34
35 #[derive(SessionDiagnostic)]
36 #[error = "E0123"]
37 #[label = "This is in the wrong place"]
38 //~^ ERROR `#[label = ...]` is not a valid SessionDiagnostic struct attribute
39 struct WrongPlace {}
40
41 #[derive(SessionDiagnostic)]
42 #[error = "E0123"]
43 struct WrongPlaceField {
44     #[suggestion = "this is the wrong kind of attribute"]
45 //~^ ERROR `#[suggestion = ...]` is not a valid SessionDiagnostic field attribute
46     sp: Span,
47 }
48
49 #[derive(SessionDiagnostic)]
50 #[message = "Hello, world!"]
51 #[error = "E0123"]
52 #[error = "E0456"] //~ ERROR `error` specified multiple times
53 struct ErrorSpecifiedTwice {}
54
55 #[derive(SessionDiagnostic)]
56 #[message = "Hello, world!"]
57 #[error = "E0123"]
58 #[lint = "some_useful_lint"] //~ ERROR `lint` specified when `error` was already specified
59 struct LintSpecifiedAfterError {}
60
61 #[derive(SessionDiagnostic)]
62 #[message = "Some lint message"]
63 #[error = "E0123"]
64 struct LintButHasErrorCode {}
65
66 #[derive(SessionDiagnostic)]
67 struct ErrorCodeNotProvided {} //~ ERROR `code` not specified
68
69 // FIXME: Uncomment when emitting lints is supported.
70 /*
71 #[derive(SessionDiagnostic)]
72 #[message = "Hello, world!"]
73 #[lint = "clashing_extern_declarations"]
74 #[lint = "improper_ctypes"] // FIXME: ERROR `lint` specified multiple times
75 struct LintSpecifiedTwice {}
76
77 #[derive(SessionDiagnostic)]
78 #[lint = "Some lint message"]
79 #[message = "Some error message"]
80 #[error = "E0123"] // ERROR `error` specified when `lint` was already specified
81 struct ErrorSpecifiedAfterLint {}
82 */
83
84 #[derive(SessionDiagnostic)]
85 #[error = "E0123"]
86 struct ErrorWithField {
87     name: String,
88     #[message = "This error has a field, and references {name}"]
89     span: Span
90 }
91
92 #[derive(SessionDiagnostic)]
93 #[error = "E0123"]
94 struct ErrorWithMessageAppliedToField {
95     #[message = "this message is applied to a String field"]
96     //~^ ERROR the `#[message = "..."]` attribute can only be applied to fields of type Span
97     name: String,
98 }
99
100 #[derive(SessionDiagnostic)]
101 #[error = "E0123"]
102 #[message = "This error has a field, and references {name}"]
103 //~^ ERROR `name` doesn't refer to a field on this type
104 struct ErrorWithNonexistentField {
105     span: Span
106 }
107
108 #[derive(SessionDiagnostic)]
109 #[error = "E0123"]
110 #[message = "This is missing a closing brace: {name"]
111 //~^ ERROR invalid format string: expected `'}'`
112 struct ErrorMissingClosingBrace {
113     name: String,
114     span: Span
115 }
116
117 #[derive(SessionDiagnostic)]
118 #[error = "E0123"]
119 #[message = "This is missing an opening brace: name}"]
120 //~^ ERROR invalid format string: unmatched `}`
121 struct ErrorMissingOpeningBrace {
122     name: String,
123     span: Span
124 }
125
126 #[derive(SessionDiagnostic)]
127 #[error = "E0123"]
128 #[message = "Something something"]
129 struct LabelOnSpan {
130     #[label = "See here"]
131     sp: Span
132 }
133
134 #[derive(SessionDiagnostic)]
135 #[error = "E0123"]
136 #[message = "Something something"]
137 struct LabelOnNonSpan {
138     #[label = "See here"]
139     //~^ ERROR The `#[label = ...]` attribute can only be applied to fields of type Span
140     id: u32,
141 }
142
143 #[derive(SessionDiagnostic)]
144 #[error = "E0123"]
145 struct Suggest {
146     #[suggestion(message = "This is a suggestion", code = "This is the suggested code")]
147     #[suggestion_short(message = "This is a suggestion", code = "This is the suggested code")]
148     #[suggestion_hidden(message = "This is a suggestion", code = "This is the suggested code")]
149     #[suggestion_verbose(message = "This is a suggestion", code = "This is the suggested code")]
150     suggestion: (Span, Applicability),
151 }
152
153 #[derive(SessionDiagnostic)]
154 #[error = "E0123"]
155 struct SuggestWithoutCode {
156     #[suggestion(message = "This is a suggestion")]
157     suggestion: (Span, Applicability),
158 }
159
160 #[derive(SessionDiagnostic)]
161 #[error = "E0123"]
162 struct SuggestWithBadKey {
163     #[suggestion(nonsense = "This is nonsense")]
164     //~^ ERROR `nonsense` is not a valid key for `#[suggestion(...)]`
165     suggestion: (Span, Applicability),
166 }
167
168 #[derive(SessionDiagnostic)]
169 #[error = "E0123"]
170 struct SuggestWithShorthandMsg {
171     #[suggestion(msg = "This is a suggestion")]
172     //~^ ERROR `msg` is not a valid key for `#[suggestion(...)]`
173     suggestion: (Span, Applicability),
174 }
175
176 #[derive(SessionDiagnostic)]
177 #[error = "E0123"]
178 struct SuggestWithoutMsg {
179     #[suggestion(code = "This is suggested code")]
180     //~^ ERROR missing suggestion message
181     suggestion: (Span, Applicability),
182 }
183
184 #[derive(SessionDiagnostic)]
185 #[error = "E0123"]
186 struct SuggestWithTypesSwapped {
187     #[suggestion(message = "This is a message", code = "This is suggested code")]
188     suggestion: (Applicability, Span),
189 }
190
191 #[derive(SessionDiagnostic)]
192 #[error = "E0123"]
193 struct SuggestWithWrongTypeApplicabilityOnly {
194     #[suggestion(message = "This is a message", code = "This is suggested code")]
195     //~^ ERROR wrong field type for suggestion
196     suggestion: Applicability,
197 }
198
199 #[derive(SessionDiagnostic)]
200 #[error = "E0123"]
201 struct SuggestWithSpanOnly{
202     #[suggestion(message = "This is a message", code = "This is suggested code")]
203     suggestion: Span,
204 }
205
206 #[derive(SessionDiagnostic)]
207 #[error = "E0123"]
208 struct SuggestWithDuplicateSpanAndApplicability {
209     #[suggestion(message = "This is a message", code = "This is suggested code")]
210     //~^ ERROR type of field annotated with `#[suggestion(...)]` contains more than one Span
211     suggestion: (Span, Span, Applicability),
212 }
213
214 #[derive(SessionDiagnostic)]
215 #[error = "E0123"]
216 struct SuggestWithDuplicateApplicabilityAndSpan {
217     #[suggestion(message = "This is a message", code = "This is suggested code")]
218     //~^ ERROR type of field annotated with `#[suggestion(...)]` contains more than one
219     suggestion: (Applicability, Applicability, Span),
220 }
221
222 #[derive(SessionDiagnostic)]
223 #[error = "E0123"]
224 struct WrongKindOfAnnotation {
225     #[label("wrong kind of annotation for label")]
226     //~^ ERROR invalid annotation list `#[label(...)]`
227     z: Span,
228 }
229
230 #[derive(SessionDiagnostic)]
231 #[error = "E0123"]
232 #[message = "Something something else"]
233 struct OptionsInErrors {
234     #[label = "Label message"]
235     label: Option<Span>,
236     #[suggestion(message = "suggestion message")]
237     opt_sugg: Option<(Span, Applicability)>,
238 }
239
240 #[derive(SessionDiagnostic)]
241 #[error = "E0456"]
242 struct MoveOutOfBorrowError<'tcx> {
243     name: Ident,
244     ty: Ty<'tcx>,
245     #[message = "cannot move {ty} out of borrow"]
246     #[label = "cannot move out of borrow"]
247     span: Span,
248     #[label = "`{ty}` first borrowed here"]
249     other_span: Span,
250     #[suggestion(message = "consider cloning here", code = "{name}.clone()")]
251     opt_sugg: Option<(Span, Applicability)>,
252 }
253
254 #[derive(SessionDiagnostic)]
255 #[error = "E0123"]
256 struct ErrorWithLifetime<'a> {
257     #[message = "Some message that references {name}"]
258     span: Span,
259     name: &'a str,
260 }