2020-08-27 20:00:21 +10:00
|
|
|
// check-fail
|
|
|
|
// Tests error conditions for specifying diagnostics using #[derive(SessionDiagnostic)]
|
|
|
|
|
2021-06-07 17:08:13 +02:00
|
|
|
// 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
|
|
|
|
|
2020-08-27 20:00:21 +10:00
|
|
|
#![feature(rustc_private)]
|
|
|
|
#![crate_type = "lib"]
|
|
|
|
|
|
|
|
extern crate rustc_span;
|
|
|
|
use rustc_span::symbol::Ident;
|
2022-03-30 09:45:36 +01:00
|
|
|
use rustc_span::Span;
|
2020-08-27 20:00:21 +10:00
|
|
|
|
|
|
|
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)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "hello-world")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct Hello {}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[warning(code = "E0123", slug = "hello-world")]
|
|
|
|
struct HelloWarn {}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
//~^ ERROR `#[derive(SessionDiagnostic)]` can only be used on structs
|
|
|
|
enum SessionDiagnosticOnEnum {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
#[error = "E0123"]
|
2022-03-31 08:35:17 +01:00
|
|
|
//~^ ERROR `#[error = ...]` is not a valid `SessionDiagnostic` struct attribute
|
|
|
|
struct WrongStructAttrStyle {}
|
2020-08-27 20:00:21 +10:00
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[nonsense(code = "E0123", slug = "foo")]
|
|
|
|
//~^ ERROR `#[nonsense(...)]` is not a valid `SessionDiagnostic` struct attribute
|
|
|
|
//~^^ ERROR diagnostic kind not specified
|
|
|
|
//~^^^ ERROR cannot find attribute `nonsense` in this scope
|
|
|
|
struct InvalidStructAttr {}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
|
|
|
#[error("E0123")]
|
|
|
|
//~^ ERROR `#[error("...")]` is not a valid `SessionDiagnostic` struct attribute
|
|
|
|
//~^^ ERROR `slug` not specified
|
|
|
|
struct InvalidLitNestedAttr {}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
|
|
|
#[error(nonsense, code = "E0123", slug = "foo")]
|
|
|
|
//~^ ERROR `#[error(nonsense)]` is not a valid `SessionDiagnostic` struct attribute
|
|
|
|
struct InvalidNestedStructAttr {}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
|
|
|
#[error(nonsense("foo"), code = "E0123", slug = "foo")]
|
|
|
|
//~^ ERROR `#[error(nonsense(...))]` is not a valid `SessionDiagnostic` struct attribute
|
|
|
|
struct InvalidNestedStructAttr1 {}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
|
|
|
#[error(nonsense = "...", code = "E0123", slug = "foo")]
|
|
|
|
//~^ ERROR `#[error(nonsense = ...)]` is not a valid `SessionDiagnostic` struct attribute
|
|
|
|
struct InvalidNestedStructAttr2 {}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
|
|
|
#[error(nonsense = 4, code = "E0123", slug = "foo")]
|
|
|
|
//~^ ERROR `#[error(nonsense = ...)]` is not a valid `SessionDiagnostic` struct attribute
|
|
|
|
struct InvalidNestedStructAttr3 {}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct WrongPlaceField {
|
|
|
|
#[suggestion = "this is the wrong kind of attribute"]
|
2022-03-30 10:04:03 +01:00
|
|
|
//~^ ERROR `#[suggestion = ...]` is not a valid `SessionDiagnostic` field attribute
|
2020-08-27 20:00:21 +10:00
|
|
|
sp: Span,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
|
|
|
#[error(code = "E0456", slug = "bar")] //~ ERROR `error` specified multiple times
|
2020-08-27 20:00:21 +10:00
|
|
|
struct ErrorSpecifiedTwice {}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
|
|
|
#[warning(code = "E0293", slug = "bar")]
|
|
|
|
//~^ ERROR `warning` specified when `error` was already specified
|
|
|
|
struct WarnSpecifiedAfterError {}
|
2020-08-27 20:00:21 +10:00
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0456", code = "E0457", slug = "bar")] //~ ERROR `code` specified multiple times
|
|
|
|
struct CodeSpecifiedTwice {}
|
2020-08-27 20:00:21 +10:00
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0456", slug = "foo", slug = "bar")] //~ ERROR `slug` specified multiple times
|
|
|
|
struct SlugSpecifiedTwice {}
|
2020-08-27 20:00:21 +10:00
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
struct KindNotProvided {} //~ ERROR diagnostic kind not specified
|
2020-08-27 20:00:21 +10:00
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0456")] //~ ERROR `slug` not specified
|
|
|
|
struct SlugNotProvided {}
|
2020-08-27 20:00:21 +10:00
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 09:21:42 +01:00
|
|
|
#[error(slug = "foo")]
|
2022-03-31 08:35:17 +01:00
|
|
|
struct CodeNotProvided {}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
|
|
|
#[error(code = "E0123", slug = "foo")]
|
|
|
|
struct MessageWrongType {
|
2022-03-31 08:50:45 +01:00
|
|
|
#[primary_span]
|
|
|
|
//~^ ERROR `#[primary_span]` attribute can only be applied to fields of type `Span`
|
2022-03-31 08:35:17 +01:00
|
|
|
foo: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
|
|
|
#[error(code = "E0123", slug = "foo")]
|
|
|
|
struct InvalidPathFieldAttr {
|
|
|
|
#[nonsense]
|
|
|
|
//~^ ERROR `#[nonsense]` is not a valid `SessionDiagnostic` field attribute
|
|
|
|
//~^^ ERROR cannot find attribute `nonsense` in this scope
|
|
|
|
foo: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct ErrorWithField {
|
|
|
|
name: String,
|
2022-03-31 08:35:17 +01:00
|
|
|
#[label = "This error has a field, and references {name}"]
|
2022-03-30 10:04:03 +01:00
|
|
|
span: Span,
|
2020-08-27 20:00:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct ErrorWithMessageAppliedToField {
|
2022-03-31 08:35:17 +01:00
|
|
|
#[label = "this message is applied to a String field"]
|
|
|
|
//~^ ERROR the `#[label = ...]` attribute can only be applied to fields of type `Span`
|
2020-08-27 20:00:21 +10:00
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct ErrorWithNonexistentField {
|
2022-03-31 08:35:17 +01:00
|
|
|
#[label = "This error has a field, and references {name}"]
|
|
|
|
//~^ ERROR `name` doesn't refer to a field on this type
|
|
|
|
foo: Span,
|
2020-08-27 20:00:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
//~^ ERROR invalid format string: expected `'}'`
|
|
|
|
struct ErrorMissingClosingBrace {
|
2022-03-31 08:35:17 +01:00
|
|
|
#[label = "This is missing a closing brace: {name"]
|
|
|
|
foo: Span,
|
2020-08-27 20:00:21 +10:00
|
|
|
name: String,
|
2022-03-30 09:45:36 +01:00
|
|
|
val: usize,
|
2020-08-27 20:00:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
//~^ ERROR invalid format string: unmatched `}`
|
|
|
|
struct ErrorMissingOpeningBrace {
|
2022-03-31 08:35:17 +01:00
|
|
|
#[label = "This is missing an opening brace: name}"]
|
|
|
|
foo: Span,
|
2020-08-27 20:00:21 +10:00
|
|
|
name: String,
|
2022-03-30 09:45:36 +01:00
|
|
|
val: usize,
|
2020-08-27 20:00:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct LabelOnSpan {
|
|
|
|
#[label = "See here"]
|
2022-03-30 10:04:03 +01:00
|
|
|
sp: Span,
|
2020-08-27 20:00:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct LabelOnNonSpan {
|
|
|
|
#[label = "See here"]
|
2022-03-31 08:35:17 +01:00
|
|
|
//~^ ERROR the `#[label = ...]` attribute can only be applied to fields of type `Span`
|
2020-08-27 20:00:21 +10:00
|
|
|
id: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
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)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct SuggestWithoutCode {
|
|
|
|
#[suggestion(message = "This is a suggestion")]
|
|
|
|
suggestion: (Span, Applicability),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct SuggestWithBadKey {
|
|
|
|
#[suggestion(nonsense = "This is nonsense")]
|
|
|
|
//~^ ERROR `nonsense` is not a valid key for `#[suggestion(...)]`
|
|
|
|
suggestion: (Span, Applicability),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct SuggestWithShorthandMsg {
|
|
|
|
#[suggestion(msg = "This is a suggestion")]
|
|
|
|
//~^ ERROR `msg` is not a valid key for `#[suggestion(...)]`
|
|
|
|
suggestion: (Span, Applicability),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct SuggestWithoutMsg {
|
|
|
|
#[suggestion(code = "This is suggested code")]
|
|
|
|
//~^ ERROR missing suggestion message
|
|
|
|
suggestion: (Span, Applicability),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct SuggestWithTypesSwapped {
|
|
|
|
#[suggestion(message = "This is a message", code = "This is suggested code")]
|
|
|
|
suggestion: (Applicability, Span),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct SuggestWithWrongTypeApplicabilityOnly {
|
|
|
|
#[suggestion(message = "This is a message", code = "This is suggested code")]
|
|
|
|
//~^ ERROR wrong field type for suggestion
|
|
|
|
suggestion: Applicability,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2022-03-30 10:04:03 +01:00
|
|
|
struct SuggestWithSpanOnly {
|
2020-08-27 20:00:21 +10:00
|
|
|
#[suggestion(message = "This is a message", code = "This is suggested code")]
|
|
|
|
suggestion: Span,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct SuggestWithDuplicateSpanAndApplicability {
|
|
|
|
#[suggestion(message = "This is a message", code = "This is suggested code")]
|
2022-03-30 10:04:03 +01:00
|
|
|
//~^ ERROR type of field annotated with `#[suggestion(...)]` contains more than one `Span`
|
2020-08-27 20:00:21 +10:00
|
|
|
suggestion: (Span, Span, Applicability),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
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)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct WrongKindOfAnnotation {
|
|
|
|
#[label("wrong kind of annotation for label")]
|
|
|
|
//~^ ERROR invalid annotation list `#[label(...)]`
|
|
|
|
z: Span,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct OptionsInErrors {
|
|
|
|
#[label = "Label message"]
|
|
|
|
label: Option<Span>,
|
|
|
|
#[suggestion(message = "suggestion message")]
|
|
|
|
opt_sugg: Option<(Span, Applicability)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0456", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct MoveOutOfBorrowError<'tcx> {
|
|
|
|
name: Ident,
|
|
|
|
ty: Ty<'tcx>,
|
2022-03-31 08:50:45 +01:00
|
|
|
#[primary_span]
|
2020-08-27 20:00:21 +10:00
|
|
|
#[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)]
|
2022-03-31 08:35:17 +01:00
|
|
|
#[error(code = "E0123", slug = "foo")]
|
2020-08-27 20:00:21 +10:00
|
|
|
struct ErrorWithLifetime<'a> {
|
2022-03-31 08:35:17 +01:00
|
|
|
#[label = "Some message that references {name}"]
|
2020-08-27 20:00:21 +10:00
|
|
|
span: Span,
|
|
|
|
name: &'a str,
|
|
|
|
}
|
2022-03-31 09:02:31 +01:00
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
|
|
|
//~^ ERROR no method named `into_diagnostic_arg` found for struct `Hello` in the current scope
|
|
|
|
#[error(code = "E0123", slug = "foo")]
|
|
|
|
struct ArgFieldWithoutSkip {
|
|
|
|
#[primary_span]
|
|
|
|
span: Span,
|
|
|
|
other: Hello,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SessionDiagnostic)]
|
|
|
|
#[error(code = "E0123", slug = "foo")]
|
|
|
|
struct ArgFieldWithSkip {
|
|
|
|
#[primary_span]
|
|
|
|
span: Span,
|
|
|
|
// `Hello` does not implement `IntoDiagnosticArg` so this would result in an error if
|
|
|
|
// not for `#[skip_arg]`.
|
|
|
|
#[skip_arg]
|
|
|
|
other: Hello,
|
|
|
|
}
|