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