rustc_hir_analysis/check/
always_applicable.rs1use rustc_data_structures::fx::FxHashSet;
8use rustc_errors::codes::*;
9use rustc_errors::{ErrorGuaranteed, struct_span_code_err};
10use rustc_infer::infer::{RegionResolutionError, TyCtxtInferExt};
11use rustc_infer::traits::{ObligationCause, ObligationCauseCode};
12use rustc_middle::span_bug;
13use rustc_middle::ty::util::CheckRegions;
14use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, TypeVisitableExt, TypingMode};
15use rustc_trait_selection::regions::InferCtxtRegionExt;
16use rustc_trait_selection::traits::{self, ObligationCtxt};
17
18use crate::errors;
19use crate::hir::def_id::{DefId, LocalDefId};
20
21pub(crate) fn check_drop_impl(
38 tcx: TyCtxt<'_>,
39 drop_impl_did: LocalDefId,
40) -> Result<(), ErrorGuaranteed> {
41 match tcx.impl_polarity(drop_impl_did) {
42 ty::ImplPolarity::Positive => {}
43 ty::ImplPolarity::Negative => {
44 return Err(tcx.dcx().emit_err(errors::DropImplPolarity::Negative {
45 span: tcx.def_span(drop_impl_did),
46 }));
47 }
48 ty::ImplPolarity::Reservation => {
49 return Err(tcx.dcx().emit_err(errors::DropImplPolarity::Reservation {
50 span: tcx.def_span(drop_impl_did),
51 }));
52 }
53 }
54
55 tcx.ensure_result().orphan_check_impl(drop_impl_did)?;
56
57 let self_ty = tcx.type_of(drop_impl_did).instantiate_identity().skip_norm_wip();
58
59 match self_ty.kind() {
60 ty::Adt(adt_def, adt_to_impl_args) => {
61 ensure_impl_params_and_item_params_correspond(
62 tcx,
63 drop_impl_did,
64 adt_def.did(),
65 adt_to_impl_args,
66 )?;
67
68 ensure_all_fields_are_const_destruct(tcx, drop_impl_did, adt_def.did())?;
69
70 ensure_impl_predicates_are_implied_by_item_defn(
71 tcx,
72 drop_impl_did,
73 adt_def.did(),
74 adt_to_impl_args,
75 )
76 }
77 _ => {
78 ::rustc_middle::util::bug::span_bug_fmt(tcx.def_span(drop_impl_did),
format_args!("incoherent impl of Drop"));span_bug!(tcx.def_span(drop_impl_did), "incoherent impl of Drop");
79 }
80 }
81}
82
83pub(crate) fn check_negative_auto_trait_impl<'tcx>(
84 tcx: TyCtxt<'tcx>,
85 impl_def_id: LocalDefId,
86 impl_trait_ref: ty::TraitRef<'tcx>,
87 polarity: ty::ImplPolarity,
88) -> Result<(), ErrorGuaranteed> {
89 let ty::ImplPolarity::Negative = polarity else {
90 return Ok(());
91 };
92
93 if !tcx.trait_is_auto(impl_trait_ref.def_id) {
94 return Ok(());
95 }
96
97 if tcx.defaultness(impl_def_id).is_default() {
98 tcx.dcx().span_delayed_bug(tcx.def_span(impl_def_id), "default impl cannot be negative");
99 }
100
101 tcx.ensure_result().orphan_check_impl(impl_def_id)?;
102
103 match impl_trait_ref.self_ty().kind() {
104 ty::Adt(adt_def, adt_to_impl_args) => {
105 ensure_impl_params_and_item_params_correspond(
106 tcx,
107 impl_def_id,
108 adt_def.did(),
109 adt_to_impl_args,
110 )?;
111
112 ensure_impl_predicates_are_implied_by_item_defn(
113 tcx,
114 impl_def_id,
115 adt_def.did(),
116 adt_to_impl_args,
117 )
118 }
119 _ => {
120 if tcx.features().auto_traits() {
121 Ok(())
126 } else {
127 Err(tcx.dcx().span_delayed_bug(
128 tcx.def_span(impl_def_id),
129 "incoherent impl of negative auto trait",
130 ))
131 }
132 }
133 }
134}
135
136fn ensure_impl_params_and_item_params_correspond<'tcx>(
137 tcx: TyCtxt<'tcx>,
138 impl_def_id: LocalDefId,
139 adt_def_id: DefId,
140 adt_to_impl_args: GenericArgsRef<'tcx>,
141) -> Result<(), ErrorGuaranteed> {
142 let Err(arg) = tcx.uses_unique_generic_params(adt_to_impl_args, CheckRegions::OnlyParam) else {
143 return Ok(());
144 };
145
146 let impl_span = tcx.def_span(impl_def_id);
147 let item_span = tcx.def_span(adt_def_id);
148 let self_descr = tcx.def_descr(adt_def_id);
149 let polarity = match tcx.impl_polarity(impl_def_id) {
150 ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
151 ty::ImplPolarity::Negative => "!",
152 };
153 let trait_name = tcx.item_name(tcx.impl_trait_id(impl_def_id.to_def_id()));
154 let mut err = {
tcx.dcx().struct_span_err(impl_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}{1}` impls cannot be specialized",
polarity, trait_name))
})).with_code(E0366)
}struct_span_code_err!(
155 tcx.dcx(),
156 impl_span,
157 E0366,
158 "`{polarity}{trait_name}` impls cannot be specialized",
159 );
160 match arg {
161 ty::util::NotUniqueParam::DuplicateParam(arg) => {
162 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is mentioned multiple times",
arg))
})format!("`{arg}` is mentioned multiple times"))
163 }
164 ty::util::NotUniqueParam::NotParam(arg) => {
165 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is not a generic parameter",
arg))
})format!("`{arg}` is not a generic parameter"))
166 }
167 };
168 err.span_note(
169 item_span,
170 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("use the same sequence of generic lifetime, type and const parameters as the {0} definition",
self_descr))
})format!(
171 "use the same sequence of generic lifetime, type and const parameters \
172 as the {self_descr} definition",
173 ),
174 );
175 Err(err.emit())
176}
177
178fn ensure_all_fields_are_const_destruct<'tcx>(
179 tcx: TyCtxt<'tcx>,
180 impl_def_id: LocalDefId,
181 adt_def_id: DefId,
182) -> Result<(), ErrorGuaranteed> {
183 if !tcx.is_conditionally_const(impl_def_id) {
184 return Ok(());
185 }
186 let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
187 let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
188
189 let impl_span = tcx.def_span(impl_def_id.to_def_id());
190 let env =
191 ty::EarlyBinder::bind(tcx.param_env(impl_def_id)).instantiate_identity().skip_norm_wip();
192 let args = ty::GenericArgs::identity_for_item(tcx, impl_def_id);
193 let destruct_trait = tcx.lang_items().destruct_trait().unwrap();
194 for field in tcx.adt_def(adt_def_id).all_fields() {
195 let field_ty = field.ty(tcx, args);
196 let cause = traits::ObligationCause::new(
197 tcx.def_span(field.did),
198 impl_def_id,
199 ObligationCauseCode::Misc,
200 );
201 ocx.register_obligation(traits::Obligation::new(
202 tcx,
203 cause,
204 env,
205 ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
206 trait_ref: ty::TraitRef::new(tcx, destruct_trait, [field_ty]),
207 constness: ty::BoundConstness::Maybe,
208 }),
209 ));
210 }
211 ocx.evaluate_obligations_error_on_ambiguity()
212 .into_iter()
213 .map(|error| {
214 let ty::ClauseKind::HostEffect(eff) =
215 error.root_obligation.predicate.expect_clause().kind().no_bound_vars().unwrap()
216 else {
217 ::core::panicking::panic("internal error: entered unreachable code")unreachable!()
218 };
219 let field_ty = eff.trait_ref.self_ty();
220 let mut diag = {
tcx.dcx().struct_span_err(error.root_obligation.cause.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` does not implement `[const] Destruct`",
field_ty))
})).with_code(E0367)
}struct_span_code_err!(
221 tcx.dcx(),
222 error.root_obligation.cause.span,
223 E0367,
224 "`{field_ty}` does not implement `[const] Destruct`",
225 )
226 .with_span_note(impl_span, "required for this `Drop` impl");
227 if field_ty.has_param()
228 && let Some(generics) = tcx.hir_node_by_def_id(impl_def_id).generics()
229 {
230 let destruct_def_id = tcx.lang_items().destruct_trait();
231 ty::suggest_constraining_type_param(
232 tcx,
233 generics,
234 &mut diag,
235 &field_ty.to_string(),
236 "[const] Destruct",
237 destruct_def_id,
238 None,
239 );
240 }
241 Err(diag.emit())
242 })
243 .collect()
244}
245
246fn ensure_impl_predicates_are_implied_by_item_defn<'tcx>(
250 tcx: TyCtxt<'tcx>,
251 impl_def_id: LocalDefId,
252 adt_def_id: DefId,
253 adt_to_impl_args: GenericArgsRef<'tcx>,
254) -> Result<(), ErrorGuaranteed> {
255 let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
256 let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
257
258 let impl_span = tcx.def_span(impl_def_id.to_def_id());
259 let trait_name = tcx.item_name(tcx.impl_trait_id(impl_def_id.to_def_id()));
260 let polarity = match tcx.impl_polarity(impl_def_id) {
261 ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
262 ty::ImplPolarity::Negative => "!",
263 };
264 let impl_adt_ty = Ty::new_adt(tcx, tcx.adt_def(adt_def_id), adt_to_impl_args);
279 let adt_env = ty::EarlyBinder::bind(tcx.param_env(adt_def_id))
280 .instantiate(tcx, adt_to_impl_args)
281 .skip_norm_wip();
282
283 let fresh_impl_args = infcx.fresh_args_for_item(impl_span, impl_def_id.to_def_id());
284 let fresh_adt_ty =
285 tcx.impl_trait_ref(impl_def_id).instantiate(tcx, fresh_impl_args).skip_norm_wip().self_ty();
286
287 ocx.eq(&ObligationCause::dummy_with_span(impl_span), adt_env, fresh_adt_ty, impl_adt_ty)
288 .expect("equating fully generic trait ref should never fail");
289
290 for (clause, span) in tcx.predicates_of(impl_def_id).instantiate(tcx, fresh_impl_args) {
291 let normalize_cause = traits::ObligationCause::misc(span, impl_def_id);
292 let pred = ocx.normalize(&normalize_cause, adt_env, clause);
293 let cause = traits::ObligationCause::new(
294 span,
295 impl_def_id,
296 ObligationCauseCode::AlwaysApplicableImpl,
297 );
298 ocx.register_obligation(traits::Obligation::new(tcx, cause, adt_env, pred));
299 }
300
301 let errors = ocx.evaluate_obligations_error_on_ambiguity();
308 if !errors.is_empty() {
309 let mut guar = None;
310 let mut root_predicates = FxHashSet::default();
311 for error in errors {
312 let root_predicate = error.root_obligation.predicate;
313 if root_predicates.insert(root_predicate) {
314 let item_span = tcx.def_span(adt_def_id);
315 let self_descr = tcx.def_descr(adt_def_id);
316 guar = Some(
317 {
tcx.dcx().struct_span_err(error.root_obligation.cause.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}{1}` impl requires `{2}` but the {3} it is implemented for does not",
polarity, trait_name, root_predicate, self_descr))
})).with_code(E0367)
}struct_span_code_err!(
318 tcx.dcx(),
319 error.root_obligation.cause.span,
320 E0367,
321 "`{polarity}{trait_name}` impl requires `{root_predicate}` \
322 but the {self_descr} it is implemented for does not",
323 )
324 .with_span_note(item_span, "the implementor must specify the same requirement")
325 .emit(),
326 );
327 }
328 }
329 return Err(guar.unwrap());
330 }
331
332 let errors = ocx.infcx.resolve_regions(impl_def_id, adt_env, []);
333 if !errors.is_empty() {
334 let mut guar = None;
335 for error in errors {
336 let item_span = tcx.def_span(adt_def_id);
337 let self_descr = tcx.def_descr(adt_def_id);
338 let outlives = match error {
339 RegionResolutionError::ConcreteFailure(_, a, b) => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: {1}", b, a))
})format!("{b}: {a}"),
340 RegionResolutionError::GenericBoundFailure(_, generic, r) => {
341 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: {1}", generic, r))
})format!("{generic}: {r}")
342 }
343 RegionResolutionError::SubSupConflict(_, _, _, a, _, b, _) => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: {1}", b, a))
})format!("{b}: {a}"),
344 RegionResolutionError::UpperBoundUniverseConflict(a, _, _, _, b) => {
345 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1}: {0}",
ty::Region::new_var(tcx, a), b))
})format!("{b}: {a}", a = ty::Region::new_var(tcx, a))
346 }
347 RegionResolutionError::CannotNormalize(..) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
348 };
349 guar = Some(
350 {
tcx.dcx().struct_span_err(error.origin().span(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}{1}` impl requires `{2}` but the {3} it is implemented for does not",
polarity, trait_name, outlives, self_descr))
})).with_code(E0367)
}struct_span_code_err!(
351 tcx.dcx(),
352 error.origin().span(),
353 E0367,
354 "`{polarity}{trait_name}` impl requires `{outlives}` \
355 but the {self_descr} it is implemented for does not",
356 )
357 .with_span_note(item_span, "the implementor must specify the same requirement")
358 .emit(),
359 );
360 }
361 return Err(guar.unwrap());
362 }
363
364 Ok(())
365}