rustc_trait_selection/error_reporting/traits/
on_unimplemented.rs1use std::path::PathBuf;
2
3use rustc_hir as hir;
4use rustc_hir::attrs::diagnostic::{CustomDiagnostic, FilterOptions, FormatArgs};
5use rustc_hir::def_id::LocalDefId;
6use rustc_hir::find_attr;
7use rustc_middle::ty::print::PrintTraitRefExt;
8use rustc_middle::ty::{self, GenericParamDef, GenericParamDefKind};
9use rustc_span::Symbol;
10
11use super::{ObligationCauseCode, PredicateObligation};
12use crate::error_reporting::TypeErrCtxt;
13
14impl<'tcx> TypeErrCtxt<'_, 'tcx> {
15 fn describe_enclosure(&self, def_id: LocalDefId) -> Option<&'static str> {
18 match self.tcx.hir_node_by_def_id(def_id) {
19 hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn { .. }, .. }) => Some("a function"),
20 hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(..), .. }) => {
21 Some("a trait method")
22 }
23 hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) => {
24 Some("a method")
25 }
26 hir::Node::Expr(hir::Expr {
27 kind: hir::ExprKind::Closure(hir::Closure { kind, .. }),
28 ..
29 }) => Some(self.describe_closure(*kind)),
30 _ => None,
31 }
32 }
33
34 pub fn on_unimplemented_note(
35 &self,
36 trait_pred: ty::PolyTraitPredicate<'tcx>,
37 obligation: &PredicateObligation<'tcx>,
38 long_ty_path: &mut Option<PathBuf>,
39 ) -> CustomDiagnostic {
40 if trait_pred.polarity() != ty::PredicatePolarity::Positive {
41 return CustomDiagnostic::default();
42 }
43 let (filter_options, format_args) =
44 self.on_unimplemented_components(trait_pred, obligation, long_ty_path);
45 if let Some(command) = {
{
'done:
{
for i in
::rustc_hir::attrs::HasAttrs::get_attrs(trait_pred.def_id(),
&self.tcx) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(OnUnimplemented { directive, ..
}) => {
break 'done Some(directive.as_deref());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}find_attr!(self.tcx, trait_pred.def_id(), OnUnimplemented {directive, ..} => directive.as_deref()).flatten() {
46 command.eval(
47 Some(&filter_options),
48 &format_args,
49 )
50 } else {
51 CustomDiagnostic::default()
52 }
53 }
54
55 pub(crate) fn on_unimplemented_components(
56 &self,
57 trait_pred: ty::PolyTraitPredicate<'tcx>,
58 obligation: &PredicateObligation<'tcx>,
59 long_ty_path: &mut Option<PathBuf>,
60 ) -> (FilterOptions, FormatArgs) {
61 let (def_id, args) = (trait_pred.def_id(), trait_pred.skip_binder().trait_ref.args);
62 let trait_pred = trait_pred.skip_binder();
63
64 let mut self_types = ::alloc::vec::Vec::new()vec![];
65 let mut generic_args: Vec<(Symbol, String)> = ::alloc::vec::Vec::new()vec![];
66 let mut crate_local = false;
67 let item_context = self.describe_enclosure(obligation.cause.body_id).unwrap_or("");
71
72 let direct = match obligation.cause.code() {
73 ObligationCauseCode::BuiltinDerived(..)
74 | ObligationCauseCode::ImplDerived(..)
75 | ObligationCauseCode::WellFormedDerived(..) => false,
76 _ => {
77 true
80 }
81 };
82
83 let from_desugaring = obligation.cause.span.desugaring_kind();
84
85 let cause = if let ObligationCauseCode::MainFunctionType = obligation.cause.code() {
86 Some("MainFunctionType".to_string())
87 } else {
88 None
89 };
90
91 {
let _guard = NoTrimmedGuard::new();
{
let _guard = NoVisibleGuard::new();
{
let generics = self.tcx.generics_of(def_id);
let self_ty = trait_pred.self_ty();
self_types.push(self_ty.to_string());
if let Some(def) = self_ty.ty_adt_def() {
self_types.push(self.tcx.type_of(def.did()).instantiate_identity().skip_norm_wip().to_string());
}
for GenericParamDef { name, kind, index, .. } in
generics.own_params.iter() {
let value =
match kind {
GenericParamDefKind::Type { .. } |
GenericParamDefKind::Const { .. } => {
args[*index as usize].to_string()
}
GenericParamDefKind::Lifetime => continue,
};
generic_args.push((*name, value));
if let GenericParamDefKind::Type { .. } = kind {
let param_ty = args[*index as usize].expect_ty();
if let Some(def) = param_ty.ty_adt_def() {
generic_args.push((*name,
self.tcx.type_of(def.did()).instantiate_identity().skip_norm_wip().to_string()));
}
}
}
if let Some(adt) = self_ty.ty_adt_def() {
if adt.did().is_local() { crate_local = true; }
self_types.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{{{0}}}", adt.descr()))
}))
}
if self_ty.is_integral() {
self_types.push("{integral}".to_owned());
}
if self_ty.is_array_slice() { self_types.push("&[]".to_owned()); }
if self_ty.is_fn() {
let fn_sig = self_ty.fn_sig(self.tcx);
let shortname =
if let ty::FnDef(def_id, _) = *self_ty.kind() &&
self.tcx.codegen_fn_attrs(def_id).safe_target_features {
"#[target_feature] fn"
} else {
match fn_sig.safety() {
hir::Safety::Safe => "fn",
hir::Safety::Unsafe => "unsafe fn",
}
};
self_types.push(shortname.to_owned());
}
if let ty::Slice(aty) = self_ty.kind() {
self_types.push("[]".to_owned());
if let Some(def) = aty.ty_adt_def() {
self_types.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("[{0}]",
self.tcx.type_of(def.did()).instantiate_identity().skip_norm_wip()))
}));
}
if aty.is_integral() {
self_types.push("[{integral}]".to_string());
}
}
if let ty::Array(aty, len) = self_ty.kind() {
self_types.push("[]".to_string());
let len = len.try_to_target_usize(self.tcx);
self_types.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("[{0}; _]", aty))
}));
if let Some(n) = len {
self_types.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("[{0}; {1}]", aty, n))
}));
}
if let Some(def) = aty.ty_adt_def() {
let def_ty =
self.tcx.type_of(def.did()).instantiate_identity().skip_norm_wip();
self_types.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("[{0}; _]", def_ty))
}));
if let Some(n) = len {
self_types.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("[{0}; {1}]", def_ty, n))
}));
}
}
if aty.is_integral() {
self_types.push("[{integral}; _]".to_string());
if let Some(n) = len {
self_types.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("[{{integral}}; {0}]", n))
}));
}
}
}
if let ty::Dynamic(traits, _) = self_ty.kind() {
for t in traits.iter() {
if let ty::ExistentialPredicate::Trait(trait_ref) =
t.skip_binder() {
self_types.push(self.tcx.def_path_str(trait_ref.def_id));
}
}
}
if let ty::Ref(_, ref_ty, rustc_ast::Mutability::Not) =
self_ty.kind() && let ty::Slice(sty) = ref_ty.kind() &&
sty.is_integral() {
self_types.push("&[{integral}]".to_owned());
}
}
}
};ty::print::with_no_trimmed_paths!(ty::print::with_no_visible_paths!({
94 let generics = self.tcx.generics_of(def_id);
95 let self_ty = trait_pred.self_ty();
96 self_types.push(self_ty.to_string());
97 if let Some(def) = self_ty.ty_adt_def() {
98 self_types.push(
101 self.tcx.type_of(def.did()).instantiate_identity().skip_norm_wip().to_string(),
102 );
103 }
104
105 for GenericParamDef { name, kind, index, .. } in generics.own_params.iter() {
106 let value = match kind {
107 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
108 args[*index as usize].to_string()
109 }
110 GenericParamDefKind::Lifetime => continue,
111 };
112 generic_args.push((*name, value));
113
114 if let GenericParamDefKind::Type { .. } = kind {
115 let param_ty = args[*index as usize].expect_ty();
116 if let Some(def) = param_ty.ty_adt_def() {
117 generic_args.push((
120 *name,
121 self.tcx
122 .type_of(def.did())
123 .instantiate_identity()
124 .skip_norm_wip()
125 .to_string(),
126 ));
127 }
128 }
129 }
130
131 if let Some(adt) = self_ty.ty_adt_def() {
132 if adt.did().is_local() {
133 crate_local = true;
134 }
135 self_types.push(format!("{{{}}}", adt.descr()))
136 }
137
138 if self_ty.is_integral() {
140 self_types.push("{integral}".to_owned());
141 }
142
143 if self_ty.is_array_slice() {
144 self_types.push("&[]".to_owned());
145 }
146
147 if self_ty.is_fn() {
148 let fn_sig = self_ty.fn_sig(self.tcx);
149 let shortname = if let ty::FnDef(def_id, _) = *self_ty.kind()
150 && self.tcx.codegen_fn_attrs(def_id).safe_target_features
151 {
152 "#[target_feature] fn"
153 } else {
154 match fn_sig.safety() {
155 hir::Safety::Safe => "fn",
156 hir::Safety::Unsafe => "unsafe fn",
157 }
158 };
159 self_types.push(shortname.to_owned());
160 }
161
162 if let ty::Slice(aty) = self_ty.kind() {
164 self_types.push("[]".to_owned());
165 if let Some(def) = aty.ty_adt_def() {
166 self_types.push(format!(
169 "[{}]",
170 self.tcx.type_of(def.did()).instantiate_identity().skip_norm_wip()
171 ));
172 }
173 if aty.is_integral() {
174 self_types.push("[{integral}]".to_string());
175 }
176 }
177
178 if let ty::Array(aty, len) = self_ty.kind() {
180 self_types.push("[]".to_string());
181 let len = len.try_to_target_usize(self.tcx);
182 self_types.push(format!("[{aty}; _]"));
183 if let Some(n) = len {
184 self_types.push(format!("[{aty}; {n}]"));
185 }
186 if let Some(def) = aty.ty_adt_def() {
187 let def_ty = self.tcx.type_of(def.did()).instantiate_identity().skip_norm_wip();
190 self_types.push(format!("[{def_ty}; _]"));
191 if let Some(n) = len {
192 self_types.push(format!("[{def_ty}; {n}]"));
193 }
194 }
195 if aty.is_integral() {
196 self_types.push("[{integral}; _]".to_string());
197 if let Some(n) = len {
198 self_types.push(format!("[{{integral}}; {n}]"));
199 }
200 }
201 }
202 if let ty::Dynamic(traits, _) = self_ty.kind() {
203 for t in traits.iter() {
204 if let ty::ExistentialPredicate::Trait(trait_ref) = t.skip_binder() {
205 self_types.push(self.tcx.def_path_str(trait_ref.def_id));
206 }
207 }
208 }
209
210 if let ty::Ref(_, ref_ty, rustc_ast::Mutability::Not) = self_ty.kind()
212 && let ty::Slice(sty) = ref_ty.kind()
213 && sty.is_integral()
214 {
215 self_types.push("&[{integral}]".to_owned());
216 }
217 }));
218
219 let this = self.tcx.def_path_str(trait_pred.trait_ref.def_id);
220 let this_sugared = trait_pred.trait_ref.print_trait_sugared().to_string();
221
222 let filter_options =
223 FilterOptions { self_types, from_desugaring, cause, crate_local, direct, generic_args };
224
225 let generic_args = self
231 .tcx
232 .generics_of(trait_pred.trait_ref.def_id)
233 .own_params
234 .iter()
235 .filter_map(|param| {
236 let value = match param.kind {
237 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
238 if let Some(ty) = trait_pred.trait_ref.args[param.index as usize].as_type()
239 {
240 self.tcx.short_string(ty, long_ty_path)
241 } else {
242 trait_pred.trait_ref.args[param.index as usize].to_string()
243 }
244 }
245 GenericParamDefKind::Lifetime => return None,
246 };
247 let name = param.name;
248 Some((name, value))
249 })
250 .collect();
251
252 let format_args = FormatArgs { this, this_sugared, generic_args, item_context };
253 (filter_options, format_args)
254 }
255}