summary refs log tree commit diff
path: root/src/test/ui-fulldeps/session-derive-errors.rs
blob: 140aaad3b3839af41abdf4d722a7007b80ec124e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// check-fail
// Tests error conditions for specifying diagnostics using #[derive(SessionDiagnostic)]

// The proc_macro2 crate handles spans differently when on beta/stable release rather than nightly,
// changing the output of this test. Since SessionDiagnostic is strictly internal to the compiler
// the test is just ignored on stable and beta:
// ignore-beta
// ignore-stable

#![feature(rustc_private)]
#![crate_type = "lib"]

extern crate rustc_span;
use rustc_span::Span;
use rustc_span::symbol::Ident;

extern crate rustc_macros;
use rustc_macros::SessionDiagnostic;

extern crate rustc_middle;
use rustc_middle::ty::Ty;

extern crate rustc_errors;
use rustc_errors::Applicability;

extern crate rustc_session;

#[derive(SessionDiagnostic)]
#[message = "Hello, world!"]
#[error = "E0123"]
struct Hello {}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
//~^ ERROR `#[derive(SessionDiagnostic)]` can only be used on structs
enum SessionDiagnosticOnEnum {
    Foo,
    Bar,
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
#[label = "This is in the wrong place"]
//~^ ERROR `#[label = ...]` is not a valid SessionDiagnostic struct attribute
struct WrongPlace {}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
struct WrongPlaceField {
    #[suggestion = "this is the wrong kind of attribute"]
//~^ ERROR `#[suggestion = ...]` is not a valid SessionDiagnostic field attribute
    sp: Span,
}

#[derive(SessionDiagnostic)]
#[message = "Hello, world!"]
#[error = "E0123"]
#[error = "E0456"] //~ ERROR `error` specified multiple times
struct ErrorSpecifiedTwice {}

#[derive(SessionDiagnostic)]
#[message = "Hello, world!"]
#[error = "E0123"]
#[lint = "some_useful_lint"] //~ ERROR `lint` specified when `error` was already specified
struct LintSpecifiedAfterError {}

#[derive(SessionDiagnostic)]
#[message = "Some lint message"]
#[error = "E0123"]
struct LintButHasErrorCode {}

#[derive(SessionDiagnostic)]
struct ErrorCodeNotProvided {} //~ ERROR `code` not specified

// FIXME: Uncomment when emitting lints is supported.
/*
#[derive(SessionDiagnostic)]
#[message = "Hello, world!"]
#[lint = "clashing_extern_declarations"]
#[lint = "improper_ctypes"] // FIXME: ERROR `lint` specified multiple times
struct LintSpecifiedTwice {}

#[derive(SessionDiagnostic)]
#[lint = "Some lint message"]
#[message = "Some error message"]
#[error = "E0123"] // ERROR `error` specified when `lint` was already specified
struct ErrorSpecifiedAfterLint {}
*/

#[derive(SessionDiagnostic)]
#[error = "E0123"]
struct ErrorWithField {
    name: String,
    #[message = "This error has a field, and references {name}"]
    span: Span
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
struct ErrorWithMessageAppliedToField {
    #[message = "this message is applied to a String field"]
    //~^ ERROR the `#[message = "..."]` attribute can only be applied to fields of type Span
    name: String,
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
#[message = "This error has a field, and references {name}"]
//~^ ERROR `name` doesn't refer to a field on this type
struct ErrorWithNonexistentField {
    span: Span
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
#[message = "This is missing a closing brace: {name"]
//~^ ERROR invalid format string: expected `'}'`
struct ErrorMissingClosingBrace {
    name: String,
    span: Span
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
#[message = "This is missing an opening brace: name}"]
//~^ ERROR invalid format string: unmatched `}`
struct ErrorMissingOpeningBrace {
    name: String,
    span: Span
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
#[message = "Something something"]
struct LabelOnSpan {
    #[label = "See here"]
    sp: Span
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
#[message = "Something something"]
struct LabelOnNonSpan {
    #[label = "See here"]
    //~^ ERROR The `#[label = ...]` attribute can only be applied to fields of type Span
    id: u32,
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
struct Suggest {
    #[suggestion(message = "This is a suggestion", code = "This is the suggested code")]
    #[suggestion_short(message = "This is a suggestion", code = "This is the suggested code")]
    #[suggestion_hidden(message = "This is a suggestion", code = "This is the suggested code")]
    #[suggestion_verbose(message = "This is a suggestion", code = "This is the suggested code")]
    suggestion: (Span, Applicability),
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
struct SuggestWithoutCode {
    #[suggestion(message = "This is a suggestion")]
    suggestion: (Span, Applicability),
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
struct SuggestWithBadKey {
    #[suggestion(nonsense = "This is nonsense")]
    //~^ ERROR `nonsense` is not a valid key for `#[suggestion(...)]`
    suggestion: (Span, Applicability),
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
struct SuggestWithShorthandMsg {
    #[suggestion(msg = "This is a suggestion")]
    //~^ ERROR `msg` is not a valid key for `#[suggestion(...)]`
    suggestion: (Span, Applicability),
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
struct SuggestWithoutMsg {
    #[suggestion(code = "This is suggested code")]
    //~^ ERROR missing suggestion message
    suggestion: (Span, Applicability),
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
struct SuggestWithTypesSwapped {
    #[suggestion(message = "This is a message", code = "This is suggested code")]
    suggestion: (Applicability, Span),
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
struct SuggestWithWrongTypeApplicabilityOnly {
    #[suggestion(message = "This is a message", code = "This is suggested code")]
    //~^ ERROR wrong field type for suggestion
    suggestion: Applicability,
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
struct SuggestWithSpanOnly{
    #[suggestion(message = "This is a message", code = "This is suggested code")]
    suggestion: Span,
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
struct SuggestWithDuplicateSpanAndApplicability {
    #[suggestion(message = "This is a message", code = "This is suggested code")]
    //~^ ERROR type of field annotated with `#[suggestion(...)]` contains more than one Span
    suggestion: (Span, Span, Applicability),
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
struct SuggestWithDuplicateApplicabilityAndSpan {
    #[suggestion(message = "This is a message", code = "This is suggested code")]
    //~^ ERROR type of field annotated with `#[suggestion(...)]` contains more than one
    suggestion: (Applicability, Applicability, Span),
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
struct WrongKindOfAnnotation {
    #[label("wrong kind of annotation for label")]
    //~^ ERROR invalid annotation list `#[label(...)]`
    z: Span,
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
#[message = "Something something else"]
struct OptionsInErrors {
    #[label = "Label message"]
    label: Option<Span>,
    #[suggestion(message = "suggestion message")]
    opt_sugg: Option<(Span, Applicability)>,
}

#[derive(SessionDiagnostic)]
#[error = "E0456"]
struct MoveOutOfBorrowError<'tcx> {
    name: Ident,
    ty: Ty<'tcx>,
    #[message = "cannot move {ty} out of borrow"]
    #[label = "cannot move out of borrow"]
    span: Span,
    #[label = "`{ty}` first borrowed here"]
    other_span: Span,
    #[suggestion(message = "consider cloning here", code = "{name}.clone()")]
    opt_sugg: Option<(Span, Applicability)>,
}

#[derive(SessionDiagnostic)]
#[error = "E0123"]
struct ErrorWithLifetime<'a> {
    #[message = "Some message that references {name}"]
    span: Span,
    name: &'a str,
}