Auto merge of #155200 - Unique-Usman:ua/diagnostic_on_type_error, r=mejrs

Introduce #[diagnostic::on_type_error(message)]
This commit is contained in:
bors
2026-06-14 08:30:12 +00:00
47 changed files with 1152 additions and 10 deletions

View File

@@ -24,6 +24,7 @@ use crate::parser::{ArgParser, MetaItemListParser, MetaItemOrLitParser, MetaItem
pub(crate) mod do_not_recommend;
pub(crate) mod on_const;
pub(crate) mod on_move;
pub(crate) mod on_type_error;
pub(crate) mod on_unimplemented;
pub(crate) mod on_unknown;
pub(crate) mod on_unmatch_args;
@@ -42,6 +43,8 @@ pub(crate) enum Mode {
DiagnosticOnUnknown,
/// `#[diagnostic::on_unmatch_args]`
DiagnosticOnUnmatchArgs,
/// `#[diagnostic::on_type_error]`
DiagnosticOnTypeError,
}
impl Mode {
@@ -53,12 +56,15 @@ impl Mode {
Self::DiagnosticOnMove => "diagnostic::on_move",
Self::DiagnosticOnUnknown => "diagnostic::on_unknown",
Self::DiagnosticOnUnmatchArgs => "diagnostic::on_unmatch_args",
Self::DiagnosticOnTypeError => "diagnostic::on_type_error",
}
}
fn expected_options(&self) -> &'static str {
const DEFAULT: &str =
"at least one of the `message`, `note` and `label` options are expected";
const DIAGNOSTIC_ON_TYPE_ERROR_EXPECTED_OPTIONS: &str =
"at least a single `note` option is expected";
match self {
Self::RustcOnUnimplemented => {
"see <https://rustc-dev-guide.rust-lang.org/diagnostics.html#rustc_on_unimplemented>"
@@ -68,11 +74,14 @@ impl Mode {
Self::DiagnosticOnMove => DEFAULT,
Self::DiagnosticOnUnknown => DEFAULT,
Self::DiagnosticOnUnmatchArgs => DEFAULT,
Self::DiagnosticOnTypeError => DIAGNOSTIC_ON_TYPE_ERROR_EXPECTED_OPTIONS,
}
}
fn allowed_options(&self) -> &'static str {
const DEFAULT: &str = "only `message`, `note` and `label` are allowed as options";
const DIAGNOSTIC_ON_TYPE_ERROR_ALLOWED_OPTIONS: &str =
"only `note` is allowed as option for `diagnostic::on_type_error`";
match self {
Self::RustcOnUnimplemented => {
"see <https://rustc-dev-guide.rust-lang.org/diagnostics.html#rustc_on_unimplemented>"
@@ -82,6 +91,7 @@ impl Mode {
Self::DiagnosticOnMove => DEFAULT,
Self::DiagnosticOnUnknown => DEFAULT,
Self::DiagnosticOnUnmatchArgs => DEFAULT,
Self::DiagnosticOnTypeError => DIAGNOSTIC_ON_TYPE_ERROR_ALLOWED_OPTIONS,
}
}
@@ -105,6 +115,9 @@ impl Mode {
Self::DiagnosticOnUnmatchArgs => {
"only `This` is allowed as a format argument, referring to the macro's name"
}
Self::DiagnosticOnTypeError => {
"only `note` is allowed as option for `diagnostic::on_type_error`"
}
}
}
}
@@ -294,7 +307,15 @@ fn parse_directive_items<'p>(
}
};
match (mode, name) {
(_, sym::message) => {
(
Mode::RustcOnUnimplemented
| Mode::DiagnosticOnUnimplemented
| Mode::DiagnosticOnConst
| Mode::DiagnosticOnMove
| Mode::DiagnosticOnUnknown
| Mode::DiagnosticOnUnmatchArgs,
sym::message,
) => {
let value = or_malformed!(value?);
if let Some(message) = &message {
duplicate!(name, message.0)
@@ -302,7 +323,15 @@ fn parse_directive_items<'p>(
message = Some((item.span(), parse_format(value)));
}
}
(_, sym::label) => {
(
Mode::RustcOnUnimplemented
| Mode::DiagnosticOnUnimplemented
| Mode::DiagnosticOnConst
| Mode::DiagnosticOnMove
| Mode::DiagnosticOnUnknown
| Mode::DiagnosticOnUnmatchArgs,
sym::label,
) => {
let value = or_malformed!(value?);
if let Some(label) = &label {
duplicate!(name, label.0)
@@ -356,7 +385,6 @@ fn parse_directive_items<'p>(
malformed!();
}
}
_other => {
malformed!();
}
@@ -430,13 +458,19 @@ fn parse_arg(
_ => FormatArg::This,
},
(Mode::DiagnosticOnTypeError, sym::Found) => FormatArg::Found,
(Mode::DiagnosticOnTypeError, sym::Expected) => FormatArg::Expected,
// Some diagnostic attributes can use `{This}` to refer to the annotated item.
// For those that don't, we continue and maybe use it as a generic parameter.
//
// FIXME(mejrs) `DiagnosticOnUnimplemented` is intentionally not here;
// that requires lang approval which is best kept for a standalone PR.
(
Mode::DiagnosticOnUnknown | Mode::DiagnosticOnMove | Mode::DiagnosticOnUnmatchArgs,
Mode::DiagnosticOnUnknown
| Mode::DiagnosticOnMove
| Mode::DiagnosticOnUnmatchArgs
| Mode::DiagnosticOnTypeError,
sym::This,
) => FormatArg::This,
@@ -462,7 +496,8 @@ fn parse_arg(
Mode::RustcOnUnimplemented
| Mode::DiagnosticOnUnimplemented
| Mode::DiagnosticOnMove
| Mode::DiagnosticOnConst,
| Mode::DiagnosticOnConst
| Mode::DiagnosticOnTypeError,
generic_param,
) => FormatArg::GenericParam { generic_param, span },

View File

@@ -0,0 +1,64 @@
use rustc_hir::attrs::AttributeKind;
use rustc_lint_defs::builtin::MISPLACED_DIAGNOSTIC_ATTRIBUTES;
use rustc_span::sym;
use crate::attributes::AttributeStability;
use crate::attributes::diagnostic::*;
use crate::attributes::prelude::*;
use crate::context::AcceptContext;
use crate::diagnostics::DiagnosticOnTypeErrorOnlyForAdt;
use crate::parser::ArgParser;
use crate::target_checking::{ALL_TARGETS, AllowedTargets};
use crate::template;
#[derive(Default)]
pub(crate) struct OnTypeErrorParser {
span: Option<Span>,
directive: Option<(Span, Directive)>,
}
impl OnTypeErrorParser {
fn parse<'sess>(&mut self, cx: &mut AcceptContext<'_, 'sess>, args: &ArgParser, mode: Mode) {
if !cx.features().diagnostic_on_type_error() {
return;
}
let span = cx.attr_span;
self.span = Some(span);
if !matches!(cx.target, Target::Enum | Target::Struct | Target::Union) {
cx.emit_lint(MISPLACED_DIAGNOSTIC_ATTRIBUTES, DiagnosticOnTypeErrorOnlyForAdt, span);
return;
}
let Some(items) = parse_list(cx, args, mode) else { return };
if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
merge_directives(cx, &mut self.directive, (span, directive));
}
}
}
impl AttributeParser for OnTypeErrorParser {
const ATTRIBUTES: AcceptMapping<Self> = &[(
&[sym::diagnostic, sym::on_type_error],
template!(List: &[r#"note = "...""#]),
AttributeStability::Stable,
|this, cx, args| {
this.parse(cx, args, Mode::DiagnosticOnTypeError);
},
)];
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
if let Some(span) = self.span {
Some(AttributeKind::OnTypeError {
span,
directive: self.directive.map(|d| Box::new(d.1)),
})
} else {
None
}
}
}

View File

@@ -32,6 +32,7 @@ use crate::attributes::deprecation::*;
use crate::attributes::diagnostic::do_not_recommend::*;
use crate::attributes::diagnostic::on_const::*;
use crate::attributes::diagnostic::on_move::*;
use crate::attributes::diagnostic::on_type_error::*;
use crate::attributes::diagnostic::on_unimplemented::*;
use crate::attributes::diagnostic::on_unknown::*;
use crate::attributes::diagnostic::on_unmatch_args::*;
@@ -145,6 +146,7 @@ attribute_parsers!(
NakedParser,
OnConstParser,
OnMoveParser,
OnTypeErrorParser,
OnUnimplementedParser,
OnUnknownParser,
OnUnmatchArgsParser,

View File

@@ -305,6 +305,10 @@ pub(crate) struct DiagnosticOnUnknownOnlyForImports {
#[diag("`#[diagnostic::on_unmatch_args]` can only be applied to macro definitions")]
pub(crate) struct DiagnosticOnUnmatchArgsOnlyForMacros;
#[derive(Diagnostic)]
#[diag("`#[diagnostic::on_type_error]` can only be applied to enums, structs or unions")]
pub(crate) struct DiagnosticOnTypeErrorOnlyForAdt;
#[derive(Diagnostic)]
#[diag("`#[diagnostic::do_not_recommend]` can only be placed on trait implementations")]
pub(crate) struct IncorrectDoNotRecommendLocation {

View File

@@ -510,6 +510,8 @@ declare_features! (
(unstable, diagnostic_on_const, "1.93.0", Some(143874)),
/// Allows giving on-move borrowck custom diagnostic messages for a type
(unstable, diagnostic_on_move, "1.96.0", Some(154181)),
/// Allows giving custom types diagnostic messages on type errors
(unstable, diagnostic_on_type_error, "CURRENT_RUSTC_VERSION", Some(155382)),
/// Allows giving unresolved imports a custom diagnostic message
(unstable, diagnostic_on_unknown, "1.96.0", Some(152900)),
/// Allows macros to customize macro argument matcher diagnostics.

View File

@@ -1173,6 +1173,12 @@ pub enum AttributeKind {
directive: Option<Box<Directive>>,
},
/// Represents`#[diagnostic::on_type_error]`.
OnTypeError {
span: Span,
directive: Option<Box<Directive>>,
},
/// Represents `#[rustc_on_unimplemented]` and `#[diagnostic::on_unimplemented]`.
OnUnimplemented {
/// None if the directive was malformed in some way.

View File

@@ -149,6 +149,10 @@ impl FormatString {
}
Piece::Arg(FormatArg::This) => ret.push_str(&args.this),
// only for on_type_error
Piece::Arg(FormatArg::Found) => ret.push_str(&args.found),
Piece::Arg(FormatArg::Expected) => ret.push_str(&args.expected),
// It's only `rustc_onunimplemented` from here
Piece::Arg(FormatArg::ThisPath) => ret.push_str(&args.this_path),
Piece::Arg(FormatArg::ThisResolved) => {
@@ -209,6 +213,8 @@ pub struct FormatArgs {
pub this: String,
pub this_resolved: String = String::new(),
pub this_path: String = String::new(),
pub found: String = String::new(),
pub expected: String = String::new(),
pub item_context: &'static str = "",
pub generic_args: Vec<(Symbol, String)> = Vec::new(),
}
@@ -238,6 +244,10 @@ pub enum FormatArg {
ItemContext,
/// What the user typed, if it doesn't match anything we can use.
AsIs(Symbol),
/// {Found} in diagnostic::on_type_error
Found,
/// {Expected} in diagnostic::on_type_error
Expected,
}
/// Represents the `on` filter in `#[rustc_on_unimplemented]`.

View File

@@ -78,6 +78,7 @@ impl AttributeKind {
NonExhaustive(..) => Yes, // Needed for rustdoc
OnConst { .. } => Yes,
OnMove { .. } => Yes,
OnTypeError { .. } => Yes,
OnUnimplemented { .. } => Yes,
OnUnknown { .. } => Yes,
OnUnmatchArgs { .. } => Yes,

View File

@@ -4451,6 +4451,7 @@ declare_lint! {
Warn,
"detects diagnostic attribute with malformed diagnostic format literals",
}
declare_lint! {
/// The `ambiguous_glob_imports` lint detects glob imports that should report ambiguity
/// errors, but previously didn't do that due to rustc bugs.

View File

@@ -39,8 +39,8 @@ use rustc_session::config::CrateType;
use rustc_session::errors::feature_err;
use rustc_session::lint;
use rustc_session::lint::builtin::{
CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
MISPLACED_DIAGNOSTIC_ATTRIBUTES, UNUSED_ATTRIBUTES,
CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, MALFORMED_DIAGNOSTIC_ATTRIBUTES,
MALFORMED_DIAGNOSTIC_FORMAT_LITERALS, MISPLACED_DIAGNOSTIC_ATTRIBUTES, UNUSED_ATTRIBUTES,
};
use rustc_span::edition::Edition;
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
@@ -249,6 +249,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
AttributeKind::OnMove { directive } => {
self.check_diagnostic_on_move(hir_id, directive.as_deref())
}
AttributeKind::OnTypeError { directive, .. } => {
self.check_diagnostic_on_type_error(hir_id, directive.as_deref())
}
// All of the following attributes have no specific checks.
// tidy-alphabetical-start
@@ -601,6 +604,58 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
}
fn check_diagnostic_on_type_error(&self, hir_id: HirId, directive: Option<&Directive>) {
if let Some(directive) = directive {
if let Node::Item(Item {
kind:
ItemKind::Struct(_, generics, _)
| ItemKind::Enum(_, generics, _)
| ItemKind::Union(_, generics, _),
..
}) = self.tcx.hir_node(hir_id)
{
let generic_count = generics
.params
.iter()
.filter(|p| !matches!(p.kind, GenericParamKind::Lifetime { .. }))
.count();
// Enforce: at most one generic
if generic_count != 1 {
self.tcx.emit_node_span_lint(
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
hir_id,
generics.span,
errors::OnTypeErrorNotExactlyOneGeneric { count: generic_count },
);
}
directive.visit_params(&mut |argument_name, span| {
let has_generic = generics.params.iter().any(|p| {
if !matches!(p.kind, GenericParamKind::Lifetime { .. })
&& let ParamName::Plain(name) = p.name
&& name.name == argument_name
{
true
} else {
false
}
});
let is_allowed = argument_name == sym::Expected || argument_name == sym::Found;
if !(has_generic | is_allowed) {
self.tcx.emit_node_span_lint(
MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
hir_id,
span,
errors::OnTypeErrorMalformedFormatLiterals { name: argument_name },
)
}
});
}
}
}
/// Checks if an `#[inline]` is applied to a function or a closure.
fn check_inline(&self, hir_id: HirId, attr_span: Span, kind: &InlineAttr, target: Target) {
match target {

View File

@@ -1267,3 +1267,18 @@ pub(crate) struct GlobOrListDelegationUnusedTargetExpr {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("unknown parameter `{$name}`")]
#[help(r#"expect either a generic argument name, {"`{This}`"}, {"`{Expected}`"} or {"`{Found}`"} as format argument"#)]
pub(crate) struct OnTypeErrorMalformedFormatLiterals {
pub name: Symbol,
}
#[derive(Diagnostic)]
#[diag(
"`#[diagnostic::on_type_error]` only supports exactly one ADT generic parameter, but found `{$count}`"
)]
pub(crate) struct OnTypeErrorNotExactlyOneGeneric {
pub count: usize,
}

View File

@@ -720,6 +720,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
(sym::on_const, Some(sym::diagnostic_on_const)),
(sym::on_unknown, Some(sym::diagnostic_on_unknown)),
(sym::on_unmatch_args, Some(sym::diagnostic_on_unmatch_args)),
(sym::on_type_error, Some(sym::diagnostic_on_type_error)),
];
if res == Res::NonMacroAttr(NonMacroAttrKind::Tool)

View File

@@ -213,6 +213,7 @@ symbols! {
Eq,
Equal,
Err,
Expected,
ExternC,
ExternRust,
Float,
@@ -223,6 +224,7 @@ symbols! {
FnPtr,
Formatter,
Forward,
Found,
From,
FromIterator,
FromResidual,
@@ -811,6 +813,7 @@ symbols! {
diagnostic_namespace,
diagnostic_on_const,
diagnostic_on_move,
diagnostic_on_type_error,
diagnostic_on_unknown,
diagnostic_on_unmatch_args,
dialect,
@@ -1452,6 +1455,7 @@ symbols! {
on,
on_const,
on_move,
on_type_error,
on_unimplemented,
on_unknown,
on_unmatch_args,

View File

@@ -51,12 +51,13 @@ use std::path::PathBuf;
use std::{cmp, fmt, iter};
use rustc_abi::ExternAbi;
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_errors::{Applicability, Diag, DiagStyledString, IntoDiagArg, StringPart, pluralize};
use rustc_hir as hir;
use rustc_hir::attrs::diagnostic::{CustomDiagnostic, Directive, FormatArgs};
use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
use rustc_hir::intravisit::Visitor;
use rustc_hir::lang_items::LangItem;
use rustc_hir::{self as hir, find_attr};
use rustc_infer::infer::DefineOpaqueTypes;
use rustc_macros::extension;
use rustc_middle::bug;
@@ -68,6 +69,7 @@ use rustc_middle::ty::{
TypeVisitableExt, Unnormalized,
};
use rustc_span::{BytePos, DUMMY_SP, DesugaringKind, Pos, Span, sym};
use thin_vec::ThinVec;
use tracing::{debug, instrument};
use crate::error_reporting::TypeErrCtxt;
@@ -1995,6 +1997,88 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
}
fn check_on_type_error_attribute(
&self,
expected_ty: Ty<'tcx>,
found_ty: Ty<'tcx>,
) -> ThinVec<String> {
let mut seen = FxHashSet::default();
let mut unique_notes: ThinVec<String> = ThinVec::new();
// Check found type for attribute
if let ty::Adt(item_def, args) = found_ty.kind() {
if let Some(Some(directive)) =
find_attr!(self.tcx, item_def.did(), OnTypeError { directive, .. } => directive)
{
let notes = self.format_on_type_error_notes(
directive,
args,
item_def.clone(),
expected_ty,
found_ty,
);
for note in notes {
if seen.insert(note.clone()) {
unique_notes.push(note);
}
}
}
}
// Check expected type for attribute
if let ty::Adt(item_def, args) = expected_ty.kind() {
if let Some(Some(directive)) =
find_attr!(self.tcx, item_def.did(), OnTypeError { directive, .. } => directive)
{
let notes = self.format_on_type_error_notes(
directive,
args,
item_def.clone(),
expected_ty,
found_ty,
);
for note in notes {
if seen.insert(note.clone()) {
unique_notes.push(note);
}
}
}
}
unique_notes
}
fn format_on_type_error_notes(
&self,
directive: &Directive,
args: &ty::GenericArgsRef<'tcx>,
item_def: ty::AdtDef<'tcx>,
expected_ty: Ty<'tcx>,
found_ty: Ty<'tcx>,
) -> ThinVec<String> {
let item_name = self.tcx.item_name(item_def.did()).to_string();
let generic_args: Vec<_> = self
.tcx
.generics_of(item_def.did())
.own_params
.iter()
.filter_map(|param| Some((param.name, args[param.index as usize].to_string())))
.collect();
let format_args = FormatArgs {
this: item_name,
generic_args,
found: found_ty.to_string(),
expected: expected_ty.to_string(),
..
};
let CustomDiagnostic { notes, .. } = directive.eval(None, &format_args);
notes.into()
}
pub fn report_and_explain_type_error(
&self,
trace: TypeTrace<'tcx>,
@@ -2005,6 +2089,14 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let span = trace.cause.span;
let mut path = None;
// Check for on_type_error attribute
let on_type_error_notes = if let Some((expected_ty, found_ty)) = trace.values.ty() {
self.check_on_type_error_attribute(expected_ty, found_ty)
} else {
ThinVec::new()
};
let failure_code = trace.cause.as_failure_code_diag(
terr,
span,
@@ -2012,6 +2104,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
);
let mut diag = self.dcx().create_err(failure_code);
*diag.long_ty_path() = path;
// Add custom notes
for note in on_type_error_notes {
diag.note(note);
}
self.note_type_err(
&mut diag,
&trace.cause,

View File

@@ -251,7 +251,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
})
.collect();
let format_args = FormatArgs { this, this_path, this_resolved, generic_args, item_context };
let format_args =
FormatArgs { this, this_path, this_resolved, generic_args, item_context, .. };
(filter_options, format_args)
}
}

View File

@@ -0,0 +1,6 @@
#![feature(diagnostic_on_type_error)]
#[diagnostic::on_type_error(
note = "[TEST] cross crate expected `{Expected}`, found `{Found}`"
)]
pub struct Foo<T>(pub T);

View File

@@ -0,0 +1,5 @@
#![feature(diagnostic_on_type_error)]
#[diagnostic::on_type_error(
note = "custom on_type_error note: expected struct `{Expected}`\n found struct `{Found}`"
)]
pub struct S<T>(pub T);

View File

@@ -0,0 +1,25 @@
//@ aux-build:on-type-error.rs
extern crate on_type_error;
use on_type_error::S;
struct K<T> {
foo: T,
}
fn main() {
let s: S<i32> = S(String::new());
//~^ ERROR mismatched types
//~| NOTE arguments to this struct are incorrect
//~| NOTE expected `i32`, found `String`
//~| NOTE this argument influences the type of `S`
//~| NOTE tuple struct defined here
let k: K<i32> = K { foo: "" };
//~^ ERROR mismatched types
//~| NOTE expected `i32`, found `&str`
let _: S<i32> = k;
//~^ ERROR mismatched types
//~| NOTE expected due to this
//~| NOTE expected `S<i32>`, found `K<i32>`
//~| NOTE custom on_type_error note: expected struct `S<i32>`
//~| NOTE expected struct `S<i32>`
}

View File

@@ -0,0 +1,43 @@
error[E0308]: mismatched types
--> $DIR/error_is_shown_in_downstream_crates.rs:10:23
|
LL | let s: S<i32> = S(String::new());
| - ^^^^^^^^^^^^^ expected `i32`, found `String`
| |
| arguments to this struct are incorrect
|
help: the type constructed contains `String` due to the type of the argument passed
--> $DIR/error_is_shown_in_downstream_crates.rs:10:21
|
LL | let s: S<i32> = S(String::new());
| ^^-------------^
| |
| this argument influences the type of `S`
note: tuple struct defined here
--> $DIR/auxiliary/on-type-error.rs:5:12
|
LL | pub struct S<T>(pub T);
| ^
error[E0308]: mismatched types
--> $DIR/error_is_shown_in_downstream_crates.rs:16:30
|
LL | let k: K<i32> = K { foo: "" };
| ^^ expected `i32`, found `&str`
error[E0308]: mismatched types
--> $DIR/error_is_shown_in_downstream_crates.rs:19:21
|
LL | let _: S<i32> = k;
| ------ ^ expected `S<i32>`, found `K<i32>`
| |
| expected due to this
|
= note: custom on_type_error note: expected struct `S<i32>`
found struct `K<i32>`
= note: expected struct `S<i32>`
found struct `K<i32>`
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0308`.

View File

@@ -0,0 +1,21 @@
#![feature(diagnostic_on_type_error)]
#[diagnostic::on_type_error(
note = "custom on_type_error note: expected `{Expected}`, found `{Found}`"
)]
struct Array<T, const N: usize>([T; N]);
//~^ WARN `#[diagnostic::on_type_error]` only supports exactly one ADT generic parameter, but found `2` [malformed_diagnostic_attributes]
//~| NOTE `#[warn(malformed_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
struct OtherArray<T, const N: usize>([T; N]);
fn main() {
let other: OtherArray<i32, 4> = OtherArray([1, 2, 3, 4]);
let _: Array<i32, 4> = other;
//~^ ERROR mismatched types
//~| NOTE expected due to this
//~| NOTE expected `Array<i32, 4>`, found `OtherArray<i32, 4>`
//~| NOTE custom on_type_error note: expected `Array<i32, 4>`, found `OtherArray<i32, 4>`
//~| NOTE expected struct `Array<i32, 4>`
}

View File

@@ -0,0 +1,23 @@
warning: `#[diagnostic::on_type_error]` only supports exactly one ADT generic parameter, but found `2`
--> $DIR/on-type-error-const-generic.rs:6:13
|
LL | struct Array<T, const N: usize>([T; N]);
| ^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(malformed_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
error[E0308]: mismatched types
--> $DIR/on-type-error-const-generic.rs:15:28
|
LL | let _: Array<i32, 4> = other;
| ------------- ^^^^^ expected `Array<i32, 4>`, found `OtherArray<i32, 4>`
| |
| expected due to this
|
= note: custom on_type_error note: expected `Array<i32, 4>`, found `OtherArray<i32, 4>`
= note: expected struct `Array<i32, 4>`
found struct `OtherArray<i32, 4>`
error: aborting due to 1 previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0308`.

View File

@@ -0,0 +1,19 @@
#![feature(diagnostic_on_type_error)]
#[diagnostic::on_type_error(note = "custom on_type_error note: test coalesce first")]
#[diagnostic::on_type_error(note = "custom on_type_error note: test coalesce second")]
struct S<T>(T);
struct K<T>(T);
fn main() {
let k = K(42);
let _: S<i32> = k;
//~^ ERROR mismatched types
//~| NOTE custom on_type_error note: test coalesce first
//~| NOTE custom on_type_error note: test coalesce second
//~| NOTE expected struct `S<i32>`
//~| NOTE expected due to this
//~| NOTE expected `S<i32>`, found `K<{integer}>`
}

View File

@@ -0,0 +1,16 @@
error[E0308]: mismatched types
--> $DIR/on-type-error-duplicate-attributes.rs:12:21
|
LL | let _: S<i32> = k;
| ------ ^ expected `S<i32>`, found `K<{integer}>`
| |
| expected due to this
|
= note: custom on_type_error note: test coalesce first
= note: custom on_type_error note: test coalesce second
= note: expected struct `S<i32>`
found struct `K<{integer}>`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.

View File

@@ -0,0 +1,13 @@
#![feature(diagnostic_on_move)]
#[diagnostic::on_move(message = "Foo", label = "Bar", note = "{Expected} {Found}")]
//~^ WARN unknown parameter `Expected` [malformed_diagnostic_format_literals]
//~| WARN unknown parameter `Found` [malformed_diagnostic_format_literals]
#[derive(Debug)]
struct Foo;
fn takes_foo(_: Foo) {}
fn main() {
let foo = Foo;
takes_foo(foo);
let bar = foo;
//~^ERROR Foo
}

View File

@@ -0,0 +1,47 @@
warning: unknown parameter `Expected`
--> $DIR/on-type-error-invalid-placeholders.rs:2:64
|
LL | #[diagnostic::on_move(message = "Foo", label = "Bar", note = "{Expected} {Found}")]
| ^^^^^^^^
|
= help: expect either a generic argument name or `{Self}` as format argument
= note: `#[warn(malformed_diagnostic_format_literals)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
warning: unknown parameter `Found`
--> $DIR/on-type-error-invalid-placeholders.rs:2:75
|
LL | #[diagnostic::on_move(message = "Foo", label = "Bar", note = "{Expected} {Found}")]
| ^^^^^
|
= help: expect either a generic argument name or `{Self}` as format argument
error[E0382]: Foo
--> $DIR/on-type-error-invalid-placeholders.rs:11:15
|
LL | let foo = Foo;
| --- Bar
LL | takes_foo(foo);
| --- value moved here
LL | let bar = foo;
| ^^^ value used here after move
|
= note: {Expected} {Found}
note: consider changing this parameter type in function `takes_foo` to borrow instead if owning the value isn't necessary
--> $DIR/on-type-error-invalid-placeholders.rs:7:17
|
LL | fn takes_foo(_: Foo) {}
| --------- ^^^ this parameter takes ownership of the value
| |
| in this function
note: if `Foo` implemented `Clone`, you could clone the value
--> $DIR/on-type-error-invalid-placeholders.rs:6:1
|
LL | struct Foo;
| ^^^^^^^^^^ consider implementing `Clone` for this type
...
LL | takes_foo(foo);
| --- you could clone this value
error: aborting due to 1 previous error; 2 warnings emitted
For more information about this error, try `rustc --explain E0382`.

View File

@@ -0,0 +1,20 @@
#![feature(diagnostic_on_type_error)]
#[diagnostic::on_type_error(
note = "custom on_type_error note: expected `{Expected}`, found `{Found}`"
)]
struct Ref<'a, T>(&'a T);
struct OtherRef<'a, T>(&'a T);
fn main() {
let value = 42;
let other: OtherRef<'_, i32> = OtherRef(&value);
let _: Ref<'_, i32> = other;
//~^ ERROR mismatched types
//~| NOTE expected struct `Ref<'_, i32>`
//~| NOTE expected due to this
//~| NOTE expected `Ref<'_, i32>`, found `OtherRef<'_, i32>`
//~| NOTE custom on_type_error note: expected `Ref<'_, i32>`, found `OtherRef<'_, i32>`
}

View File

@@ -0,0 +1,15 @@
error[E0308]: mismatched types
--> $DIR/on-type-error-lifetime-generic.rs:14:27
|
LL | let _: Ref<'_, i32> = other;
| ------------ ^^^^^ expected `Ref<'_, i32>`, found `OtherRef<'_, i32>`
| |
| expected due to this
|
= note: custom on_type_error note: expected `Ref<'_, i32>`, found `OtherRef<'_, i32>`
= note: expected struct `Ref<'_, i32>`
found struct `OtherRef<'_, i32>`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.

View File

@@ -0,0 +1,17 @@
#![feature(diagnostic_on_type_error)]
#[diagnostic::on_type_error(
note = "custom on_type_error note: expected enum `{This}<{T}>`, found `{Found}`"
)]
enum MyEnum<T> {
Variant(T),
}
fn main() {
let e: MyEnum<&str> = "hello";
//~^ ERROR mismatched types
//~| NOTE expected `MyEnum<&str>`, found `&str`
//~| NOTE custom on_type_error note: expected enum `MyEnum<&str>`, found `&'static str
//~| NOTE expected due to this
//~| NOTE expected enum `MyEnum<&str>`
}

View File

@@ -0,0 +1,19 @@
error[E0308]: mismatched types
--> $DIR/on_type_error_enum.rs:11:27
|
LL | let e: MyEnum<&str> = "hello";
| ------------ ^^^^^^^ expected `MyEnum<&str>`, found `&str`
| |
| expected due to this
|
= note: custom on_type_error note: expected enum `MyEnum<&str>`, found `&'static str`
= note: expected enum `MyEnum<&str>`
found reference `&'static str`
help: try wrapping the expression in `MyEnum::Variant`
|
LL | let e: MyEnum<&str> = MyEnum::Variant("hello");
| ++++++++++++++++ +
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.

View File

@@ -0,0 +1,25 @@
#![feature(diagnostic_on_type_error)]
#[diagnostic::on_type_error(
note = "custom on_type_error note: expected struct `{Expected}`\n found struct `{Found}`"
)]
struct S<T>(T);
//~^ NOTE tuple struct defined here
struct K<T> {
foo: T,
}
fn main() {
let s: S<i32> = S(String::new());
//~^ ERROR mismatched types
//~| NOTE arguments to this struct are incorrect
//~| NOTE expected `i32`, found `String`
//~| NOTE this argument influences the type of `S`
let k: K<i32> = K { foo: "" };
//~^ ERROR mismatched types
//~| NOTE expected `i32`, found `&str`
let _: S<i32> = k;
//~^ ERROR mismatched types
//~| NOTE expected due to this
//~| NOTE expected `S<i32>`, found `K<i32>`
//~| NOTE custom on_type_error note: expected struct `S<i32>`
//~| NOTE expected struct `S<i32>`
}

View File

@@ -0,0 +1,43 @@
error[E0308]: mismatched types
--> $DIR/on_type_error_simple.rs:11:23
|
LL | let s: S<i32> = S(String::new());
| - ^^^^^^^^^^^^^ expected `i32`, found `String`
| |
| arguments to this struct are incorrect
|
help: the type constructed contains `String` due to the type of the argument passed
--> $DIR/on_type_error_simple.rs:11:21
|
LL | let s: S<i32> = S(String::new());
| ^^-------------^
| |
| this argument influences the type of `S`
note: tuple struct defined here
--> $DIR/on_type_error_simple.rs:5:8
|
LL | struct S<T>(T);
| ^
error[E0308]: mismatched types
--> $DIR/on_type_error_simple.rs:16:30
|
LL | let k: K<i32> = K { foo: "" };
| ^^ expected `i32`, found `&str`
error[E0308]: mismatched types
--> $DIR/on_type_error_simple.rs:19:21
|
LL | let _: S<i32> = k;
| ------ ^ expected `S<i32>`, found `K<i32>`
| |
| expected due to this
|
= note: custom on_type_error note: expected struct `S<i32>`
found struct `K<i32>`
= note: expected struct `S<i32>`
found struct `K<i32>`
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0308`.

View File

@@ -0,0 +1,19 @@
#![feature(diagnostic_on_type_error)]
#[diagnostic::on_type_error(
note = "custom on_type_error note: expected union `{Expected}`, found `{Found}`"
)]
union MyUnion<T> {
value: std::mem::ManuallyDrop<T>,
}
fn takes_wrapper(_: MyUnion<i32>) {}
fn main() {
let u1: MyUnion<i32> = 32;
//~^ ERROR mismatched types
//~| NOTE expected due to this
//~| NOTE expected `MyUnion<i32>`, found integer
//~| NOTE custom on_type_error note: expected union `MyUnion<i32>`, found `{integer}`
//~| NOTE expected union `MyUnion<i32>`
}

View File

@@ -0,0 +1,15 @@
error[E0308]: mismatched types
--> $DIR/on_type_error_union.rs:13:28
|
LL | let u1: MyUnion<i32> = 32;
| ------------ ^^ expected `MyUnion<i32>`, found integer
| |
| expected due to this
|
= note: custom on_type_error note: expected union `MyUnion<i32>`, found `{integer}`
= note: expected union `MyUnion<i32>`
found type `{integer}`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.

View File

@@ -0,0 +1,10 @@
#![feature(diagnostic_on_type_error)]
#[diagnostic::on_type_error(note = "custom on_type_error note: too many generics")]
struct TooMany<T, U>(T, U);
//~^ WARN `#[diagnostic::on_type_error]` only supports exactly one ADT generic parameter, but found `2`
fn main() {
let _: TooMany<i32, i32> = TooMany(32, "test");
//~^ ERROR mismatched types
}

View File

@@ -0,0 +1,32 @@
warning: `#[diagnostic::on_type_error]` only supports exactly one ADT generic parameter, but found `2`
--> $DIR/on_type_error_with_too_many_generics.rs:4:15
|
LL | struct TooMany<T, U>(T, U);
| ^^^^^^
|
= note: `#[warn(malformed_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
error[E0308]: mismatched types
--> $DIR/on_type_error_with_too_many_generics.rs:8:44
|
LL | let _: TooMany<i32, i32> = TooMany(32, "test");
| ------- ^^^^^^ expected `i32`, found `&str`
| |
| arguments to this struct are incorrect
|
help: the type constructed contains `&'static str` due to the type of the argument passed
--> $DIR/on_type_error_with_too_many_generics.rs:8:32
|
LL | let _: TooMany<i32, i32> = TooMany(32, "test");
| ^^^^^^^^^^^^------^
| |
| this argument influences the type of `TooMany`
note: tuple struct defined here
--> $DIR/on_type_error_with_too_many_generics.rs:4:8
|
LL | struct TooMany<T, U>(T, U);
| ^^^^^^^
error: aborting due to 1 previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0308`.

View File

@@ -0,0 +1,28 @@
#![feature(diagnostic_on_type_error)]
#[diagnostic::on_type_error(note = "custom on_type_error note: invalid format {Expected:123}")]
//~^ WARN format specifiers are not permitted in diagnostic attributes
//~| NOTE `#[warn(malformed_diagnostic_format_literals)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
//~| NOTE remove this format specifier
struct InvalidFormat1<T>(T);
#[diagnostic::on_type_error(note = "custom on_type_error note: invalid format {Expected:!}")]
//~^ WARN format specifiers are not permitted in diagnostic attributes
//~| NOTE remove this format specifier
struct InvalidFormat2<T>(T);
fn main() {
// Create type errors to trigger the notes
let _: InvalidFormat1<i32> = InvalidFormat2("");
//~^ ERROR mismatched types
//~| NOTE expected due to this
//~| NOTE expected `InvalidFormat1<i32>`, found `InvalidFormat2<&str>`
//~| NOTE custom on_type_error note: invalid format InvalidFormat1<i32>
//~| NOTE expected struct `InvalidFormat1<i32>`
let _: InvalidFormat2<i32> = InvalidFormat1(3.14);
//~^ ERROR mismatched types
//~| NOTE expected due to this
//~| NOTE expected `InvalidFormat2<i32>`, found `InvalidFormat1<{float}>`
//~| NOTE custom on_type_error note: invalid format InvalidFormat2<i32>
//~| NOTE expected struct `InvalidFormat2<i32>`
}

View File

@@ -0,0 +1,41 @@
warning: format specifiers are not permitted in diagnostic attributes
--> $DIR/report_warning_on_invalid_formats.rs:3:88
|
LL | #[diagnostic::on_type_error(note = "custom on_type_error note: invalid format {Expected:123}")]
| ^^^^ remove this format specifier
|
= note: `#[warn(malformed_diagnostic_format_literals)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
warning: format specifiers are not permitted in diagnostic attributes
--> $DIR/report_warning_on_invalid_formats.rs:9:88
|
LL | #[diagnostic::on_type_error(note = "custom on_type_error note: invalid format {Expected:!}")]
| ^^ remove this format specifier
error[E0308]: mismatched types
--> $DIR/report_warning_on_invalid_formats.rs:16:34
|
LL | let _: InvalidFormat1<i32> = InvalidFormat2("");
| ------------------- ^^^^^^^^^^^^^^^^^^ expected `InvalidFormat1<i32>`, found `InvalidFormat2<&str>`
| |
| expected due to this
|
= note: custom on_type_error note: invalid format InvalidFormat1<i32>
= note: expected struct `InvalidFormat1<i32>`
found struct `InvalidFormat2<&str>`
error[E0308]: mismatched types
--> $DIR/report_warning_on_invalid_formats.rs:22:34
|
LL | let _: InvalidFormat2<i32> = InvalidFormat1(3.14);
| ------------------- ^^^^^^^^^^^^^^^^^^^^ expected `InvalidFormat2<i32>`, found `InvalidFormat1<{float}>`
| |
| expected due to this
|
= note: custom on_type_error note: invalid format InvalidFormat2<i32>
= note: expected struct `InvalidFormat2<i32>`
found struct `InvalidFormat1<{float}>`
error: aborting due to 2 previous errors; 2 warnings emitted
For more information about this error, try `rustc --explain E0308`.

View File

@@ -0,0 +1,25 @@
#![feature(diagnostic_on_type_error)]
#[diagnostic::on_type_error(note)]
//~^ WARN malformed `diagnostic::on_type_error` attribute
//~| NOTE `#[warn(malformed_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
//~| NOTE invalid option found here
struct NoValue<T>(T);
#[diagnostic::on_type_error(note =)]
//~^ WARN expected a literal or missing delimiter
struct EmptyValue<T>(T);
fn main() {
// Create type errors
let _: NoValue<i32> = 43;
//~^ ERROR mismatched types
//~| NOTE expected due to this
//~| NOTE expected `NoValue<i32>`, found integer
//~| NOTE expected struct `NoValue<i32>`
let _: EmptyValue<i32> = 44;
//~^ ERROR mismatched types
//~| NOTE expected due to this
//~| NOTE expected `EmptyValue<i32>`, found integer
//~| NOTE expected struct `EmptyValue<i32>`
}

View File

@@ -0,0 +1,50 @@
warning: malformed `diagnostic::on_type_error` attribute
--> $DIR/report_warning_on_invalid_meta_item_syntax.rs:3:29
|
LL | #[diagnostic::on_type_error(note)]
| ^^^^ invalid option found here
|
= help: only `note` is allowed as option for `diagnostic::on_type_error`
= note: `#[warn(malformed_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
warning: expected a literal or missing delimiter
--> $DIR/report_warning_on_invalid_meta_item_syntax.rs:9:28
|
LL | #[diagnostic::on_type_error(note =)]
| ^^^^^^^^
|
= help: only literals are allowed as values for the `message`, `note` and `label` options. These options must be separated by a comma
error[E0308]: mismatched types
--> $DIR/report_warning_on_invalid_meta_item_syntax.rs:15:27
|
LL | let _: NoValue<i32> = 43;
| ------------ ^^ expected `NoValue<i32>`, found integer
| |
| expected due to this
|
= note: expected struct `NoValue<i32>`
found type `{integer}`
help: try wrapping the expression in `NoValue`
|
LL | let _: NoValue<i32> = NoValue(43);
| ++++++++ +
error[E0308]: mismatched types
--> $DIR/report_warning_on_invalid_meta_item_syntax.rs:20:30
|
LL | let _: EmptyValue<i32> = 44;
| --------------- ^^ expected `EmptyValue<i32>`, found integer
| |
| expected due to this
|
= note: expected struct `EmptyValue<i32>`
found type `{integer}`
help: try wrapping the expression in `EmptyValue`
|
LL | let _: EmptyValue<i32> = EmptyValue(44);
| +++++++++++ +
error: aborting due to 2 previous errors; 2 warnings emitted
For more information about this error, try `rustc --explain E0308`.

View File

@@ -0,0 +1,15 @@
#![feature(diagnostic_on_type_error)]
#[diagnostic::on_type_error]
//~^ WARN missing options for `diagnostic::on_type_error` attribute
//~| NOTE `#[warn(malformed_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
struct MissingOptions<T>(T);
fn main() {
// Create a type error
let _: MissingOptions<i32> = 32;
//~^ ERROR mismatched types
//~| NOTE expected due to this
//~| NOTE expected `MissingOptions<i32>`, found integer
//~| NOTE expected struct `MissingOptions<i32>`
}

View File

@@ -0,0 +1,27 @@
warning: missing options for `diagnostic::on_type_error` attribute
--> $DIR/report_warning_on_missing_options.rs:3:1
|
LL | #[diagnostic::on_type_error]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: at least a single `note` option is expected
= note: `#[warn(malformed_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
error[E0308]: mismatched types
--> $DIR/report_warning_on_missing_options.rs:10:34
|
LL | let _: MissingOptions<i32> = 32;
| ------------------- ^^ expected `MissingOptions<i32>`, found integer
| |
| expected due to this
|
= note: expected struct `MissingOptions<i32>`
found type `{integer}`
help: try wrapping the expression in `MissingOptions`
|
LL | let _: MissingOptions<i32> = MissingOptions(32);
| +++++++++++++++ +
error: aborting due to 1 previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0308`.

View File

@@ -0,0 +1,40 @@
#![feature(diagnostic_on_type_error)]
#[diagnostic::on_type_error(note = "custom on_type_error note: not an ADT")]
//~^ WARN `#[diagnostic::on_type_error]` can only be applied to enums, structs or unions
fn function() {}
#[diagnostic::on_type_error(note = "custom on_type_error note: not an ADT")]
//~^ WARN `#[diagnostic::on_type_error]` can only be applied to enums, structs or unions
static STATIC: i32 = 0;
#[diagnostic::on_type_error(note = "custom on_type_error note: not an ADT")]
//~^ WARN `#[diagnostic::on_type_error]` can only be applied to enums, structs or unions
mod module {}
#[diagnostic::on_type_error(note = "custom on_type_error note: not an ADT")]
//~^ WARN `#[diagnostic::on_type_error]` can only be applied to enums, structs or unions
trait Trait {}
#[diagnostic::on_type_error(note = "custom on_type_error note: not an ADT")]
//~^ WARN `#[diagnostic::on_type_error]` can only be applied to enums, structs or unions
type TypeAlias = i32;
struct SomeStruct;
impl SomeStruct {
#[diagnostic::on_type_error(note = "custom on_type_error note: not an ADT")]
//~^ WARN `#[diagnostic::on_type_error]` can only be applied to enums, structs or unions
fn method() {}
}
fn main() {
// Create a type error with a valid ADT to ensure the feature works
#[diagnostic::on_type_error(
note = "custom on_type_error note: expected `{Expected}`, found `{Found}`"
)]
struct Valid<T>(T);
let _: Valid<i32> = SomeStruct;
//~^ ERROR mismatched types
}

View File

@@ -0,0 +1,53 @@
warning: `#[diagnostic::on_type_error]` can only be applied to enums, structs or unions
--> $DIR/report_warning_on_non_adt.rs:3:1
|
LL | #[diagnostic::on_type_error(note = "custom on_type_error note: not an ADT")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(misplaced_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
warning: `#[diagnostic::on_type_error]` can only be applied to enums, structs or unions
--> $DIR/report_warning_on_non_adt.rs:7:1
|
LL | #[diagnostic::on_type_error(note = "custom on_type_error note: not an ADT")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: `#[diagnostic::on_type_error]` can only be applied to enums, structs or unions
--> $DIR/report_warning_on_non_adt.rs:11:1
|
LL | #[diagnostic::on_type_error(note = "custom on_type_error note: not an ADT")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: `#[diagnostic::on_type_error]` can only be applied to enums, structs or unions
--> $DIR/report_warning_on_non_adt.rs:15:1
|
LL | #[diagnostic::on_type_error(note = "custom on_type_error note: not an ADT")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: `#[diagnostic::on_type_error]` can only be applied to enums, structs or unions
--> $DIR/report_warning_on_non_adt.rs:19:1
|
LL | #[diagnostic::on_type_error(note = "custom on_type_error note: not an ADT")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: `#[diagnostic::on_type_error]` can only be applied to enums, structs or unions
--> $DIR/report_warning_on_non_adt.rs:26:5
|
LL | #[diagnostic::on_type_error(note = "custom on_type_error note: not an ADT")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0308]: mismatched types
--> $DIR/report_warning_on_non_adt.rs:38:25
|
LL | let _: Valid<i32> = SomeStruct;
| ---------- ^^^^^^^^^^ expected `Valid<i32>`, found `SomeStruct`
| |
| expected due to this
|
= note: custom on_type_error note: expected `Valid<i32>`, found `SomeStruct`
= note: expected struct `Valid<i32>`
found struct `SomeStruct`
error: aborting due to 1 previous error; 6 warnings emitted
For more information about this error, try `rustc --explain E0308`.

View File

@@ -0,0 +1,23 @@
#![feature(diagnostic_on_type_error)]
#[diagnostic::on_type_error(unknown = "custom on_type_error note: option")]
//~^ WARN malformed `diagnostic::on_type_error` attribute
struct UnknownOption<T>(T);
#[diagnostic::on_type_error(message = "custom on_type_error note: not allowed")]
//~^ WARN malformed `diagnostic::on_type_error` attribute
struct MessageOption<T>(T);
#[diagnostic::on_type_error(label = "custom on_type_error note: not allowed")]
//~^ WARN malformed `diagnostic::on_type_error` attribute
struct LabelOption<T>(T);
fn main() {
// Create type errors
let _: UnknownOption<i32> = MessageOption(43);
//~^ ERROR mismatched types
let _: MessageOption<i32> = UnknownOption(32);
//~^ ERROR mismatched types
let _: LabelOption<i32> = UnknownOption(32);
//~^ ERROR mismatched types
}

View File

@@ -0,0 +1,61 @@
warning: malformed `diagnostic::on_type_error` attribute
--> $DIR/report_warning_on_unknown_options.rs:3:29
|
LL | #[diagnostic::on_type_error(unknown = "custom on_type_error note: option")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid option found here
|
= help: only `note` is allowed as option for `diagnostic::on_type_error`
= note: `#[warn(malformed_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
warning: malformed `diagnostic::on_type_error` attribute
--> $DIR/report_warning_on_unknown_options.rs:7:29
|
LL | #[diagnostic::on_type_error(message = "custom on_type_error note: not allowed")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid option found here
|
= help: only `note` is allowed as option for `diagnostic::on_type_error`
warning: malformed `diagnostic::on_type_error` attribute
--> $DIR/report_warning_on_unknown_options.rs:11:29
|
LL | #[diagnostic::on_type_error(label = "custom on_type_error note: not allowed")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid option found here
|
= help: only `note` is allowed as option for `diagnostic::on_type_error`
error[E0308]: mismatched types
--> $DIR/report_warning_on_unknown_options.rs:17:33
|
LL | let _: UnknownOption<i32> = MessageOption(43);
| ------------------ ^^^^^^^^^^^^^^^^^ expected `UnknownOption<i32>`, found `MessageOption<{integer}>`
| |
| expected due to this
|
= note: expected struct `UnknownOption<i32>`
found struct `MessageOption<{integer}>`
error[E0308]: mismatched types
--> $DIR/report_warning_on_unknown_options.rs:19:33
|
LL | let _: MessageOption<i32> = UnknownOption(32);
| ------------------ ^^^^^^^^^^^^^^^^^ expected `MessageOption<i32>`, found `UnknownOption<{integer}>`
| |
| expected due to this
|
= note: expected struct `MessageOption<i32>`
found struct `UnknownOption<{integer}>`
error[E0308]: mismatched types
--> $DIR/report_warning_on_unknown_options.rs:21:31
|
LL | let _: LabelOption<i32> = UnknownOption(32);
| ---------------- ^^^^^^^^^^^^^^^^^ expected `LabelOption<i32>`, found `UnknownOption<{integer}>`
| |
| expected due to this
|
= note: expected struct `LabelOption<i32>`
found struct `UnknownOption<{integer}>`
error: aborting due to 3 previous errors; 3 warnings emitted
For more information about this error, try `rustc --explain E0308`.

View File

@@ -0,0 +1,19 @@
//! Test that the feature gate is required for diagnostic_on_type_error
#[diagnostic::on_type_error(note = "custom on_type_error note: expected {Expected}, found {Found}")]
//~^ WARN unknown diagnostic attribute
//~| NOTE `#[warn(unknown_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
#[derive(Debug)]
struct Foo<T>(T);
fn takes_foo(_: Foo<i32>) {}
//~^ NOTE function defined here
fn main() {
let foo = Foo(String::new());
takes_foo(foo);
//~^ERROR mismatched types
//~| NOTE arguments to this function are incorrect
//~| NOTE expected `Foo<i32>`, found `Foo<String>`
//~| NOTE expected struct `Foo<i32>`
}

View File

@@ -0,0 +1,28 @@
warning: unknown diagnostic attribute
--> $DIR/feature-gate-diagnostic-on-type-error.rs:3:15
|
LL | #[diagnostic::on_type_error(note = "custom on_type_error note: expected {Expected}, found {Found}")]
| ^^^^^^^^^^^^^
|
= help: add `#![feature(diagnostic_on_type_error)]` to the crate attributes to enable
= note: `#[warn(unknown_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
error[E0308]: mismatched types
--> $DIR/feature-gate-diagnostic-on-type-error.rs:14:15
|
LL | takes_foo(foo);
| --------- ^^^ expected `Foo<i32>`, found `Foo<String>`
| |
| arguments to this function are incorrect
|
= note: expected struct `Foo<i32>`
found struct `Foo<String>`
note: function defined here
--> $DIR/feature-gate-diagnostic-on-type-error.rs:9:4
|
LL | fn takes_foo(_: Foo<i32>) {}
| ^^^^^^^^^ -----------
error: aborting due to 1 previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0308`.