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