2 // Tests error conditions for specifying diagnostics using #[derive(SessionDiagnostic)]
4 #![feature(rustc_private)]
7 extern crate rustc_span;
9 use rustc_span::symbol::Ident;
11 extern crate rustc_macros;
12 use rustc_macros::SessionDiagnostic;
14 extern crate rustc_middle;
15 use rustc_middle::ty::Ty;
17 extern crate rustc_errors;
18 use rustc_errors::Applicability;
20 extern crate rustc_session;
22 #[derive(SessionDiagnostic)]
23 #[message = "Hello, world!"]
27 #[derive(SessionDiagnostic)]
29 //~^ ERROR `#[derive(SessionDiagnostic)]` can only be used on structs
30 enum SessionDiagnosticOnEnum {
35 #[derive(SessionDiagnostic)]
37 #[label = "This is in the wrong place"]
38 //~^ ERROR `#[label = ...]` is not a valid SessionDiagnostic struct attribute
41 #[derive(SessionDiagnostic)]
43 struct WrongPlaceField {
44 #[suggestion = "this is the wrong kind of attribute"]
45 //~^ ERROR `#[suggestion = ...]` is not a valid SessionDiagnostic field attribute
49 #[derive(SessionDiagnostic)]
50 #[message = "Hello, world!"]
52 #[error = "E0456"] //~ ERROR `error` specified multiple times
53 struct ErrorSpecifiedTwice {}
55 #[derive(SessionDiagnostic)]
56 #[message = "Hello, world!"]
58 #[lint = "some_useful_lint"] //~ ERROR `lint` specified when `error` was already specified
59 struct LintSpecifiedAfterError {}
61 #[derive(SessionDiagnostic)]
62 #[message = "Some lint message"]
64 struct LintButHasErrorCode {}
66 #[derive(SessionDiagnostic)]
67 struct ErrorCodeNotProvided {} //~ ERROR `code` not specified
69 // FIXME: Uncomment when emitting lints is supported.
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 {}
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 {}
84 #[derive(SessionDiagnostic)]
86 struct ErrorWithField {
88 #[message = "This error has a field, and references {name}"]
92 #[derive(SessionDiagnostic)]
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
100 #[derive(SessionDiagnostic)]
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 {
108 #[derive(SessionDiagnostic)]
110 #[message = "This is missing a closing brace: {name"]
111 //~^ ERROR invalid format string: expected `'}'`
112 struct ErrorMissingClosingBrace {
117 #[derive(SessionDiagnostic)]
119 #[message = "This is missing an opening brace: name}"]
120 //~^ ERROR invalid format string: unmatched `}`
121 struct ErrorMissingOpeningBrace {
126 #[derive(SessionDiagnostic)]
128 #[message = "Something something"]
130 #[label = "See here"]
134 #[derive(SessionDiagnostic)]
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
143 #[derive(SessionDiagnostic)]
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),
153 #[derive(SessionDiagnostic)]
155 struct SuggestWithoutCode {
156 #[suggestion(message = "This is a suggestion")]
157 suggestion: (Span, Applicability),
160 #[derive(SessionDiagnostic)]
162 struct SuggestWithBadKey {
163 #[suggestion(nonsense = "This is nonsense")]
164 //~^ ERROR `nonsense` is not a valid key for `#[suggestion(...)]`
165 suggestion: (Span, Applicability),
168 #[derive(SessionDiagnostic)]
170 struct SuggestWithShorthandMsg {
171 #[suggestion(msg = "This is a suggestion")]
172 //~^ ERROR `msg` is not a valid key for `#[suggestion(...)]`
173 suggestion: (Span, Applicability),
176 #[derive(SessionDiagnostic)]
178 struct SuggestWithoutMsg {
179 #[suggestion(code = "This is suggested code")]
180 //~^ ERROR missing suggestion message
181 suggestion: (Span, Applicability),
184 #[derive(SessionDiagnostic)]
186 struct SuggestWithTypesSwapped {
187 #[suggestion(message = "This is a message", code = "This is suggested code")]
188 suggestion: (Applicability, Span),
191 #[derive(SessionDiagnostic)]
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,
199 #[derive(SessionDiagnostic)]
201 struct SuggestWithSpanOnly{
202 #[suggestion(message = "This is a message", code = "This is suggested code")]
206 #[derive(SessionDiagnostic)]
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),
214 #[derive(SessionDiagnostic)]
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),
222 #[derive(SessionDiagnostic)]
224 struct WrongKindOfAnnotation {
225 #[label("wrong kind of annotation for label")]
226 //~^ ERROR invalid annotation list `#[label(...)]`
230 #[derive(SessionDiagnostic)]
232 #[message = "Something something else"]
233 struct OptionsInErrors {
234 #[label = "Label message"]
236 #[suggestion(message = "suggestion message")]
237 opt_sugg: Option<(Span, Applicability)>,
240 #[derive(SessionDiagnostic)]
242 struct MoveOutOfBorrowError<'tcx> {
245 #[message = "cannot move {ty} out of borrow"]
246 #[label = "cannot move out of borrow"]
248 #[label = "`{ty}` first borrowed here"]
250 #[suggestion(message = "consider cloning here", code = "{name}.clone()")]
251 opt_sugg: Option<(Span, Applicability)>,
254 #[derive(SessionDiagnostic)]
256 struct ErrorWithLifetime<'a> {
257 #[message = "Some message that references {name}"]