1mod bounds;
17mod cmse;
18mod dyn_trait;
19pub mod errors;
20pub mod generics;
21
22use std::{assert_matches, slice};
23
24use rustc_abi::FIRST_VARIANT;
25use rustc_ast::LitKind;
26use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
27use rustc_errors::codes::*;
28use rustc_errors::{
29 Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, FatalError, StashKey,
30 struct_span_code_err,
31};
32use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
33use rustc_hir::def_id::{DefId, LocalDefId};
34use rustc_hir::{self as hir, AnonConst, GenericArg, GenericArgs, HirId};
35use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
36use rustc_infer::traits::DynCompatibilityViolation;
37use rustc_macros::{TypeFoldable, TypeVisitable};
38use rustc_middle::middle::stability::AllowUnstable;
39use rustc_middle::ty::{
40 self, Const, FnSigKind, GenericArgKind, GenericArgsRef, GenericParamDefKind, LitToConstInput,
41 Ty, TyCtxt, TypeSuperFoldable, TypeVisitableExt, TypingMode, Unnormalized, Upcast,
42 const_lit_matches_ty, fold_regions,
43};
44use rustc_middle::{bug, span_bug};
45use rustc_session::errors::feature_err;
46use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
47use rustc_span::{DUMMY_SP, Ident, Span, kw, sym};
48use rustc_trait_selection::infer::InferCtxtExt;
49use rustc_trait_selection::traits::{self, FulfillmentError};
50use tracing::{debug, instrument};
51
52use crate::check::check_abi;
53use crate::diagnostics::{BadReturnTypeNotation, NoFieldOnType};
54use crate::hir_ty_lowering::errors::{GenericsArgsErrExtend, prohibit_assoc_item_constraint};
55use crate::hir_ty_lowering::generics::{check_generic_arg_count, lower_generic_args};
56use crate::middle::resolve_bound_vars as rbv;
57use crate::{NoVariantNamed, check_c_variadic_abi};
58
59#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for ImpliedBoundsContext<'tcx> {
#[inline]
fn clone(&self) -> ImpliedBoundsContext<'tcx> {
let _: ::core::clone::AssertParamIsClone<LocalDefId>;
let _:
::core::clone::AssertParamIsClone<&'tcx [hir::WherePredicate<'tcx>]>;
*self
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for ImpliedBoundsContext<'tcx> { }Copy)]
62pub(crate) enum ImpliedBoundsContext<'tcx> {
63 TraitDef(LocalDefId),
66 TyParam(LocalDefId, &'tcx [hir::WherePredicate<'tcx>]),
68 AssociatedTypeOrImplTrait,
70}
71
72#[derive(#[automatically_derived]
impl ::core::fmt::Debug for GenericPathSegment {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"GenericPathSegment", &self.0, &&self.1)
}
}Debug)]
74pub struct GenericPathSegment(pub DefId, pub usize);
75
76#[derive(#[automatically_derived]
impl ::core::marker::Copy for PredicateFilter { }Copy, #[automatically_derived]
impl ::core::clone::Clone for PredicateFilter {
#[inline]
fn clone(&self) -> PredicateFilter {
let _: ::core::clone::AssertParamIsClone<Ident>;
*self
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for PredicateFilter {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
PredicateFilter::All =>
::core::fmt::Formatter::write_str(f, "All"),
PredicateFilter::SelfOnly =>
::core::fmt::Formatter::write_str(f, "SelfOnly"),
PredicateFilter::SelfTraitThatDefines(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"SelfTraitThatDefines", &__self_0),
PredicateFilter::SelfAndAssociatedTypeBounds =>
::core::fmt::Formatter::write_str(f,
"SelfAndAssociatedTypeBounds"),
PredicateFilter::ConstIfConst =>
::core::fmt::Formatter::write_str(f, "ConstIfConst"),
PredicateFilter::SelfConstIfConst =>
::core::fmt::Formatter::write_str(f, "SelfConstIfConst"),
}
}
}Debug)]
77pub enum PredicateFilter {
78 All,
80
81 SelfOnly,
83
84 SelfTraitThatDefines(Ident),
88
89 SelfAndAssociatedTypeBounds,
93
94 ConstIfConst,
96
97 SelfConstIfConst,
99}
100
101#[derive(#[automatically_derived]
impl<'a> ::core::fmt::Debug for RegionInferReason<'a> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
RegionInferReason::ExplicitObjectLifetime =>
::core::fmt::Formatter::write_str(f,
"ExplicitObjectLifetime"),
RegionInferReason::ObjectLifetimeDefault(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ObjectLifetimeDefault", &__self_0),
RegionInferReason::Param(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Param",
&__self_0),
RegionInferReason::RegionPredicate =>
::core::fmt::Formatter::write_str(f, "RegionPredicate"),
RegionInferReason::Reference =>
::core::fmt::Formatter::write_str(f, "Reference"),
RegionInferReason::OutlivesBound =>
::core::fmt::Formatter::write_str(f, "OutlivesBound"),
}
}
}Debug)]
102pub enum RegionInferReason<'a> {
103 ExplicitObjectLifetime,
105 ObjectLifetimeDefault(Span),
107 Param(&'a ty::GenericParamDef),
109 RegionPredicate,
110 Reference,
111 OutlivesBound,
112}
113
114#[derive(#[automatically_derived]
impl ::core::marker::Copy for InherentAssocCandidate { }Copy, #[automatically_derived]
impl ::core::clone::Clone for InherentAssocCandidate {
#[inline]
fn clone(&self) -> InherentAssocCandidate {
let _: ::core::clone::AssertParamIsClone<DefId>;
*self
}
}Clone, const _: () =
{
impl<'tcx>
::rustc_middle::ty::TypeFoldable<::rustc_middle::ty::TyCtxt<'tcx>>
for InherentAssocCandidate {
fn try_fold_with<__F: ::rustc_middle::ty::FallibleTypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
__folder: &mut __F) -> Result<Self, __F::Error> {
Ok(match self {
InherentAssocCandidate {
impl_: __binding_0,
assoc_item: __binding_1,
scope: __binding_2 } => {
InherentAssocCandidate {
impl_: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
__folder)?,
assoc_item: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_1,
__folder)?,
scope: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_2,
__folder)?,
}
}
})
}
fn fold_with<__F: ::rustc_middle::ty::TypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
__folder: &mut __F) -> Self {
match self {
InherentAssocCandidate {
impl_: __binding_0,
assoc_item: __binding_1,
scope: __binding_2 } => {
InherentAssocCandidate {
impl_: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
__folder),
assoc_item: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_1,
__folder),
scope: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_2,
__folder),
}
}
}
}
}
};TypeFoldable, const _: () =
{
impl<'tcx>
::rustc_middle::ty::TypeVisitable<::rustc_middle::ty::TyCtxt<'tcx>>
for InherentAssocCandidate {
fn visit_with<__V: ::rustc_middle::ty::TypeVisitor<::rustc_middle::ty::TyCtxt<'tcx>>>(&self,
__visitor: &mut __V) -> __V::Result {
match *self {
InherentAssocCandidate {
impl_: ref __binding_0,
assoc_item: ref __binding_1,
scope: ref __binding_2 } => {
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_1,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_2,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
}
}
<__V::Result as ::rustc_middle::ty::VisitorResult>::output()
}
}
};TypeVisitable, #[automatically_derived]
impl ::core::fmt::Debug for InherentAssocCandidate {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f,
"InherentAssocCandidate", "impl_", &self.impl_, "assoc_item",
&self.assoc_item, "scope", &&self.scope)
}
}Debug)]
115pub struct InherentAssocCandidate {
116 pub impl_: DefId,
117 pub assoc_item: DefId,
118 pub scope: DefId,
119}
120
121pub struct ResolvedStructPath<'tcx> {
122 pub res: Result<Res, ErrorGuaranteed>,
123 pub ty: Ty<'tcx>,
124}
125
126pub trait HirTyLowerer<'tcx> {
131 fn tcx(&self) -> TyCtxt<'tcx>;
132
133 fn dcx(&self) -> DiagCtxtHandle<'_>;
134
135 fn item_def_id(&self) -> LocalDefId;
137
138 fn re_infer(&self, span: Span, reason: RegionInferReason<'_>) -> ty::Region<'tcx>;
140
141 fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx>;
143
144 fn ct_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Const<'tcx>;
146
147 fn register_trait_ascription_bounds(
148 &self,
149 bounds: Vec<(ty::Clause<'tcx>, Span)>,
150 hir_id: HirId,
151 span: Span,
152 );
153
154 fn probe_ty_param_bounds(
169 &self,
170 span: Span,
171 def_id: LocalDefId,
172 assoc_ident: Ident,
173 ) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]>;
174
175 fn select_inherent_assoc_candidates(
176 &self,
177 span: Span,
178 self_ty: Ty<'tcx>,
179 candidates: Vec<InherentAssocCandidate>,
180 ) -> (Vec<InherentAssocCandidate>, Vec<FulfillmentError<'tcx>>);
181
182 fn lower_assoc_item_path(
195 &self,
196 span: Span,
197 item_def_id: DefId,
198 item_segment: &hir::PathSegment<'tcx>,
199 poly_trait_ref: ty::PolyTraitRef<'tcx>,
200 ) -> Result<(DefId, GenericArgsRef<'tcx>), ErrorGuaranteed>;
201
202 fn lower_fn_sig(
203 &self,
204 decl: &hir::FnDecl<'tcx>,
205 generics: Option<&hir::Generics<'_>>,
206 hir_id: HirId,
207 hir_ty: Option<&hir::Ty<'_>>,
208 ) -> (Vec<Ty<'tcx>>, Ty<'tcx>);
209
210 fn probe_adt(&self, span: Span, ty: Ty<'tcx>) -> Option<ty::AdtDef<'tcx>>;
217
218 fn record_ty(&self, hir_id: HirId, ty: Ty<'tcx>, span: Span);
220
221 fn infcx(&self) -> Option<&InferCtxt<'tcx>>;
223
224 fn lowerer(&self) -> &dyn HirTyLowerer<'tcx>
229 where
230 Self: Sized,
231 {
232 self
233 }
234
235 fn dyn_compatibility_violations(&self, trait_def_id: DefId) -> Vec<DynCompatibilityViolation>;
238}
239
240enum AssocItemQSelf {
244 Trait(DefId),
245 TyParam(LocalDefId, Span),
246 SelfTyAlias,
247}
248
249impl AssocItemQSelf {
250 fn to_string(&self, tcx: TyCtxt<'_>) -> String {
251 match *self {
252 Self::Trait(def_id) => tcx.def_path_str(def_id),
253 Self::TyParam(def_id, _) => tcx.hir_ty_param_name(def_id).to_string(),
254 Self::SelfTyAlias => kw::SelfUpper.to_string(),
255 }
256 }
257}
258
259#[derive(#[automatically_derived]
impl ::core::fmt::Debug for LowerTypeRelativePathMode {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
LowerTypeRelativePathMode::Type(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Type",
&__self_0),
LowerTypeRelativePathMode::Const =>
::core::fmt::Formatter::write_str(f, "Const"),
}
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for LowerTypeRelativePathMode {
#[inline]
fn clone(&self) -> LowerTypeRelativePathMode {
let _: ::core::clone::AssertParamIsClone<PermitVariants>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for LowerTypeRelativePathMode { }Copy)]
260enum LowerTypeRelativePathMode {
261 Type(PermitVariants),
262 Const,
263}
264
265impl LowerTypeRelativePathMode {
266 fn assoc_tag(self) -> ty::AssocTag {
267 match self {
268 Self::Type(_) => ty::AssocTag::Type,
269 Self::Const => ty::AssocTag::Const,
270 }
271 }
272
273 fn def_kind_for_diagnostics(self) -> DefKind {
275 match self {
276 Self::Type(_) => DefKind::AssocTy,
277 Self::Const => DefKind::AssocConst { is_type_const: false },
278 }
279 }
280
281 fn permit_variants(self) -> PermitVariants {
282 match self {
283 Self::Type(permit_variants) => permit_variants,
284 Self::Const => PermitVariants::No,
287 }
288 }
289}
290
291#[derive(#[automatically_derived]
impl ::core::fmt::Debug for PermitVariants {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
PermitVariants::Yes => "Yes",
PermitVariants::No => "No",
})
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for PermitVariants {
#[inline]
fn clone(&self) -> PermitVariants { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for PermitVariants { }Copy)]
293pub enum PermitVariants {
294 Yes,
295 No,
296}
297
298#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for TypeRelativePath<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
TypeRelativePath::AssocItem(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"AssocItem", &__self_0),
TypeRelativePath::Variant { adt: __self_0, variant_did: __self_1 }
=>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"Variant", "adt", __self_0, "variant_did", &__self_1),
TypeRelativePath::Ctor { ctor_def_id: __self_0, args: __self_1 }
=>
::core::fmt::Formatter::debug_struct_field2_finish(f, "Ctor",
"ctor_def_id", __self_0, "args", &__self_1),
}
}
}Debug, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for TypeRelativePath<'tcx> {
#[inline]
fn clone(&self) -> TypeRelativePath<'tcx> {
let _: ::core::clone::AssertParamIsClone<ty::AliasTerm<'tcx>>;
let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
let _: ::core::clone::AssertParamIsClone<DefId>;
let _: ::core::clone::AssertParamIsClone<GenericArgsRef<'tcx>>;
*self
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for TypeRelativePath<'tcx> { }Copy)]
299enum TypeRelativePath<'tcx> {
300 AssocItem(ty::AliasTerm<'tcx>),
301 Variant { adt: Ty<'tcx>, variant_did: DefId },
302 Ctor { ctor_def_id: DefId, args: GenericArgsRef<'tcx> },
303}
304
305#[derive(#[automatically_derived]
impl ::core::marker::Copy for ExplicitLateBound { }Copy, #[automatically_derived]
impl ::core::clone::Clone for ExplicitLateBound {
#[inline]
fn clone(&self) -> ExplicitLateBound { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for ExplicitLateBound {
#[inline]
fn eq(&self, other: &ExplicitLateBound) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for ExplicitLateBound {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
ExplicitLateBound::Yes => "Yes",
ExplicitLateBound::No => "No",
})
}
}Debug)]
315pub enum ExplicitLateBound {
316 Yes,
317 No,
318}
319
320#[derive(#[automatically_derived]
impl ::core::fmt::Debug for IsMethodCall {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
IsMethodCall::Yes => "Yes",
IsMethodCall::No => "No",
})
}
}Debug, #[automatically_derived]
impl ::core::marker::Copy for IsMethodCall { }Copy, #[automatically_derived]
impl ::core::clone::Clone for IsMethodCall {
#[inline]
fn clone(&self) -> IsMethodCall { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for IsMethodCall {
#[inline]
fn eq(&self, other: &IsMethodCall) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
321pub enum IsMethodCall {
322 Yes,
323 No,
324}
325
326#[derive(#[automatically_derived]
impl ::core::fmt::Debug for GenericArgPosition {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
GenericArgPosition::Type =>
::core::fmt::Formatter::write_str(f, "Type"),
GenericArgPosition::Value(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Value",
&__self_0),
}
}
}Debug, #[automatically_derived]
impl ::core::marker::Copy for GenericArgPosition { }Copy, #[automatically_derived]
impl ::core::clone::Clone for GenericArgPosition {
#[inline]
fn clone(&self) -> GenericArgPosition {
let _: ::core::clone::AssertParamIsClone<IsMethodCall>;
*self
}
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for GenericArgPosition {
#[inline]
fn eq(&self, other: &GenericArgPosition) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(GenericArgPosition::Value(__self_0),
GenericArgPosition::Value(__arg1_0)) =>
__self_0 == __arg1_0,
_ => true,
}
}
}PartialEq)]
329pub(crate) enum GenericArgPosition {
330 Type,
331 Value(IsMethodCall),
332}
333
334#[derive(#[automatically_derived]
impl ::core::clone::Clone for OverlappingAsssocItemConstraints {
#[inline]
fn clone(&self) -> OverlappingAsssocItemConstraints { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for OverlappingAsssocItemConstraints { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for OverlappingAsssocItemConstraints {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
OverlappingAsssocItemConstraints::Allowed => "Allowed",
OverlappingAsssocItemConstraints::Forbidden => "Forbidden",
})
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for OverlappingAsssocItemConstraints {
#[inline]
fn eq(&self, other: &OverlappingAsssocItemConstraints) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
338pub(crate) enum OverlappingAsssocItemConstraints {
339 Allowed,
340 Forbidden,
341}
342
343#[derive(#[automatically_derived]
impl ::core::clone::Clone for GenericArgCountMismatch {
#[inline]
fn clone(&self) -> GenericArgCountMismatch {
GenericArgCountMismatch {
reported: ::core::clone::Clone::clone(&self.reported),
invalid_args: ::core::clone::Clone::clone(&self.invalid_args),
}
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for GenericArgCountMismatch {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"GenericArgCountMismatch", "reported", &self.reported,
"invalid_args", &&self.invalid_args)
}
}Debug)]
346pub struct GenericArgCountMismatch {
347 pub reported: ErrorGuaranteed,
348 pub invalid_args: Vec<usize>,
350}
351
352#[derive(#[automatically_derived]
impl ::core::clone::Clone for GenericArgCountResult {
#[inline]
fn clone(&self) -> GenericArgCountResult {
GenericArgCountResult {
explicit_late_bound: ::core::clone::Clone::clone(&self.explicit_late_bound),
correct: ::core::clone::Clone::clone(&self.correct),
}
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for GenericArgCountResult {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"GenericArgCountResult", "explicit_late_bound",
&self.explicit_late_bound, "correct", &&self.correct)
}
}Debug)]
355pub struct GenericArgCountResult {
356 pub explicit_late_bound: ExplicitLateBound,
357 pub correct: Result<(), GenericArgCountMismatch>,
358}
359
360pub trait GenericArgsLowerer<'a, 'tcx> {
365 fn args_for_def_id(&mut self, def_id: DefId) -> (Option<&'a GenericArgs<'tcx>>, bool);
366
367 fn provided_kind(
368 &mut self,
369 preceding_args: &[ty::GenericArg<'tcx>],
370 param: &ty::GenericParamDef,
371 arg: &GenericArg<'tcx>,
372 ) -> ty::GenericArg<'tcx>;
373
374 fn inferred_kind(
375 &mut self,
376 preceding_args: &[ty::GenericArg<'tcx>],
377 param: &ty::GenericParamDef,
378 infer_args: bool,
379 ) -> ty::GenericArg<'tcx>;
380}
381
382enum ForbidParamContext {
384 ConstArgument,
386 EnumDiscriminant,
388}
389
390struct ForbidParamUsesFolder<'tcx> {
391 tcx: TyCtxt<'tcx>,
392 anon_const_def_id: LocalDefId,
393 span: Span,
394 is_self_alias: bool,
395 context: ForbidParamContext,
396}
397
398impl<'tcx> ForbidParamUsesFolder<'tcx> {
399 fn error(&self) -> ErrorGuaranteed {
400 let msg = match self.context {
401 ForbidParamContext::EnumDiscriminant if self.is_self_alias => {
402 "generic `Self` types are not permitted in enum discriminant values"
403 }
404 ForbidParamContext::EnumDiscriminant => {
405 "generic parameters may not be used in enum discriminant values"
406 }
407 ForbidParamContext::ConstArgument if self.is_self_alias => {
408 "generic `Self` types are currently not permitted in anonymous constants"
409 }
410 ForbidParamContext::ConstArgument => {
411 if self.tcx.features().generic_const_args() {
412 "generic parameters in const blocks are not allowed; use a named `const` item instead"
413 } else {
414 "generic parameters may not be used in const operations"
415 }
416 }
417 };
418 let mut diag = self.tcx.dcx().struct_span_err(self.span, msg);
419 if self.is_self_alias && #[allow(non_exhaustive_omitted_patterns)] match self.context {
ForbidParamContext::ConstArgument => true,
_ => false,
}matches!(self.context, ForbidParamContext::ConstArgument) {
420 let anon_const_hir_id: HirId = HirId::make_owner(self.anon_const_def_id);
421 let parent_impl = self.tcx.hir_parent_owner_iter(anon_const_hir_id).find_map(
422 |(_, node)| match node {
423 hir::OwnerNode::Item(hir::Item {
424 kind: hir::ItemKind::Impl(impl_), ..
425 }) => Some(impl_),
426 _ => None,
427 },
428 );
429 if let Some(impl_) = parent_impl {
430 diag.span_note(impl_.self_ty.span, "not a concrete type");
431 }
432 }
433 if #[allow(non_exhaustive_omitted_patterns)] match self.context {
ForbidParamContext::ConstArgument => true,
_ => false,
}matches!(self.context, ForbidParamContext::ConstArgument)
434 && self.tcx.features().min_generic_const_args()
435 {
436 if !self.tcx.features().generic_const_args() {
437 diag.help("add `#![feature(generic_const_args)]` to allow generic expressions as the RHS of const items");
438 } else {
439 diag.help("consider factoring the expression into a `type const` item and use it as the const argument instead");
440 }
441 }
442 diag.emit()
443 }
444}
445
446impl<'tcx> ty::TypeFolder<TyCtxt<'tcx>> for ForbidParamUsesFolder<'tcx> {
447 fn cx(&self) -> TyCtxt<'tcx> {
448 self.tcx
449 }
450
451 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
452 if #[allow(non_exhaustive_omitted_patterns)] match t.kind() {
ty::Param(..) => true,
_ => false,
}matches!(t.kind(), ty::Param(..)) {
453 return Ty::new_error(self.tcx, self.error());
454 }
455 t.super_fold_with(self)
456 }
457
458 fn fold_const(&mut self, c: Const<'tcx>) -> Const<'tcx> {
459 if #[allow(non_exhaustive_omitted_patterns)] match c.kind() {
ty::ConstKind::Param(..) => true,
_ => false,
}matches!(c.kind(), ty::ConstKind::Param(..)) {
460 return Const::new_error(self.tcx, self.error());
461 }
462 c.super_fold_with(self)
463 }
464
465 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
466 if #[allow(non_exhaustive_omitted_patterns)] match r.kind() {
ty::RegionKind::ReEarlyParam(..) | ty::RegionKind::ReLateParam(..) =>
true,
_ => false,
}matches!(r.kind(), ty::RegionKind::ReEarlyParam(..) | ty::RegionKind::ReLateParam(..)) {
467 return ty::Region::new_error(self.tcx, self.error());
468 }
469 r
470 }
471}
472
473impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
474 pub fn check_param_res_if_mcg_for_instantiate_value_path(
478 &self,
479 res: Res,
480 span: Span,
481 ) -> Result<(), ErrorGuaranteed> {
482 let tcx = self.tcx();
483 let parent_def_id = self.item_def_id();
484 if let Res::Def(DefKind::ConstParam, _) = res
485 && tcx.def_kind(parent_def_id) == DefKind::AnonConst
486 && let ty::AnonConstKind::MCG = tcx.anon_const_kind(parent_def_id)
487 {
488 let folder = ForbidParamUsesFolder {
489 tcx,
490 anon_const_def_id: parent_def_id,
491 span,
492 is_self_alias: false,
493 context: ForbidParamContext::ConstArgument,
494 };
495 return Err(folder.error());
496 }
497 Ok(())
498 }
499
500 fn anon_const_forbids_generic_params(&self) -> Option<ForbidParamContext> {
508 let tcx = self.tcx();
509 let parent_def_id = self.item_def_id();
510
511 let anon_const_def_id = match tcx.def_kind(parent_def_id) {
515 DefKind::AnonConst => parent_def_id,
516 DefKind::InlineConst | DefKind::Closure => {
517 let root = tcx.typeck_root_def_id(parent_def_id.into());
518 match tcx.def_kind(root) {
519 DefKind::AnonConst => root.expect_local(),
520 _ => return None,
521 }
522 }
523 _ => return None,
524 };
525
526 match tcx.anon_const_kind(anon_const_def_id) {
527 ty::AnonConstKind::MCG => Some(ForbidParamContext::ConstArgument),
528 ty::AnonConstKind::NonTypeSystem => {
529 if tcx.generics_of(anon_const_def_id).count() == 0 {
533 Some(ForbidParamContext::EnumDiscriminant)
534 } else {
535 None
536 }
537 }
538 ty::AnonConstKind::GCE | ty::AnonConstKind::RepeatExprCount => None,
539 }
540 }
541
542 #[must_use = "need to use transformed output"]
548 fn check_param_uses_if_mcg<T>(&self, term: T, span: Span, is_self_alias: bool) -> T
549 where
550 T: ty::TypeFoldable<TyCtxt<'tcx>>,
551 {
552 let tcx = self.tcx();
553 if let Some(context) = self.anon_const_forbids_generic_params()
554 && (term.has_param() || term.has_escaping_bound_vars())
556 {
557 let anon_const_def_id = self.item_def_id();
558 let mut folder =
559 ForbidParamUsesFolder { tcx, anon_const_def_id, span, is_self_alias, context };
560 term.fold_with(&mut folder)
561 } else {
562 term
563 }
564 }
565
566 x;#[instrument(level = "debug", skip(self), ret)]
568 pub fn lower_lifetime(
569 &self,
570 lifetime: &hir::Lifetime,
571 reason: RegionInferReason<'_>,
572 ) -> ty::Region<'tcx> {
573 if let Some(resolved) = self.tcx().named_bound_var(lifetime.hir_id) {
574 let region = self.lower_resolved_lifetime(resolved);
575 self.check_param_uses_if_mcg(region, lifetime.ident.span, false)
576 } else {
577 self.re_infer(lifetime.ident.span, reason)
578 }
579 }
580
581 x;#[instrument(level = "debug", skip(self), ret)]
583 fn lower_resolved_lifetime(&self, resolved: rbv::ResolvedArg) -> ty::Region<'tcx> {
584 let tcx = self.tcx();
585
586 match resolved {
587 rbv::ResolvedArg::StaticLifetime => tcx.lifetimes.re_static,
588
589 rbv::ResolvedArg::LateBound(debruijn, index, def_id) => {
590 let br = ty::BoundRegion {
591 var: ty::BoundVar::from_u32(index),
592 kind: ty::BoundRegionKind::Named(def_id.to_def_id()),
593 };
594 ty::Region::new_bound(tcx, debruijn, br)
595 }
596
597 rbv::ResolvedArg::EarlyBound(def_id) => {
598 let name = tcx.hir_ty_param_name(def_id);
599 let item_def_id = tcx.hir_ty_param_owner(def_id);
600 let generics = tcx.generics_of(item_def_id);
601 let index = generics.param_def_id_to_index[&def_id.to_def_id()];
602 ty::Region::new_early_param(tcx, ty::EarlyParamRegion { index, name })
603 }
604
605 rbv::ResolvedArg::Free(scope, id) => {
606 ty::Region::new_late_param(
607 tcx,
608 scope.to_def_id(),
609 ty::LateParamRegionKind::Named(id.to_def_id()),
610 )
611
612 }
614
615 rbv::ResolvedArg::Error(guar) => ty::Region::new_error(tcx, guar),
616 }
617 }
618
619 pub fn lower_generic_args_of_path_segment(
620 &self,
621 span: Span,
622 def_id: DefId,
623 item_segment: &hir::PathSegment<'tcx>,
624 ) -> GenericArgsRef<'tcx> {
625 let (args, _) = self.lower_generic_args_of_path(span, def_id, &[], item_segment, None);
626 if let Some(c) = item_segment.args().constraints.first() {
627 prohibit_assoc_item_constraint(self, c, Some((def_id, item_segment, span)));
628 }
629 args
630 }
631
632 x;#[instrument(level = "debug", skip(self, span), ret)]
667 pub(crate) fn lower_generic_args_of_path(
668 &self,
669 span: Span,
670 def_id: DefId,
671 parent_args: &[ty::GenericArg<'tcx>],
672 segment: &hir::PathSegment<'tcx>,
673 self_ty: Option<Ty<'tcx>>,
674 ) -> (GenericArgsRef<'tcx>, GenericArgCountResult) {
675 let tcx = self.tcx();
680 let generics = tcx.generics_of(def_id);
681 debug!(?generics);
682
683 if generics.has_self {
684 if generics.parent.is_some() {
685 assert!(!parent_args.is_empty())
688 } else {
689 assert!(self_ty.is_some());
691 }
692 } else {
693 assert!(self_ty.is_none());
694 }
695
696 let arg_count = check_generic_arg_count(
697 self,
698 def_id,
699 segment,
700 generics,
701 GenericArgPosition::Type,
702 self_ty.is_some(),
703 );
704
705 if generics.is_own_empty() {
710 return (tcx.mk_args(parent_args), arg_count);
711 }
712
713 struct GenericArgsCtxt<'a, 'tcx> {
714 lowerer: &'a dyn HirTyLowerer<'tcx>,
715 def_id: DefId,
716 generic_args: &'a GenericArgs<'tcx>,
717 span: Span,
718 infer_args: bool,
719 incorrect_args: &'a Result<(), GenericArgCountMismatch>,
720 }
721
722 impl<'a, 'tcx> GenericArgsLowerer<'a, 'tcx> for GenericArgsCtxt<'a, 'tcx> {
723 fn args_for_def_id(&mut self, did: DefId) -> (Option<&'a GenericArgs<'tcx>>, bool) {
724 if did == self.def_id {
725 (Some(self.generic_args), self.infer_args)
726 } else {
727 (None, false)
729 }
730 }
731
732 fn provided_kind(
733 &mut self,
734 preceding_args: &[ty::GenericArg<'tcx>],
735 param: &ty::GenericParamDef,
736 arg: &GenericArg<'tcx>,
737 ) -> ty::GenericArg<'tcx> {
738 let tcx = self.lowerer.tcx();
739
740 if let Err(incorrect) = self.incorrect_args {
741 if incorrect.invalid_args.contains(&(param.index as usize)) {
742 return param.to_error(tcx);
743 }
744 }
745
746 let handle_ty_args = |has_default, ty: &hir::Ty<'tcx>| {
747 if has_default {
748 tcx.check_optional_stability(
749 param.def_id,
750 Some(arg.hir_id()),
751 arg.span(),
752 None,
753 AllowUnstable::No,
754 |_, _| {
755 },
761 );
762 }
763 self.lowerer.lower_ty(ty).into()
764 };
765
766 match (¶m.kind, arg) {
767 (GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
768 self.lowerer.lower_lifetime(lt, RegionInferReason::Param(param)).into()
769 }
770 (&GenericParamDefKind::Type { has_default, .. }, GenericArg::Type(ty)) => {
771 handle_ty_args(has_default, ty.as_unambig_ty())
773 }
774 (&GenericParamDefKind::Type { has_default, .. }, GenericArg::Infer(inf)) => {
775 handle_ty_args(has_default, &inf.to_ty())
776 }
777 (GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => self
778 .lowerer
779 .lower_const_arg(
781 ct.as_unambig_ct(),
782 tcx.type_of(param.def_id)
783 .instantiate(tcx, preceding_args)
784 .skip_norm_wip(),
785 )
786 .into(),
787 (&GenericParamDefKind::Const { .. }, GenericArg::Infer(inf)) => {
788 self.lowerer.ct_infer(Some(param), inf.span).into()
789 }
790 (kind, arg) => span_bug!(
791 self.span,
792 "mismatched path argument for kind {kind:?}: found arg {arg:?}"
793 ),
794 }
795 }
796
797 fn inferred_kind(
798 &mut self,
799 preceding_args: &[ty::GenericArg<'tcx>],
800 param: &ty::GenericParamDef,
801 infer_args: bool,
802 ) -> ty::GenericArg<'tcx> {
803 let tcx = self.lowerer.tcx();
804
805 if let Err(incorrect) = self.incorrect_args {
806 if incorrect.invalid_args.contains(&(param.index as usize)) {
807 return param.to_error(tcx);
808 }
809 }
810 match param.kind {
811 GenericParamDefKind::Lifetime => {
812 self.lowerer.re_infer(self.span, RegionInferReason::Param(param)).into()
813 }
814 GenericParamDefKind::Type { has_default, synthetic } => {
815 if !infer_args && has_default {
816 if let Some(prev) =
818 preceding_args.iter().find_map(|arg| match arg.kind() {
819 GenericArgKind::Type(ty) => ty.error_reported().err(),
820 _ => None,
821 })
822 {
823 return Ty::new_error(tcx, prev).into();
825 }
826 tcx.at(self.span)
827 .type_of(param.def_id)
828 .instantiate(tcx, preceding_args)
829 .skip_norm_wip()
830 .into()
831 } else if synthetic {
832 Ty::new_param(tcx, param.index, param.name).into()
833 } else if infer_args {
834 self.lowerer.ty_infer(Some(param), self.span).into()
835 } else {
836 Ty::new_misc_error(tcx).into()
838 }
839 }
840 GenericParamDefKind::Const { has_default, .. } => {
841 let ty = tcx
842 .at(self.span)
843 .type_of(param.def_id)
844 .instantiate(tcx, preceding_args)
845 .skip_norm_wip();
846 if let Err(guar) = ty.error_reported() {
847 return ty::Const::new_error(tcx, guar).into();
848 }
849 if !infer_args && has_default {
850 tcx.const_param_default(param.def_id)
851 .instantiate(tcx, preceding_args)
852 .skip_norm_wip()
853 .into()
854 } else if infer_args {
855 self.lowerer.ct_infer(Some(param), self.span).into()
856 } else {
857 ty::Const::new_misc_error(tcx).into()
859 }
860 }
861 }
862 }
863 }
864
865 let mut args_ctx = GenericArgsCtxt {
866 lowerer: self,
867 def_id,
868 span,
869 generic_args: segment.args(),
870 infer_args: segment.infer_args,
871 incorrect_args: &arg_count.correct,
872 };
873
874 let args = lower_generic_args(
875 self,
876 def_id,
877 parent_args,
878 self_ty.is_some(),
879 self_ty,
880 &arg_count,
881 &mut args_ctx,
882 );
883
884 (args, arg_count)
885 }
886
887 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("lower_generic_args_of_assoc_item",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(887u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["span",
"item_def_id", "item_segment", "parent_args"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&item_def_id)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&item_segment)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&parent_args)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: GenericArgsRef<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
let (args, _) =
self.lower_generic_args_of_path(span, item_def_id,
parent_args, item_segment, None);
if let Some(c) = item_segment.args().constraints.first() {
prohibit_assoc_item_constraint(self, c,
Some((item_def_id, item_segment, span)));
}
args
}
}
}#[instrument(level = "debug", skip(self))]
888 pub fn lower_generic_args_of_assoc_item(
889 &self,
890 span: Span,
891 item_def_id: DefId,
892 item_segment: &hir::PathSegment<'tcx>,
893 parent_args: GenericArgsRef<'tcx>,
894 ) -> GenericArgsRef<'tcx> {
895 let (args, _) =
896 self.lower_generic_args_of_path(span, item_def_id, parent_args, item_segment, None);
897 if let Some(c) = item_segment.args().constraints.first() {
898 prohibit_assoc_item_constraint(self, c, Some((item_def_id, item_segment, span)));
899 }
900 args
901 }
902
903 pub fn lower_impl_trait_ref(
907 &self,
908 trait_ref: &hir::TraitRef<'tcx>,
909 self_ty: Ty<'tcx>,
910 ) -> ty::TraitRef<'tcx> {
911 let [leading_segments @ .., segment] = trait_ref.path.segments else { ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!() };
912
913 let _ = self.prohibit_generic_args(leading_segments.iter(), GenericsArgsErrExtend::None);
914
915 self.lower_mono_trait_ref(
916 trait_ref.path.span,
917 trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise()),
918 self_ty,
919 segment,
920 true,
921 )
922 }
923
924 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("lower_poly_trait_ref",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(947u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["bound_generic_params",
"constness", "polarity", "trait_ref", "span", "self_ty",
"predicate_filter", "overlapping_assoc_item_constraints"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&bound_generic_params)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&constness)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&polarity)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&trait_ref)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self_ty)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&predicate_filter)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&overlapping_assoc_item_constraints)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: GenericArgCountResult = loop {};
return __tracing_attr_fake_return;
}
{
let tcx = self.tcx();
let _ = bound_generic_params;
let trait_def_id =
trait_ref.trait_def_id().unwrap_or_else(||
FatalError.raise());
let transient =
match polarity {
hir::BoundPolarity::Positive => {
tcx.is_lang_item(trait_def_id, hir::LangItem::PointeeSized)
}
hir::BoundPolarity::Negative(_) => false,
hir::BoundPolarity::Maybe(_) => {
self.require_bound_to_relax_default_trait(trait_ref, span);
true
}
};
let bounds = if transient { &mut Vec::new() } else { bounds };
let polarity =
match polarity {
hir::BoundPolarity::Positive | hir::BoundPolarity::Maybe(_)
=> {
ty::PredicatePolarity::Positive
}
hir::BoundPolarity::Negative(_) =>
ty::PredicatePolarity::Negative,
};
let [leading_segments @ .., segment] =
trait_ref.path.segments else {
::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))
};
let _ =
self.prohibit_generic_args(leading_segments.iter(),
GenericsArgsErrExtend::None);
self.report_internal_fn_trait(span, trait_def_id, segment, false);
let (generic_args, arg_count) =
self.lower_generic_args_of_path(trait_ref.path.span,
trait_def_id, &[], segment, Some(self_ty));
let constraints = segment.args().constraints;
if transient &&
(!generic_args[1..].is_empty() || !constraints.is_empty()) {
self.dcx().span_delayed_bug(span,
"transient bound should not have args or constraints");
}
let bound_vars = tcx.late_bound_vars(trait_ref.hir_ref_id);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:1027",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(1027u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["bound_vars"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&bound_vars)
as &dyn Value))])
});
} else { ; }
};
let poly_trait_ref =
ty::Binder::bind_with_vars(ty::TraitRef::new_from_args(tcx,
trait_def_id, generic_args), bound_vars);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:1034",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(1034u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["poly_trait_ref"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&poly_trait_ref)
as &dyn Value))])
});
} else { ; }
};
match predicate_filter {
PredicateFilter::All | PredicateFilter::SelfOnly |
PredicateFilter::SelfTraitThatDefines(..) |
PredicateFilter::SelfAndAssociatedTypeBounds => {
let bound =
poly_trait_ref.map_bound(|trait_ref|
{
ty::ClauseKind::Trait(ty::TraitPredicate {
trait_ref,
polarity,
})
});
let bound = (bound.upcast(tcx), span);
if tcx.is_lang_item(trait_def_id,
rustc_hir::LangItem::Sized) {
bounds.insert(0, bound);
} else { bounds.push(bound); }
}
PredicateFilter::ConstIfConst |
PredicateFilter::SelfConstIfConst => {}
}
if let hir::BoundConstness::Always(span) |
hir::BoundConstness::Maybe(span) = constness &&
!tcx.is_const_trait(trait_def_id) {
let (def_span, suggestion, suggestion_pre) =
match (trait_def_id.as_local(), tcx.sess.is_nightly_build())
{
(Some(trait_def_id), true) => {
let span = tcx.hir_expect_item(trait_def_id).vis_span;
let span =
tcx.sess.source_map().span_extend_while_whitespace(span);
(None, Some(span.shrink_to_hi()),
if self.tcx().features().const_trait_impl() {
""
} else {
"enable `#![feature(const_trait_impl)]` in your crate and "
})
}
(None, _) | (_, false) =>
(Some(tcx.def_span(trait_def_id)), None, ""),
};
self.dcx().emit_err(crate::diagnostics::ConstBoundForNonConstTrait {
span,
modifier: constness.as_str(),
def_span,
trait_name: tcx.def_path_str(trait_def_id),
suggestion,
suggestion_pre,
});
} else {
match predicate_filter {
PredicateFilter::SelfTraitThatDefines(..) => {}
PredicateFilter::All | PredicateFilter::SelfOnly |
PredicateFilter::SelfAndAssociatedTypeBounds => {
match constness {
hir::BoundConstness::Always(_) => {
if polarity == ty::PredicatePolarity::Positive {
bounds.push((poly_trait_ref.to_host_effect_clause(tcx,
ty::BoundConstness::Const), span));
}
}
hir::BoundConstness::Maybe(_) => {}
hir::BoundConstness::Never => {}
}
}
PredicateFilter::ConstIfConst |
PredicateFilter::SelfConstIfConst => {
match constness {
hir::BoundConstness::Maybe(_) => {
if polarity == ty::PredicatePolarity::Positive {
bounds.push((poly_trait_ref.to_host_effect_clause(tcx,
ty::BoundConstness::Maybe), span));
}
}
hir::BoundConstness::Always(_) | hir::BoundConstness::Never
=> {}
}
}
}
}
let mut dup_constraints =
(overlapping_assoc_item_constraints ==
OverlappingAsssocItemConstraints::Forbidden).then_some(FxIndexMap::default());
for constraint in constraints {
if polarity == ty::PredicatePolarity::Negative {
self.dcx().span_delayed_bug(constraint.span,
"negative trait bounds should not have assoc item constraints");
break;
}
let _: Result<_, ErrorGuaranteed> =
self.lower_assoc_item_constraint(trait_ref.hir_ref_id,
poly_trait_ref, constraint, bounds,
dup_constraints.as_mut(), constraint.span,
predicate_filter);
}
arg_count
}
}
}#[instrument(level = "debug", skip(self, bounds))]
948 pub(crate) fn lower_poly_trait_ref(
949 &self,
950 &hir::PolyTraitRef {
951 bound_generic_params,
952 modifiers: hir::TraitBoundModifiers { constness, polarity },
953 trait_ref,
954 span,
955 }: &hir::PolyTraitRef<'tcx>,
956 self_ty: Ty<'tcx>,
957 bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
958 predicate_filter: PredicateFilter,
959 overlapping_assoc_item_constraints: OverlappingAsssocItemConstraints,
960 ) -> GenericArgCountResult {
961 let tcx = self.tcx();
962
963 let _ = bound_generic_params;
966
967 let trait_def_id = trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise());
968
969 let transient = match polarity {
974 hir::BoundPolarity::Positive => {
975 tcx.is_lang_item(trait_def_id, hir::LangItem::PointeeSized)
981 }
982 hir::BoundPolarity::Negative(_) => false,
983 hir::BoundPolarity::Maybe(_) => {
984 self.require_bound_to_relax_default_trait(trait_ref, span);
985 true
986 }
987 };
988 let bounds = if transient { &mut Vec::new() } else { bounds };
989
990 let polarity = match polarity {
991 hir::BoundPolarity::Positive | hir::BoundPolarity::Maybe(_) => {
992 ty::PredicatePolarity::Positive
993 }
994 hir::BoundPolarity::Negative(_) => ty::PredicatePolarity::Negative,
995 };
996
997 let [leading_segments @ .., segment] = trait_ref.path.segments else { bug!() };
998
999 let _ = self.prohibit_generic_args(leading_segments.iter(), GenericsArgsErrExtend::None);
1000 self.report_internal_fn_trait(span, trait_def_id, segment, false);
1001
1002 let (generic_args, arg_count) = self.lower_generic_args_of_path(
1003 trait_ref.path.span,
1004 trait_def_id,
1005 &[],
1006 segment,
1007 Some(self_ty),
1008 );
1009
1010 let constraints = segment.args().constraints;
1011
1012 if transient && (!generic_args[1..].is_empty() || !constraints.is_empty()) {
1013 self.dcx()
1023 .span_delayed_bug(span, "transient bound should not have args or constraints");
1024 }
1025
1026 let bound_vars = tcx.late_bound_vars(trait_ref.hir_ref_id);
1027 debug!(?bound_vars);
1028
1029 let poly_trait_ref = ty::Binder::bind_with_vars(
1030 ty::TraitRef::new_from_args(tcx, trait_def_id, generic_args),
1031 bound_vars,
1032 );
1033
1034 debug!(?poly_trait_ref);
1035
1036 match predicate_filter {
1038 PredicateFilter::All
1039 | PredicateFilter::SelfOnly
1040 | PredicateFilter::SelfTraitThatDefines(..)
1041 | PredicateFilter::SelfAndAssociatedTypeBounds => {
1042 let bound = poly_trait_ref.map_bound(|trait_ref| {
1043 ty::ClauseKind::Trait(ty::TraitPredicate { trait_ref, polarity })
1044 });
1045 let bound = (bound.upcast(tcx), span);
1046 if tcx.is_lang_item(trait_def_id, rustc_hir::LangItem::Sized) {
1052 bounds.insert(0, bound);
1053 } else {
1054 bounds.push(bound);
1055 }
1056 }
1057 PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
1058 }
1059
1060 if let hir::BoundConstness::Always(span) | hir::BoundConstness::Maybe(span) = constness
1061 && !tcx.is_const_trait(trait_def_id)
1062 {
1063 let (def_span, suggestion, suggestion_pre) =
1064 match (trait_def_id.as_local(), tcx.sess.is_nightly_build()) {
1065 (Some(trait_def_id), true) => {
1066 let span = tcx.hir_expect_item(trait_def_id).vis_span;
1067 let span = tcx.sess.source_map().span_extend_while_whitespace(span);
1068
1069 (
1070 None,
1071 Some(span.shrink_to_hi()),
1072 if self.tcx().features().const_trait_impl() {
1073 ""
1074 } else {
1075 "enable `#![feature(const_trait_impl)]` in your crate and "
1076 },
1077 )
1078 }
1079 (None, _) | (_, false) => (Some(tcx.def_span(trait_def_id)), None, ""),
1080 };
1081 self.dcx().emit_err(crate::diagnostics::ConstBoundForNonConstTrait {
1082 span,
1083 modifier: constness.as_str(),
1084 def_span,
1085 trait_name: tcx.def_path_str(trait_def_id),
1086 suggestion,
1087 suggestion_pre,
1088 });
1089 } else {
1090 match predicate_filter {
1091 PredicateFilter::SelfTraitThatDefines(..) => {}
1093 PredicateFilter::All
1094 | PredicateFilter::SelfOnly
1095 | PredicateFilter::SelfAndAssociatedTypeBounds => {
1096 match constness {
1097 hir::BoundConstness::Always(_) => {
1098 if polarity == ty::PredicatePolarity::Positive {
1099 bounds.push((
1100 poly_trait_ref
1101 .to_host_effect_clause(tcx, ty::BoundConstness::Const),
1102 span,
1103 ));
1104 }
1105 }
1106 hir::BoundConstness::Maybe(_) => {
1107 }
1112 hir::BoundConstness::Never => {}
1113 }
1114 }
1115 PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {
1122 match constness {
1123 hir::BoundConstness::Maybe(_) => {
1124 if polarity == ty::PredicatePolarity::Positive {
1125 bounds.push((
1126 poly_trait_ref
1127 .to_host_effect_clause(tcx, ty::BoundConstness::Maybe),
1128 span,
1129 ));
1130 }
1131 }
1132 hir::BoundConstness::Always(_) | hir::BoundConstness::Never => {}
1133 }
1134 }
1135 }
1136 }
1137
1138 let mut dup_constraints = (overlapping_assoc_item_constraints
1139 == OverlappingAsssocItemConstraints::Forbidden)
1140 .then_some(FxIndexMap::default());
1141
1142 for constraint in constraints {
1143 if polarity == ty::PredicatePolarity::Negative {
1147 self.dcx().span_delayed_bug(
1148 constraint.span,
1149 "negative trait bounds should not have assoc item constraints",
1150 );
1151 break;
1152 }
1153
1154 let _: Result<_, ErrorGuaranteed> = self.lower_assoc_item_constraint(
1156 trait_ref.hir_ref_id,
1157 poly_trait_ref,
1158 constraint,
1159 bounds,
1160 dup_constraints.as_mut(),
1161 constraint.span,
1162 predicate_filter,
1163 );
1164 }
1166
1167 arg_count
1168 }
1169
1170 fn lower_mono_trait_ref(
1174 &self,
1175 span: Span,
1176 trait_def_id: DefId,
1177 self_ty: Ty<'tcx>,
1178 trait_segment: &hir::PathSegment<'tcx>,
1179 is_impl: bool,
1180 ) -> ty::TraitRef<'tcx> {
1181 self.report_internal_fn_trait(span, trait_def_id, trait_segment, is_impl);
1182
1183 let (generic_args, _) =
1184 self.lower_generic_args_of_path(span, trait_def_id, &[], trait_segment, Some(self_ty));
1185 if let Some(c) = trait_segment.args().constraints.first() {
1186 prohibit_assoc_item_constraint(self, c, Some((trait_def_id, trait_segment, span)));
1187 }
1188 ty::TraitRef::new_from_args(self.tcx(), trait_def_id, generic_args)
1189 }
1190
1191 fn probe_trait_that_defines_assoc_item(
1192 &self,
1193 trait_def_id: DefId,
1194 assoc_tag: ty::AssocTag,
1195 assoc_ident: Ident,
1196 ) -> bool {
1197 self.tcx()
1198 .associated_items(trait_def_id)
1199 .find_by_ident_and_kind(self.tcx(), assoc_ident, assoc_tag, trait_def_id)
1200 .is_some()
1201 }
1202
1203 fn lower_path_segment(
1204 &self,
1205 span: Span,
1206 def_id: DefId,
1207 item_segment: &hir::PathSegment<'tcx>,
1208 ) -> Ty<'tcx> {
1209 let tcx = self.tcx();
1210 let args = self.lower_generic_args_of_path_segment(span, def_id, item_segment);
1211
1212 if let DefKind::TyAlias = tcx.def_kind(def_id)
1213 && tcx.type_alias_is_lazy(def_id)
1214 {
1215 let alias_ty = ty::AliasTy::new_from_args(tcx, ty::Free { def_id }, args);
1219 Ty::new_alias(tcx, alias_ty)
1220 } else {
1221 tcx.at(span).type_of(def_id).instantiate(tcx, args).skip_norm_wip()
1222 }
1223 }
1224
1225 x;#[instrument(level = "debug", skip_all, ret)]
1233 fn probe_single_ty_param_bound_for_assoc_item(
1234 &self,
1235 ty_param_def_id: LocalDefId,
1236 ty_param_span: Span,
1237 assoc_tag: ty::AssocTag,
1238 assoc_ident: Ident,
1239 span: Span,
1240 ) -> Result<ty::PolyTraitRef<'tcx>, ErrorGuaranteed> {
1241 debug!(?ty_param_def_id, ?assoc_ident, ?span);
1242 let tcx = self.tcx();
1243
1244 let predicates = &self.probe_ty_param_bounds(span, ty_param_def_id, assoc_ident);
1245 debug!("predicates={:#?}", predicates);
1246
1247 self.probe_single_bound_for_assoc_item(
1248 || {
1249 let trait_refs = predicates
1250 .iter_identity_copied()
1251 .map(Unnormalized::skip_norm_wip)
1252 .filter_map(|(p, _)| Some(p.as_trait_clause()?.map_bound(|t| t.trait_ref)));
1253 traits::transitive_bounds_that_define_assoc_item(tcx, trait_refs, assoc_ident)
1254 },
1255 AssocItemQSelf::TyParam(ty_param_def_id, ty_param_span),
1256 assoc_tag,
1257 assoc_ident,
1258 span,
1259 None,
1260 )
1261 }
1262
1263 x;#[instrument(level = "debug", skip(self, all_candidates, qself, constraint), ret)]
1269 fn probe_single_bound_for_assoc_item<I>(
1270 &self,
1271 all_candidates: impl Fn() -> I,
1272 qself: AssocItemQSelf,
1273 assoc_tag: ty::AssocTag,
1274 assoc_ident: Ident,
1275 span: Span,
1276 constraint: Option<&hir::AssocItemConstraint<'tcx>>,
1277 ) -> Result<ty::PolyTraitRef<'tcx>, ErrorGuaranteed>
1278 where
1279 I: Iterator<Item = ty::PolyTraitRef<'tcx>>,
1280 {
1281 let mut matching_candidates = all_candidates().filter(|r| {
1282 self.probe_trait_that_defines_assoc_item(r.def_id(), assoc_tag, assoc_ident)
1283 });
1284
1285 let Some(bound1) = matching_candidates.next() else {
1286 return Err(self.report_unresolved_assoc_item(
1287 all_candidates,
1288 qself,
1289 assoc_tag,
1290 assoc_ident,
1291 span,
1292 constraint,
1293 ));
1294 };
1295
1296 if let Some(bound2) = matching_candidates.next() {
1297 return Err(self.report_ambiguous_assoc_item(
1298 bound1,
1299 bound2,
1300 matching_candidates,
1301 qself,
1302 assoc_tag,
1303 assoc_ident,
1304 span,
1305 constraint,
1306 ));
1307 }
1308
1309 Ok(bound1)
1310 }
1311
1312 x;#[instrument(level = "debug", skip_all, ret)]
1339 pub fn lower_type_relative_ty_path(
1340 &self,
1341 self_ty: Ty<'tcx>,
1342 hir_self_ty: &'tcx hir::Ty<'tcx>,
1343 segment: &'tcx hir::PathSegment<'tcx>,
1344 qpath_hir_id: HirId,
1345 span: Span,
1346 permit_variants: PermitVariants,
1347 ) -> Result<(Ty<'tcx>, DefKind, DefId), ErrorGuaranteed> {
1348 let tcx = self.tcx();
1349 match self.lower_type_relative_path(
1350 self_ty,
1351 hir_self_ty,
1352 segment,
1353 qpath_hir_id,
1354 span,
1355 LowerTypeRelativePathMode::Type(permit_variants),
1356 )? {
1357 TypeRelativePath::AssocItem(alias_term) => {
1358 let alias_ty = alias_term.expect_ty();
1359 let def_id = alias_ty.kind.def_id();
1360 let ty = alias_ty.to_ty(tcx);
1361 let ty = self.check_param_uses_if_mcg(ty, span, false);
1362 Ok((ty, tcx.def_kind(def_id), def_id))
1363 }
1364 TypeRelativePath::Variant { adt, variant_did } => {
1365 let adt = self.check_param_uses_if_mcg(adt, span, false);
1366 Ok((adt, DefKind::Variant, variant_did))
1367 }
1368 TypeRelativePath::Ctor { .. } => {
1369 let e = tcx.dcx().span_err(span, "expected type, found tuple constructor");
1370 Err(e)
1371 }
1372 }
1373 }
1374
1375 x;#[instrument(level = "debug", skip_all, ret)]
1377 fn lower_type_relative_const_path(
1378 &self,
1379 self_ty: Ty<'tcx>,
1380 hir_self_ty: &'tcx hir::Ty<'tcx>,
1381 segment: &'tcx hir::PathSegment<'tcx>,
1382 qpath_hir_id: HirId,
1383 span: Span,
1384 ) -> Result<Const<'tcx>, ErrorGuaranteed> {
1385 let tcx = self.tcx();
1386 match self.lower_type_relative_path(
1387 self_ty,
1388 hir_self_ty,
1389 segment,
1390 qpath_hir_id,
1391 span,
1392 LowerTypeRelativePathMode::Const,
1393 )? {
1394 TypeRelativePath::AssocItem(alias_term) => {
1395 let alias_ct = alias_term.expect_ct();
1396 if let Some(def_id) = alias_ct.kind.opt_def_id() {
1397 self.require_type_const_attribute(def_id, span)?;
1398 }
1399 let ct = Const::new_unevaluated(tcx, alias_ct);
1400 let ct = self.check_param_uses_if_mcg(ct, span, false);
1401 Ok(ct)
1402 }
1403 TypeRelativePath::Ctor { ctor_def_id, args } => match tcx.def_kind(ctor_def_id) {
1404 DefKind::Ctor(_, CtorKind::Fn) => {
1405 Ok(ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, ctor_def_id, args)))
1406 }
1407 DefKind::Ctor(ctor_of, CtorKind::Const) => {
1408 Ok(self.construct_const_ctor_value(ctor_def_id, ctor_of, args))
1409 }
1410 _ => unreachable!(),
1411 },
1412 TypeRelativePath::Variant { .. } => {
1415 span_bug!(span, "unexpected variant res for type associated const path")
1416 }
1417 }
1418 }
1419
1420 x;#[instrument(level = "debug", skip_all, ret)]
1422 fn lower_type_relative_path(
1423 &self,
1424 self_ty: Ty<'tcx>,
1425 hir_self_ty: &'tcx hir::Ty<'tcx>,
1426 segment: &'tcx hir::PathSegment<'tcx>,
1427 qpath_hir_id: HirId,
1428 span: Span,
1429 mode: LowerTypeRelativePathMode,
1430 ) -> Result<TypeRelativePath<'tcx>, ErrorGuaranteed> {
1431 debug!(%self_ty, ?segment.ident);
1432 let tcx = self.tcx();
1433
1434 let mut variant_def_id = None;
1436 if let Some(adt_def) = self.probe_adt(span, self_ty) {
1437 if adt_def.is_enum() {
1438 let variant_def = adt_def
1439 .variants()
1440 .iter()
1441 .find(|vd| tcx.hygienic_eq(segment.ident, vd.ident(tcx), adt_def.did()));
1442 if let Some(variant_def) = variant_def {
1443 if matches!(mode, LowerTypeRelativePathMode::Const)
1446 && let Some((_, ctor_def_id)) = variant_def.ctor
1447 {
1448 tcx.check_stability(variant_def.def_id, Some(qpath_hir_id), span, None);
1449 let _ = self.prohibit_generic_args(
1450 slice::from_ref(segment).iter(),
1451 GenericsArgsErrExtend::EnumVariant {
1452 qself: hir_self_ty,
1453 assoc_segment: segment,
1454 adt_def,
1455 },
1456 );
1457 let ty::Adt(_, enum_args) = self_ty.kind() else { unreachable!() };
1458 return Ok(TypeRelativePath::Ctor { ctor_def_id, args: enum_args });
1459 }
1460 if let PermitVariants::Yes = mode.permit_variants() {
1461 tcx.check_stability(variant_def.def_id, Some(qpath_hir_id), span, None);
1462 let _ = self.prohibit_generic_args(
1463 slice::from_ref(segment).iter(),
1464 GenericsArgsErrExtend::EnumVariant {
1465 qself: hir_self_ty,
1466 assoc_segment: segment,
1467 adt_def,
1468 },
1469 );
1470 return Ok(TypeRelativePath::Variant {
1471 adt: self_ty,
1472 variant_did: variant_def.def_id,
1473 });
1474 } else {
1475 variant_def_id = Some(variant_def.def_id);
1476 }
1477 }
1478 }
1479
1480 if let Some(alias_term) = self.probe_inherent_assoc_item(
1482 segment,
1483 adt_def.did(),
1484 self_ty,
1485 qpath_hir_id,
1486 span,
1487 mode.assoc_tag(),
1488 )? {
1489 return Ok(TypeRelativePath::AssocItem(alias_term));
1490 }
1491 }
1492
1493 let (item_def_id, bound) = self.resolve_type_relative_path(
1494 self_ty,
1495 hir_self_ty,
1496 mode.assoc_tag(),
1497 segment,
1498 qpath_hir_id,
1499 span,
1500 variant_def_id,
1501 )?;
1502
1503 let (item_def_id, args) = self.lower_assoc_item_path(span, item_def_id, segment, bound)?;
1504
1505 if let Some(variant_def_id) = variant_def_id {
1506 tcx.emit_node_span_lint(
1507 AMBIGUOUS_ASSOCIATED_ITEMS,
1508 qpath_hir_id,
1509 span,
1510 errors::AmbiguityBetweenVariantAndAssocItem {
1511 variant_def_id,
1512 item_def_id,
1513 span,
1514 segment_ident: segment.ident,
1515 bound_def_id: bound.def_id(),
1516 self_ty,
1517 tcx,
1518 mode,
1519 },
1520 );
1521 }
1522
1523 Ok(TypeRelativePath::AssocItem(ty::AliasTerm::new_from_def_id(tcx, item_def_id, args)))
1524 }
1525
1526 fn resolve_type_relative_path(
1528 &self,
1529 self_ty: Ty<'tcx>,
1530 hir_self_ty: &'tcx hir::Ty<'tcx>,
1531 assoc_tag: ty::AssocTag,
1532 segment: &'tcx hir::PathSegment<'tcx>,
1533 qpath_hir_id: HirId,
1534 span: Span,
1535 variant_def_id: Option<DefId>,
1536 ) -> Result<(DefId, ty::PolyTraitRef<'tcx>), ErrorGuaranteed> {
1537 let tcx = self.tcx();
1538
1539 let self_ty_res = match hir_self_ty.kind {
1540 hir::TyKind::Path(hir::QPath::Resolved(_, path)) => path.res,
1541 _ => Res::Err,
1542 };
1543
1544 let bound = match (self_ty.kind(), self_ty_res) {
1546 (_, Res::SelfTyAlias { alias_to: impl_def_id, is_trait_impl: true, .. }) => {
1547 let trait_ref = tcx.impl_trait_ref(impl_def_id);
1550
1551 self.probe_single_bound_for_assoc_item(
1552 || {
1553 let trait_ref =
1554 ty::Binder::dummy(trait_ref.instantiate_identity().skip_norm_wip());
1555 traits::supertraits(tcx, trait_ref)
1556 },
1557 AssocItemQSelf::SelfTyAlias,
1558 assoc_tag,
1559 segment.ident,
1560 span,
1561 None,
1562 )?
1563 }
1564 (
1565 &ty::Param(_),
1566 Res::SelfTyParam { trait_: param_did } | Res::Def(DefKind::TyParam, param_did),
1567 ) => self.probe_single_ty_param_bound_for_assoc_item(
1568 param_did.expect_local(),
1569 hir_self_ty.span,
1570 assoc_tag,
1571 segment.ident,
1572 span,
1573 )?,
1574 _ => {
1575 return Err(self.report_unresolved_type_relative_path(
1576 self_ty,
1577 hir_self_ty,
1578 assoc_tag,
1579 segment.ident,
1580 qpath_hir_id,
1581 span,
1582 variant_def_id,
1583 ));
1584 }
1585 };
1586
1587 let assoc_item = self
1588 .probe_assoc_item(segment.ident, assoc_tag, qpath_hir_id, span, bound.def_id())
1589 .expect("failed to find associated item");
1590
1591 Ok((assoc_item.def_id, bound))
1592 }
1593
1594 fn probe_inherent_assoc_item(
1596 &self,
1597 segment: &hir::PathSegment<'tcx>,
1598 adt_did: DefId,
1599 self_ty: Ty<'tcx>,
1600 block: HirId,
1601 span: Span,
1602 assoc_tag: ty::AssocTag,
1603 ) -> Result<Option<ty::AliasTerm<'tcx>>, ErrorGuaranteed> {
1604 let tcx = self.tcx();
1605
1606 if !tcx.features().inherent_associated_types() {
1607 match assoc_tag {
1608 ty::AssocTag::Type => return Ok(None),
1613 ty::AssocTag::Const => {
1614 return Err(feature_err(
1618 &tcx.sess,
1619 sym::inherent_associated_types,
1620 span,
1621 "inherent associated types are unstable",
1622 )
1623 .emit());
1624 }
1625 ty::AssocTag::Fn => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
1626 }
1627 }
1628
1629 let name = segment.ident;
1630 let candidates: Vec<_> = tcx
1631 .inherent_impls(adt_did)
1632 .iter()
1633 .filter_map(|&impl_| {
1634 let (item, scope) =
1635 self.probe_assoc_item_unchecked(name, assoc_tag, block, impl_)?;
1636 Some(InherentAssocCandidate { impl_, assoc_item: item.def_id, scope })
1637 })
1638 .collect();
1639
1640 if candidates.is_empty() {
1645 return Ok(None);
1646 }
1647
1648 let (applicable_candidates, fulfillment_errors) =
1649 self.select_inherent_assoc_candidates(span, self_ty, candidates.clone());
1650
1651 let InherentAssocCandidate { impl_, assoc_item, scope: def_scope } =
1653 match &applicable_candidates[..] {
1654 &[] => Err(self.report_unresolved_inherent_assoc_item(
1655 name,
1656 self_ty,
1657 candidates,
1658 fulfillment_errors,
1659 span,
1660 assoc_tag,
1661 )),
1662
1663 &[applicable_candidate] => Ok(applicable_candidate),
1664
1665 &[_, ..] => Err(self.report_ambiguous_inherent_assoc_item(
1666 name,
1667 candidates.into_iter().map(|cand| cand.assoc_item).collect(),
1668 span,
1669 )),
1670 }?;
1671
1672 self.check_assoc_item(assoc_item, name, def_scope, block, span);
1675
1676 let parent_args = ty::GenericArgs::identity_for_item(tcx, impl_);
1680 let args = self.lower_generic_args_of_assoc_item(span, assoc_item, segment, parent_args);
1681 let args = tcx.mk_args_from_iter(
1682 std::iter::once(ty::GenericArg::from(self_ty))
1683 .chain(args.into_iter().skip(parent_args.len())),
1684 );
1685
1686 let kind = match assoc_tag {
1687 ty::AssocTag::Type => ty::AliasTermKind::InherentTy { def_id: assoc_item },
1688 ty::AssocTag::Const => {
1689 self.require_type_const_attribute(assoc_item, span)?;
1692 ty::AliasTermKind::InherentConst { def_id: assoc_item }
1693 }
1694 ty::AssocTag::Fn => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
1695 };
1696
1697 Ok(Some(ty::AliasTerm::new_from_args(tcx, kind, args)))
1698 }
1699
1700 fn probe_assoc_item(
1704 &self,
1705 ident: Ident,
1706 assoc_tag: ty::AssocTag,
1707 block: HirId,
1708 span: Span,
1709 scope: DefId,
1710 ) -> Option<ty::AssocItem> {
1711 let (item, scope) = self.probe_assoc_item_unchecked(ident, assoc_tag, block, scope)?;
1712 self.check_assoc_item(item.def_id, ident, scope, block, span);
1713 Some(item)
1714 }
1715
1716 fn probe_assoc_item_unchecked(
1721 &self,
1722 ident: Ident,
1723 assoc_tag: ty::AssocTag,
1724 block: HirId,
1725 scope: DefId,
1726 ) -> Option<(ty::AssocItem, DefId)> {
1727 let tcx = self.tcx();
1728
1729 let (ident, def_scope) = tcx.adjust_ident_and_get_scope(ident, scope, block);
1730 let item = tcx
1734 .associated_items(scope)
1735 .filter_by_name_unhygienic(ident.name)
1736 .find(|i| i.tag() == assoc_tag && i.ident(tcx).normalize_to_macros_2_0() == ident)?;
1737
1738 Some((*item, def_scope))
1739 }
1740
1741 fn check_assoc_item(
1743 &self,
1744 item_def_id: DefId,
1745 ident: Ident,
1746 scope: DefId,
1747 block: HirId,
1748 span: Span,
1749 ) {
1750 let tcx = self.tcx();
1751
1752 if !tcx.visibility(item_def_id).is_accessible_from(scope, tcx) {
1753 self.dcx().emit_err(crate::diagnostics::AssocItemIsPrivate {
1754 span,
1755 kind: tcx.def_descr(item_def_id),
1756 name: ident,
1757 defined_here_label: tcx.def_span(item_def_id),
1758 });
1759 }
1760
1761 tcx.check_stability(item_def_id, Some(block), span, None);
1762 }
1763
1764 fn probe_traits_that_match_assoc_ty(
1765 &self,
1766 qself_ty: Ty<'tcx>,
1767 assoc_ident: Ident,
1768 ) -> Vec<String> {
1769 let tcx = self.tcx();
1770
1771 let infcx_;
1774 let infcx = if let Some(infcx) = self.infcx() {
1775 infcx
1776 } else {
1777 if !!qself_ty.has_infer() {
::core::panicking::panic("assertion failed: !qself_ty.has_infer()")
};assert!(!qself_ty.has_infer());
1778 infcx_ = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
1779 &infcx_
1780 };
1781
1782 tcx.all_traits_including_private()
1783 .filter(|trait_def_id| {
1784 tcx.associated_items(*trait_def_id)
1786 .in_definition_order()
1787 .any(|i| {
1788 i.is_type()
1789 && !i.is_impl_trait_in_trait()
1790 && i.ident(tcx).normalize_to_macros_2_0() == assoc_ident
1791 })
1792 && tcx.visibility(*trait_def_id)
1794 .is_accessible_from(self.item_def_id(), tcx)
1795 && tcx.all_impls(*trait_def_id)
1796 .any(|impl_def_id| {
1797 let header = tcx.impl_trait_header(impl_def_id);
1798 let trait_ref = header.trait_ref.instantiate(tcx, infcx.fresh_args_for_item(DUMMY_SP, impl_def_id)).skip_norm_wip();
1799
1800 let value = fold_regions(tcx, qself_ty, |_, _| tcx.lifetimes.re_erased);
1801 if value.has_escaping_bound_vars() {
1803 return false;
1804 }
1805 infcx
1806 .can_eq(
1807 ty::ParamEnv::empty(),
1808 trait_ref.self_ty(),
1809 value,
1810 ) && header.polarity != ty::ImplPolarity::Negative
1811 })
1812 })
1813 .map(|trait_def_id| tcx.def_path_str(trait_def_id))
1814 .collect()
1815 }
1816
1817 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("lower_resolved_assoc_ty_path",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(1818u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&[],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{ meta.fields().value_set(&[]) })
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Ty<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
match self.lower_resolved_assoc_item_path(span, opt_self_ty,
item_def_id, trait_segment, item_segment,
ty::AssocTag::Type) {
Ok((item_def_id, item_args)) => {
Ty::new_projection_from_args(self.tcx(), item_def_id,
item_args)
}
Err(guar) => Ty::new_error(self.tcx(), guar),
}
}
}
}#[instrument(level = "debug", skip_all)]
1819 fn lower_resolved_assoc_ty_path(
1820 &self,
1821 span: Span,
1822 opt_self_ty: Option<Ty<'tcx>>,
1823 item_def_id: DefId,
1824 trait_segment: Option<&hir::PathSegment<'tcx>>,
1825 item_segment: &hir::PathSegment<'tcx>,
1826 ) -> Ty<'tcx> {
1827 match self.lower_resolved_assoc_item_path(
1828 span,
1829 opt_self_ty,
1830 item_def_id,
1831 trait_segment,
1832 item_segment,
1833 ty::AssocTag::Type,
1834 ) {
1835 Ok((item_def_id, item_args)) => {
1836 Ty::new_projection_from_args(self.tcx(), item_def_id, item_args)
1837 }
1838 Err(guar) => Ty::new_error(self.tcx(), guar),
1839 }
1840 }
1841
1842 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("lower_resolved_assoc_const_path",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(1843u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&[],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{ meta.fields().value_set(&[]) })
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return:
Result<Const<'tcx>, ErrorGuaranteed> = loop {};
return __tracing_attr_fake_return;
}
{
let tcx = self.tcx();
let (item_def_id, item_args) =
self.lower_resolved_assoc_item_path(span, opt_self_ty,
item_def_id, trait_segment, item_segment,
ty::AssocTag::Const)?;
self.require_type_const_attribute(item_def_id, span)?;
let uv =
ty::UnevaluatedConst::new(tcx,
ty::UnevaluatedConstKind::new_from_def_id(tcx, item_def_id),
item_args);
Ok(Const::new_unevaluated(tcx, uv))
}
}
}#[instrument(level = "debug", skip_all)]
1844 fn lower_resolved_assoc_const_path(
1845 &self,
1846 span: Span,
1847 opt_self_ty: Option<Ty<'tcx>>,
1848 item_def_id: DefId,
1849 trait_segment: Option<&hir::PathSegment<'tcx>>,
1850 item_segment: &hir::PathSegment<'tcx>,
1851 ) -> Result<Const<'tcx>, ErrorGuaranteed> {
1852 let tcx = self.tcx();
1853 let (item_def_id, item_args) = self.lower_resolved_assoc_item_path(
1854 span,
1855 opt_self_ty,
1856 item_def_id,
1857 trait_segment,
1858 item_segment,
1859 ty::AssocTag::Const,
1860 )?;
1861 self.require_type_const_attribute(item_def_id, span)?;
1862 let uv = ty::UnevaluatedConst::new(
1863 tcx,
1864 ty::UnevaluatedConstKind::new_from_def_id(tcx, item_def_id),
1865 item_args,
1866 );
1867 Ok(Const::new_unevaluated(tcx, uv))
1868 }
1869
1870 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("lower_resolved_assoc_item_path",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(1871u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&[],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{ meta.fields().value_set(&[]) })
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return:
Result<(DefId, GenericArgsRef<'tcx>), ErrorGuaranteed> =
loop {};
return __tracing_attr_fake_return;
}
{
let tcx = self.tcx();
let trait_def_id = tcx.parent(item_def_id);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:1884",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(1884u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["trait_def_id"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&trait_def_id)
as &dyn Value))])
});
} else { ; }
};
let Some(self_ty) =
opt_self_ty else {
return Err(self.report_missing_self_ty_for_resolved_path(trait_def_id,
span, item_segment, assoc_tag));
};
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:1894",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(1894u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["self_ty"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&self_ty) as
&dyn Value))])
});
} else { ; }
};
let trait_ref =
self.lower_mono_trait_ref(span, trait_def_id, self_ty,
trait_segment.unwrap(), false);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:1898",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(1898u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["trait_ref"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&trait_ref)
as &dyn Value))])
});
} else { ; }
};
let item_args =
self.lower_generic_args_of_assoc_item(span, item_def_id,
item_segment, trait_ref.args);
Ok((item_def_id, item_args))
}
}
}#[instrument(level = "debug", skip_all)]
1872 fn lower_resolved_assoc_item_path(
1873 &self,
1874 span: Span,
1875 opt_self_ty: Option<Ty<'tcx>>,
1876 item_def_id: DefId,
1877 trait_segment: Option<&hir::PathSegment<'tcx>>,
1878 item_segment: &hir::PathSegment<'tcx>,
1879 assoc_tag: ty::AssocTag,
1880 ) -> Result<(DefId, GenericArgsRef<'tcx>), ErrorGuaranteed> {
1881 let tcx = self.tcx();
1882
1883 let trait_def_id = tcx.parent(item_def_id);
1884 debug!(?trait_def_id);
1885
1886 let Some(self_ty) = opt_self_ty else {
1887 return Err(self.report_missing_self_ty_for_resolved_path(
1888 trait_def_id,
1889 span,
1890 item_segment,
1891 assoc_tag,
1892 ));
1893 };
1894 debug!(?self_ty);
1895
1896 let trait_ref =
1897 self.lower_mono_trait_ref(span, trait_def_id, self_ty, trait_segment.unwrap(), false);
1898 debug!(?trait_ref);
1899
1900 let item_args =
1901 self.lower_generic_args_of_assoc_item(span, item_def_id, item_segment, trait_ref.args);
1902
1903 Ok((item_def_id, item_args))
1904 }
1905
1906 pub fn prohibit_generic_args<'a>(
1907 &self,
1908 segments: impl Iterator<Item = &'a hir::PathSegment<'a>> + Clone,
1909 err_extend: GenericsArgsErrExtend<'a>,
1910 ) -> Result<(), ErrorGuaranteed> {
1911 let args_visitors = segments.clone().flat_map(|segment| segment.args().args);
1912 let mut result = Ok(());
1913 if let Some(_) = args_visitors.clone().next() {
1914 result = Err(self.report_prohibited_generic_args(
1915 segments.clone(),
1916 args_visitors,
1917 err_extend,
1918 ));
1919 }
1920
1921 for segment in segments {
1922 if let Some(c) = segment.args().constraints.first() {
1924 return Err(prohibit_assoc_item_constraint(self, c, None));
1925 }
1926 }
1927
1928 result
1929 }
1930
1931 pub fn probe_generic_path_segments(
1949 &self,
1950 segments: &[hir::PathSegment<'_>],
1951 self_ty: Option<Ty<'tcx>>,
1952 kind: DefKind,
1953 def_id: DefId,
1954 span: Span,
1955 ) -> Vec<GenericPathSegment> {
1956 let tcx = self.tcx();
2002
2003 if !!segments.is_empty() {
::core::panicking::panic("assertion failed: !segments.is_empty()")
};assert!(!segments.is_empty());
2004 let last = segments.len() - 1;
2005
2006 let mut generic_segments = ::alloc::vec::Vec::new()vec![];
2007
2008 match kind {
2009 DefKind::Ctor(CtorOf::Struct, ..) => {
2011 let generics = tcx.generics_of(def_id);
2014 let generics_def_id = generics.parent.unwrap_or(def_id);
2017 generic_segments.push(GenericPathSegment(generics_def_id, last));
2018 }
2019
2020 DefKind::Ctor(CtorOf::Variant, ..) | DefKind::Variant => {
2022 let (generics_def_id, index) = if let Some(self_ty) = self_ty {
2023 let adt_def = self.probe_adt(span, self_ty).unwrap();
2026 if true {
if !adt_def.is_enum() {
::core::panicking::panic("assertion failed: adt_def.is_enum()")
};
};debug_assert!(adt_def.is_enum());
2027
2028 (adt_def.did(), last)
2040 } else if let [.., second_to_last, _] = segments
2041 && second_to_last.args.is_some()
2042 && let Res::Def(DefKind::Enum, _) = second_to_last.res
2043 {
2044 let def_id = match kind {
2053 DefKind::Ctor(..) => tcx.parent(def_id),
2054 _ => def_id,
2055 };
2056
2057 let enum_def_id = tcx.parent(def_id);
2059
2060 (enum_def_id, last - 1)
2061 } else {
2062 let generics = tcx.generics_of(def_id);
2069 (generics.parent.unwrap_or(def_id), last)
2072 };
2073 generic_segments.push(GenericPathSegment(generics_def_id, index));
2074 }
2075
2076 DefKind::Fn | DefKind::Const { .. } | DefKind::ConstParam | DefKind::Static { .. } => {
2078 generic_segments.push(GenericPathSegment(def_id, last));
2079 }
2080
2081 DefKind::AssocFn | DefKind::AssocConst { .. } => {
2083 if segments.len() >= 2 {
2084 let generics = tcx.generics_of(def_id);
2085 generic_segments.push(GenericPathSegment(generics.parent.unwrap(), last - 1));
2086 }
2087 generic_segments.push(GenericPathSegment(def_id, last));
2088 }
2089
2090 kind => ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected definition kind {0:?} for {1:?}",
kind, def_id))bug!("unexpected definition kind {:?} for {:?}", kind, def_id),
2091 }
2092
2093 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:2093",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(2093u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["generic_segments"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&generic_segments)
as &dyn Value))])
});
} else { ; }
};debug!(?generic_segments);
2094
2095 generic_segments
2096 }
2097
2098 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("lower_resolved_ty_path",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(2099u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&[],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{ meta.fields().value_set(&[]) })
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Ty<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:2107",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(2107u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["path.res",
"opt_self_ty", "path.segments"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&path.res)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&opt_self_ty)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&path.segments)
as &dyn Value))])
});
} else { ; }
};
let tcx = self.tcx();
let span = path.span;
match path.res {
Res::Def(DefKind::OpaqueTy, did) => {
{
match tcx.opaque_ty_origin(did) {
hir::OpaqueTyOrigin::TyAlias { .. } => {}
ref left_val => {
::core::panicking::assert_matches_failed(left_val,
"hir::OpaqueTyOrigin::TyAlias { .. }",
::core::option::Option::None);
}
}
};
let [leading_segments @ .., segment] =
path.segments else {
::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))
};
let _ =
self.prohibit_generic_args(leading_segments.iter(),
GenericsArgsErrExtend::OpaqueTy);
let args =
self.lower_generic_args_of_path_segment(span, did, segment);
Ty::new_opaque(tcx, did, args)
}
Res::Def(DefKind::Enum | DefKind::TyAlias | DefKind::Struct |
DefKind::Union | DefKind::ForeignTy, did) => {
match (&opt_self_ty, &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
let [leading_segments @ .., segment] =
path.segments else {
::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))
};
let _ =
self.prohibit_generic_args(leading_segments.iter(),
GenericsArgsErrExtend::None);
self.lower_path_segment(span, did, segment)
}
Res::Def(kind @ DefKind::Variant, def_id) if
let PermitVariants::Yes = permit_variants => {
match (&opt_self_ty, &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
let generic_segments =
self.probe_generic_path_segments(path.segments, None, kind,
def_id, span);
let indices: FxHashSet<_> =
generic_segments.iter().map(|GenericPathSegment(_, index)|
index).collect();
let _ =
self.prohibit_generic_args(path.segments.iter().enumerate().filter_map(|(index,
seg)|
{
if !indices.contains(&index) { Some(seg) } else { None }
}), GenericsArgsErrExtend::DefVariant(&path.segments));
let &GenericPathSegment(def_id, index) =
generic_segments.last().unwrap();
self.lower_path_segment(span, def_id, &path.segments[index])
}
Res::Def(DefKind::TyParam, def_id) => {
match (&opt_self_ty, &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
let _ =
self.prohibit_generic_args(path.segments.iter(),
GenericsArgsErrExtend::Param(def_id));
self.lower_ty_param(hir_id)
}
Res::SelfTyParam { .. } => {
match (&opt_self_ty, &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
let _ =
self.prohibit_generic_args(path.segments.iter(),
if let [hir::PathSegment { args: Some(args), ident, .. }] =
&path.segments {
GenericsArgsErrExtend::SelfTyParam(ident.span.shrink_to_hi().to(args.span_ext))
} else { GenericsArgsErrExtend::None });
self.check_param_uses_if_mcg(tcx.types.self_param, span,
false)
}
Res::SelfTyAlias { alias_to: def_id, .. } => {
match (&opt_self_ty, &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
let ty =
tcx.at(span).type_of(def_id).instantiate_identity().skip_norm_wip();
let _ =
self.prohibit_generic_args(path.segments.iter(),
GenericsArgsErrExtend::SelfTyAlias { def_id, span });
self.check_param_uses_if_mcg(ty, span, true)
}
Res::Def(DefKind::AssocTy, def_id) => {
let trait_segment =
if let [modules @ .., trait_, _item] = path.segments {
let _ =
self.prohibit_generic_args(modules.iter(),
GenericsArgsErrExtend::None);
Some(trait_)
} else { None };
self.lower_resolved_assoc_ty_path(span, opt_self_ty, def_id,
trait_segment, path.segments.last().unwrap())
}
Res::PrimTy(prim_ty) => {
match (&opt_self_ty, &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
let _ =
self.prohibit_generic_args(path.segments.iter(),
GenericsArgsErrExtend::PrimTy(prim_ty));
match prim_ty {
hir::PrimTy::Bool => tcx.types.bool,
hir::PrimTy::Char => tcx.types.char,
hir::PrimTy::Int(it) => Ty::new_int(tcx, it),
hir::PrimTy::Uint(uit) => Ty::new_uint(tcx, uit),
hir::PrimTy::Float(ft) => Ty::new_float(tcx, ft),
hir::PrimTy::Str => tcx.types.str_,
}
}
Res::Err => {
let e =
self.tcx().dcx().span_delayed_bug(path.span,
"path with `Res::Err` but no error emitted");
Ty::new_error(tcx, e)
}
Res::Def(..) => {
match (&path.segments.get(0).map(|seg| seg.ident.name),
&Some(kw::SelfUpper)) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val,
::core::option::Option::Some(format_args!("only expected incorrect resolution for `Self`")));
}
}
};
Ty::new_error(self.tcx(),
self.dcx().span_delayed_bug(span,
"incorrect resolution for `Self`"))
}
_ =>
::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("unexpected resolution: {0:?}", path.res)),
}
}
}
}#[instrument(level = "debug", skip_all)]
2100 pub fn lower_resolved_ty_path(
2101 &self,
2102 opt_self_ty: Option<Ty<'tcx>>,
2103 path: &hir::Path<'tcx>,
2104 hir_id: HirId,
2105 permit_variants: PermitVariants,
2106 ) -> Ty<'tcx> {
2107 debug!(?path.res, ?opt_self_ty, ?path.segments);
2108 let tcx = self.tcx();
2109
2110 let span = path.span;
2111 match path.res {
2112 Res::Def(DefKind::OpaqueTy, did) => {
2113 assert_matches!(tcx.opaque_ty_origin(did), hir::OpaqueTyOrigin::TyAlias { .. });
2115 let [leading_segments @ .., segment] = path.segments else { bug!() };
2116 let _ = self.prohibit_generic_args(
2117 leading_segments.iter(),
2118 GenericsArgsErrExtend::OpaqueTy,
2119 );
2120 let args = self.lower_generic_args_of_path_segment(span, did, segment);
2121 Ty::new_opaque(tcx, did, args)
2122 }
2123 Res::Def(
2124 DefKind::Enum
2125 | DefKind::TyAlias
2126 | DefKind::Struct
2127 | DefKind::Union
2128 | DefKind::ForeignTy,
2129 did,
2130 ) => {
2131 assert_eq!(opt_self_ty, None);
2132 let [leading_segments @ .., segment] = path.segments else { bug!() };
2133 let _ = self
2134 .prohibit_generic_args(leading_segments.iter(), GenericsArgsErrExtend::None);
2135 self.lower_path_segment(span, did, segment)
2136 }
2137 Res::Def(kind @ DefKind::Variant, def_id)
2138 if let PermitVariants::Yes = permit_variants =>
2139 {
2140 assert_eq!(opt_self_ty, None);
2143
2144 let generic_segments =
2145 self.probe_generic_path_segments(path.segments, None, kind, def_id, span);
2146 let indices: FxHashSet<_> =
2147 generic_segments.iter().map(|GenericPathSegment(_, index)| index).collect();
2148 let _ = self.prohibit_generic_args(
2149 path.segments.iter().enumerate().filter_map(|(index, seg)| {
2150 if !indices.contains(&index) { Some(seg) } else { None }
2151 }),
2152 GenericsArgsErrExtend::DefVariant(&path.segments),
2153 );
2154
2155 let &GenericPathSegment(def_id, index) = generic_segments.last().unwrap();
2156 self.lower_path_segment(span, def_id, &path.segments[index])
2157 }
2158 Res::Def(DefKind::TyParam, def_id) => {
2159 assert_eq!(opt_self_ty, None);
2160 let _ = self.prohibit_generic_args(
2161 path.segments.iter(),
2162 GenericsArgsErrExtend::Param(def_id),
2163 );
2164 self.lower_ty_param(hir_id)
2165 }
2166 Res::SelfTyParam { .. } => {
2167 assert_eq!(opt_self_ty, None);
2169 let _ = self.prohibit_generic_args(
2170 path.segments.iter(),
2171 if let [hir::PathSegment { args: Some(args), ident, .. }] = &path.segments {
2172 GenericsArgsErrExtend::SelfTyParam(
2173 ident.span.shrink_to_hi().to(args.span_ext),
2174 )
2175 } else {
2176 GenericsArgsErrExtend::None
2177 },
2178 );
2179 self.check_param_uses_if_mcg(tcx.types.self_param, span, false)
2180 }
2181 Res::SelfTyAlias { alias_to: def_id, .. } => {
2182 assert_eq!(opt_self_ty, None);
2184 let ty = tcx.at(span).type_of(def_id).instantiate_identity().skip_norm_wip();
2186 let _ = self.prohibit_generic_args(
2187 path.segments.iter(),
2188 GenericsArgsErrExtend::SelfTyAlias { def_id, span },
2189 );
2190 self.check_param_uses_if_mcg(ty, span, true)
2191 }
2192 Res::Def(DefKind::AssocTy, def_id) => {
2193 let trait_segment = if let [modules @ .., trait_, _item] = path.segments {
2194 let _ = self.prohibit_generic_args(modules.iter(), GenericsArgsErrExtend::None);
2195 Some(trait_)
2196 } else {
2197 None
2198 };
2199 self.lower_resolved_assoc_ty_path(
2200 span,
2201 opt_self_ty,
2202 def_id,
2203 trait_segment,
2204 path.segments.last().unwrap(),
2205 )
2206 }
2207 Res::PrimTy(prim_ty) => {
2208 assert_eq!(opt_self_ty, None);
2209 let _ = self.prohibit_generic_args(
2210 path.segments.iter(),
2211 GenericsArgsErrExtend::PrimTy(prim_ty),
2212 );
2213 match prim_ty {
2214 hir::PrimTy::Bool => tcx.types.bool,
2215 hir::PrimTy::Char => tcx.types.char,
2216 hir::PrimTy::Int(it) => Ty::new_int(tcx, it),
2217 hir::PrimTy::Uint(uit) => Ty::new_uint(tcx, uit),
2218 hir::PrimTy::Float(ft) => Ty::new_float(tcx, ft),
2219 hir::PrimTy::Str => tcx.types.str_,
2220 }
2221 }
2222 Res::Err => {
2223 let e = self
2224 .tcx()
2225 .dcx()
2226 .span_delayed_bug(path.span, "path with `Res::Err` but no error emitted");
2227 Ty::new_error(tcx, e)
2228 }
2229 Res::Def(..) => {
2230 assert_eq!(
2231 path.segments.get(0).map(|seg| seg.ident.name),
2232 Some(kw::SelfUpper),
2233 "only expected incorrect resolution for `Self`"
2234 );
2235 Ty::new_error(
2236 self.tcx(),
2237 self.dcx().span_delayed_bug(span, "incorrect resolution for `Self`"),
2238 )
2239 }
2240 _ => span_bug!(span, "unexpected resolution: {:?}", path.res),
2241 }
2242 }
2243
2244 pub(crate) fn lower_ty_param(&self, hir_id: HirId) -> Ty<'tcx> {
2249 let tcx = self.tcx();
2250
2251 let ty = match tcx.named_bound_var(hir_id) {
2252 Some(rbv::ResolvedArg::LateBound(debruijn, index, def_id)) => {
2253 let br = ty::BoundTy {
2254 var: ty::BoundVar::from_u32(index),
2255 kind: ty::BoundTyKind::Param(def_id.to_def_id()),
2256 };
2257 Ty::new_bound(tcx, debruijn, br)
2258 }
2259 Some(rbv::ResolvedArg::EarlyBound(def_id)) => {
2260 let item_def_id = tcx.hir_ty_param_owner(def_id);
2261 let generics = tcx.generics_of(item_def_id);
2262 let index = generics.param_def_id_to_index[&def_id.to_def_id()];
2263 Ty::new_param(tcx, index, tcx.hir_ty_param_name(def_id))
2264 }
2265 Some(rbv::ResolvedArg::Error(guar)) => Ty::new_error(tcx, guar),
2266 arg => ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected bound var resolution for {0:?}: {1:?}",
hir_id, arg))bug!("unexpected bound var resolution for {hir_id:?}: {arg:?}"),
2267 };
2268 self.check_param_uses_if_mcg(ty, tcx.hir_span(hir_id), false)
2269 }
2270
2271 pub(crate) fn lower_const_param(&self, param_def_id: DefId, path_hir_id: HirId) -> Const<'tcx> {
2276 let tcx = self.tcx();
2277
2278 let ct = match tcx.named_bound_var(path_hir_id) {
2279 Some(rbv::ResolvedArg::EarlyBound(_)) => {
2280 let item_def_id = tcx.parent(param_def_id);
2283 let generics = tcx.generics_of(item_def_id);
2284 let index = generics.param_def_id_to_index[¶m_def_id];
2285 let name = tcx.item_name(param_def_id);
2286 ty::Const::new_param(tcx, ty::ParamConst::new(index, name))
2287 }
2288 Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => ty::Const::new_bound(
2289 tcx,
2290 debruijn,
2291 ty::BoundConst::new(ty::BoundVar::from_u32(index)),
2292 ),
2293 Some(rbv::ResolvedArg::Error(guar)) => ty::Const::new_error(tcx, guar),
2294 arg => ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected bound var resolution for {0:?}: {1:?}",
path_hir_id, arg))bug!("unexpected bound var resolution for {:?}: {arg:?}", path_hir_id),
2295 };
2296 self.check_param_uses_if_mcg(ct, tcx.hir_span(path_hir_id), false)
2297 }
2298
2299 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("lower_const_arg",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(2300u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["const_arg", "ty"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&const_arg)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&ty)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Const<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
let tcx = self.tcx();
if let hir::ConstArgKind::Anon(anon) = &const_arg.kind {
if tcx.features().generic_const_parameter_types() &&
(ty.has_free_regions() || ty.has_erased_regions()) {
let e =
self.dcx().span_err(const_arg.span,
"anonymous constants with lifetimes in their type are not yet supported");
tcx.feed_anon_const_type(anon.def_id,
ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
return ty::Const::new_error(tcx, e);
}
if ty.has_non_region_infer() {
let e =
self.dcx().span_err(const_arg.span,
"anonymous constants with inferred types are not yet supported");
tcx.feed_anon_const_type(anon.def_id,
ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
return ty::Const::new_error(tcx, e);
}
if ty.has_non_region_param() {
let e =
self.dcx().span_err(const_arg.span,
"anonymous constants referencing generics are not yet supported");
tcx.feed_anon_const_type(anon.def_id,
ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
return ty::Const::new_error(tcx, e);
}
tcx.feed_anon_const_type(anon.def_id,
ty::EarlyBinder::bind(ty));
}
let hir_id = const_arg.hir_id;
match const_arg.kind {
hir::ConstArgKind::Tup(exprs) =>
self.lower_const_arg_tup(exprs, ty, const_arg.span),
hir::ConstArgKind::Path(hir::QPath::Resolved(maybe_qself,
path)) => {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:2352",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(2352u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["maybe_qself",
"path"], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&maybe_qself)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&path) as
&dyn Value))])
});
} else { ; }
};
let opt_self_ty =
maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
self.lower_resolved_const_path(opt_self_ty, path, hir_id)
}
hir::ConstArgKind::Path(hir::QPath::TypeRelative(hir_self_ty,
segment)) => {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:2357",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(2357u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["hir_self_ty",
"segment"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&hir_self_ty)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&segment) as
&dyn Value))])
});
} else { ; }
};
let self_ty = self.lower_ty(hir_self_ty);
self.lower_type_relative_const_path(self_ty, hir_self_ty,
segment, hir_id,
const_arg.span).unwrap_or_else(|guar|
Const::new_error(tcx, guar))
}
hir::ConstArgKind::Struct(qpath, inits) => {
self.lower_const_arg_struct(hir_id, qpath, inits,
const_arg.span)
}
hir::ConstArgKind::TupleCall(qpath, args) => {
self.lower_const_arg_tuple_call(hir_id, qpath, args,
const_arg.span)
}
hir::ConstArgKind::Array(array_expr) =>
self.lower_const_arg_array(array_expr, ty),
hir::ConstArgKind::Anon(anon) =>
self.lower_const_arg_anon(anon),
hir::ConstArgKind::Infer(()) =>
self.ct_infer(None, const_arg.span),
hir::ConstArgKind::Error(e) => ty::Const::new_error(tcx, e),
hir::ConstArgKind::Literal { lit, negated } => {
self.lower_const_arg_literal(&lit, negated, ty,
const_arg.span)
}
}
}
}
}#[instrument(skip(self), level = "debug")]
2301 pub fn lower_const_arg(&self, const_arg: &hir::ConstArg<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
2302 let tcx = self.tcx();
2303
2304 if let hir::ConstArgKind::Anon(anon) = &const_arg.kind {
2305 if tcx.features().generic_const_parameter_types()
2314 && (ty.has_free_regions() || ty.has_erased_regions())
2315 {
2316 let e = self.dcx().span_err(
2317 const_arg.span,
2318 "anonymous constants with lifetimes in their type are not yet supported",
2319 );
2320 tcx.feed_anon_const_type(anon.def_id, ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
2321 return ty::Const::new_error(tcx, e);
2322 }
2323 if ty.has_non_region_infer() {
2327 let e = self.dcx().span_err(
2328 const_arg.span,
2329 "anonymous constants with inferred types are not yet supported",
2330 );
2331 tcx.feed_anon_const_type(anon.def_id, ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
2332 return ty::Const::new_error(tcx, e);
2333 }
2334 if ty.has_non_region_param() {
2337 let e = self.dcx().span_err(
2338 const_arg.span,
2339 "anonymous constants referencing generics are not yet supported",
2340 );
2341 tcx.feed_anon_const_type(anon.def_id, ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
2342 return ty::Const::new_error(tcx, e);
2343 }
2344
2345 tcx.feed_anon_const_type(anon.def_id, ty::EarlyBinder::bind(ty));
2346 }
2347
2348 let hir_id = const_arg.hir_id;
2349 match const_arg.kind {
2350 hir::ConstArgKind::Tup(exprs) => self.lower_const_arg_tup(exprs, ty, const_arg.span),
2351 hir::ConstArgKind::Path(hir::QPath::Resolved(maybe_qself, path)) => {
2352 debug!(?maybe_qself, ?path);
2353 let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
2354 self.lower_resolved_const_path(opt_self_ty, path, hir_id)
2355 }
2356 hir::ConstArgKind::Path(hir::QPath::TypeRelative(hir_self_ty, segment)) => {
2357 debug!(?hir_self_ty, ?segment);
2358 let self_ty = self.lower_ty(hir_self_ty);
2359 self.lower_type_relative_const_path(
2360 self_ty,
2361 hir_self_ty,
2362 segment,
2363 hir_id,
2364 const_arg.span,
2365 )
2366 .unwrap_or_else(|guar| Const::new_error(tcx, guar))
2367 }
2368 hir::ConstArgKind::Struct(qpath, inits) => {
2369 self.lower_const_arg_struct(hir_id, qpath, inits, const_arg.span)
2370 }
2371 hir::ConstArgKind::TupleCall(qpath, args) => {
2372 self.lower_const_arg_tuple_call(hir_id, qpath, args, const_arg.span)
2373 }
2374 hir::ConstArgKind::Array(array_expr) => self.lower_const_arg_array(array_expr, ty),
2375 hir::ConstArgKind::Anon(anon) => self.lower_const_arg_anon(anon),
2376 hir::ConstArgKind::Infer(()) => self.ct_infer(None, const_arg.span),
2377 hir::ConstArgKind::Error(e) => ty::Const::new_error(tcx, e),
2378 hir::ConstArgKind::Literal { lit, negated } => {
2379 self.lower_const_arg_literal(&lit, negated, ty, const_arg.span)
2380 }
2381 }
2382 }
2383
2384 fn lower_const_arg_array(
2385 &self,
2386 array_expr: &'tcx hir::ConstArgArrayExpr<'tcx>,
2387 ty: Ty<'tcx>,
2388 ) -> Const<'tcx> {
2389 let tcx = self.tcx();
2390
2391 let elem_ty = match ty.kind() {
2392 ty::Array(elem_ty, _) => elem_ty,
2393 ty::Error(e) => return Const::new_error(tcx, *e),
2394 _ => {
2395 let e = tcx
2396 .dcx()
2397 .span_err(array_expr.span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected `{0}`, found const array",
ty))
})format!("expected `{}`, found const array", ty));
2398 return Const::new_error(tcx, e);
2399 }
2400 };
2401
2402 let elems = array_expr
2403 .elems
2404 .iter()
2405 .map(|elem| self.lower_const_arg(elem, *elem_ty))
2406 .collect::<Vec<_>>();
2407
2408 let valtree = ty::ValTree::from_branches(tcx, elems);
2409
2410 ty::Const::new_value(tcx, valtree, ty)
2411 }
2412
2413 fn lower_const_arg_tuple_call(
2414 &self,
2415 hir_id: HirId,
2416 qpath: hir::QPath<'tcx>,
2417 args: &'tcx [&'tcx hir::ConstArg<'tcx>],
2418 span: Span,
2419 ) -> Const<'tcx> {
2420 let tcx = self.tcx();
2421
2422 let non_adt_or_variant_res = || {
2423 let e = tcx.dcx().span_err(span, "tuple constructor with invalid base path");
2424 ty::Const::new_error(tcx, e)
2425 };
2426
2427 let ctor_const = match qpath {
2428 hir::QPath::Resolved(maybe_qself, path) => {
2429 let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
2430 self.lower_resolved_const_path(opt_self_ty, path, hir_id)
2431 }
2432 hir::QPath::TypeRelative(hir_self_ty, segment) => {
2433 let self_ty = self.lower_ty(hir_self_ty);
2434 match self.lower_type_relative_const_path(
2435 self_ty,
2436 hir_self_ty,
2437 segment,
2438 hir_id,
2439 span,
2440 ) {
2441 Ok(c) => c,
2442 Err(_) => return non_adt_or_variant_res(),
2443 }
2444 }
2445 };
2446
2447 let Some(value) = ctor_const.try_to_value() else {
2448 return non_adt_or_variant_res();
2449 };
2450
2451 let (adt_def, adt_args, variant_did) = match value.ty.kind() {
2452 ty::FnDef(def_id, fn_args)
2453 if let DefKind::Ctor(CtorOf::Variant, _) = tcx.def_kind(*def_id) =>
2454 {
2455 let parent_did = tcx.parent(*def_id);
2456 let enum_did = tcx.parent(parent_did);
2457 (tcx.adt_def(enum_did), fn_args, parent_did)
2458 }
2459 ty::FnDef(def_id, fn_args)
2460 if let DefKind::Ctor(CtorOf::Struct, _) = tcx.def_kind(*def_id) =>
2461 {
2462 let parent_did = tcx.parent(*def_id);
2463 (tcx.adt_def(parent_did), fn_args, parent_did)
2464 }
2465 _ => {
2466 let e = self.dcx().span_err(
2467 span,
2468 "complex const arguments must be placed inside of a `const` block",
2469 );
2470 return Const::new_error(tcx, e);
2471 }
2472 };
2473
2474 let variant_def = adt_def.variant_with_id(variant_did);
2475 let variant_idx = adt_def.variant_index_with_id(variant_did).as_u32();
2476
2477 if args.len() != variant_def.fields.len() {
2478 let e = tcx.dcx().span_err(
2479 span,
2480 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("tuple constructor has {0} arguments but {1} were provided",
variant_def.fields.len(), args.len()))
})format!(
2481 "tuple constructor has {} arguments but {} were provided",
2482 variant_def.fields.len(),
2483 args.len()
2484 ),
2485 );
2486 return ty::Const::new_error(tcx, e);
2487 }
2488
2489 let fields = variant_def
2490 .fields
2491 .iter()
2492 .zip(args)
2493 .map(|(field_def, arg)| {
2494 self.lower_const_arg(
2495 arg,
2496 tcx.type_of(field_def.did).instantiate(tcx, adt_args).skip_norm_wip(),
2497 )
2498 })
2499 .collect::<Vec<_>>();
2500
2501 let opt_discr_const = if adt_def.is_enum() {
2502 let valtree = ty::ValTree::from_scalar_int(tcx, variant_idx.into());
2503 Some(ty::Const::new_value(tcx, valtree, tcx.types.u32))
2504 } else {
2505 None
2506 };
2507
2508 let valtree = ty::ValTree::from_branches(tcx, opt_discr_const.into_iter().chain(fields));
2509 let adt_ty = Ty::new_adt(tcx, adt_def, adt_args);
2510 ty::Const::new_value(tcx, valtree, adt_ty)
2511 }
2512
2513 fn lower_const_arg_tup(
2514 &self,
2515 exprs: &'tcx [&'tcx hir::ConstArg<'tcx>],
2516 ty: Ty<'tcx>,
2517 span: Span,
2518 ) -> Const<'tcx> {
2519 let tcx = self.tcx();
2520
2521 let tys = match ty.kind() {
2522 ty::Tuple(tys) => tys,
2523 ty::Error(e) => return Const::new_error(tcx, *e),
2524 _ => {
2525 let e = tcx.dcx().span_err(span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected `{0}`, found const tuple",
ty))
})format!("expected `{}`, found const tuple", ty));
2526 return Const::new_error(tcx, e);
2527 }
2528 };
2529
2530 let exprs = exprs
2531 .iter()
2532 .zip(tys.iter())
2533 .map(|(expr, ty)| self.lower_const_arg(expr, ty))
2534 .collect::<Vec<_>>();
2535
2536 let valtree = ty::ValTree::from_branches(tcx, exprs);
2537 ty::Const::new_value(tcx, valtree, ty)
2538 }
2539
2540 fn lower_const_arg_struct(
2541 &self,
2542 hir_id: HirId,
2543 qpath: hir::QPath<'tcx>,
2544 inits: &'tcx [&'tcx hir::ConstArgExprField<'tcx>],
2545 span: Span,
2546 ) -> Const<'tcx> {
2547 let tcx = self.tcx();
2550
2551 let non_adt_or_variant_res = || {
2552 let e = tcx.dcx().span_err(span, "struct expression with invalid base path");
2553 ty::Const::new_error(tcx, e)
2554 };
2555
2556 let ResolvedStructPath { res: opt_res, ty } =
2557 self.lower_path_for_struct_expr(qpath, span, hir_id);
2558
2559 let variant_did = match qpath {
2560 hir::QPath::Resolved(maybe_qself, path) => {
2561 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:2561",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(2561u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["maybe_qself",
"path"], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&maybe_qself)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&path) as
&dyn Value))])
});
} else { ; }
};debug!(?maybe_qself, ?path);
2562 let variant_did = match path.res {
2563 Res::Def(DefKind::Variant | DefKind::Struct, did) => did,
2564 _ => return non_adt_or_variant_res(),
2565 };
2566
2567 variant_did
2568 }
2569 hir::QPath::TypeRelative(hir_self_ty, segment) => {
2570 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:2570",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(2570u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["hir_self_ty",
"segment"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&hir_self_ty)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&segment) as
&dyn Value))])
});
} else { ; }
};debug!(?hir_self_ty, ?segment);
2571
2572 let res_def_id = match opt_res {
2573 Ok(r)
2574 if #[allow(non_exhaustive_omitted_patterns)] match tcx.def_kind(r.def_id()) {
DefKind::Variant | DefKind::Struct => true,
_ => false,
}matches!(
2575 tcx.def_kind(r.def_id()),
2576 DefKind::Variant | DefKind::Struct
2577 ) =>
2578 {
2579 r.def_id()
2580 }
2581 Ok(_) => return non_adt_or_variant_res(),
2582 Err(e) => return ty::Const::new_error(tcx, e),
2583 };
2584
2585 res_def_id
2586 }
2587 };
2588
2589 let ty::Adt(adt_def, adt_args) = ty.kind() else { ::core::panicking::panic("internal error: entered unreachable code")unreachable!() };
2590
2591 let variant_def = adt_def.variant_with_id(variant_did);
2592 let variant_idx = adt_def.variant_index_with_id(variant_did).as_u32();
2593
2594 let fields = variant_def
2595 .fields
2596 .iter()
2597 .map(|field_def| {
2598 let mut init_expr =
2601 inits.iter().filter(|init_expr| init_expr.field.name == field_def.name);
2602
2603 match init_expr.next() {
2604 Some(expr) => {
2605 if let Some(expr) = init_expr.next() {
2606 let e = tcx.dcx().span_err(
2607 expr.span,
2608 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("struct expression with multiple initialisers for `{0}`",
field_def.name))
})format!(
2609 "struct expression with multiple initialisers for `{}`",
2610 field_def.name,
2611 ),
2612 );
2613 return ty::Const::new_error(tcx, e);
2614 }
2615
2616 self.lower_const_arg(
2617 expr.expr,
2618 tcx.type_of(field_def.did).instantiate(tcx, adt_args).skip_norm_wip(),
2619 )
2620 }
2621 None => {
2622 let e = tcx.dcx().span_err(
2623 span,
2624 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("struct expression with missing field initialiser for `{0}`",
field_def.name))
})format!(
2625 "struct expression with missing field initialiser for `{}`",
2626 field_def.name
2627 ),
2628 );
2629 ty::Const::new_error(tcx, e)
2630 }
2631 }
2632 })
2633 .collect::<Vec<_>>();
2634
2635 let opt_discr_const = if adt_def.is_enum() {
2636 let valtree = ty::ValTree::from_scalar_int(tcx, variant_idx.into());
2637 Some(ty::Const::new_value(tcx, valtree, tcx.types.u32))
2638 } else {
2639 None
2640 };
2641
2642 let valtree = ty::ValTree::from_branches(tcx, opt_discr_const.into_iter().chain(fields));
2643 ty::Const::new_value(tcx, valtree, ty)
2644 }
2645
2646 pub fn lower_path_for_struct_expr(
2647 &self,
2648 qpath: hir::QPath<'tcx>,
2649 path_span: Span,
2650 hir_id: HirId,
2651 ) -> ResolvedStructPath<'tcx> {
2652 match qpath {
2653 hir::QPath::Resolved(ref maybe_qself, path) => {
2654 let self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
2655 let ty = self.lower_resolved_ty_path(self_ty, path, hir_id, PermitVariants::Yes);
2656 ResolvedStructPath { res: Ok(path.res), ty }
2657 }
2658 hir::QPath::TypeRelative(hir_self_ty, segment) => {
2659 let self_ty = self.lower_ty(hir_self_ty);
2660
2661 let result = self.lower_type_relative_ty_path(
2662 self_ty,
2663 hir_self_ty,
2664 segment,
2665 hir_id,
2666 path_span,
2667 PermitVariants::Yes,
2668 );
2669 let ty = result
2670 .map(|(ty, _, _)| ty)
2671 .unwrap_or_else(|guar| Ty::new_error(self.tcx(), guar));
2672
2673 ResolvedStructPath {
2674 res: result.map(|(_, kind, def_id)| Res::Def(kind, def_id)),
2675 ty,
2676 }
2677 }
2678 }
2679 }
2680
2681 fn lower_resolved_const_path(
2683 &self,
2684 opt_self_ty: Option<Ty<'tcx>>,
2685 path: &hir::Path<'tcx>,
2686 hir_id: HirId,
2687 ) -> Const<'tcx> {
2688 let tcx = self.tcx();
2689 let span = path.span;
2690 let ct = match path.res {
2691 Res::Def(DefKind::ConstParam, def_id) => {
2692 match (&opt_self_ty, &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(opt_self_ty, None);
2693 let _ = self.prohibit_generic_args(
2694 path.segments.iter(),
2695 GenericsArgsErrExtend::Param(def_id),
2696 );
2697 self.lower_const_param(def_id, hir_id)
2698 }
2699 Res::Def(DefKind::Const { .. }, did) => {
2700 if let Err(guar) = self.require_type_const_attribute(did, span) {
2701 return Const::new_error(self.tcx(), guar);
2702 }
2703
2704 match (&opt_self_ty, &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(opt_self_ty, None);
2705 let [leading_segments @ .., segment] = path.segments else { ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!() };
2706 let _ = self
2707 .prohibit_generic_args(leading_segments.iter(), GenericsArgsErrExtend::None);
2708 let args = self.lower_generic_args_of_path_segment(span, did, segment);
2709 ty::Const::new_unevaluated(
2710 tcx,
2711 ty::UnevaluatedConst::new(
2712 tcx,
2713 ty::UnevaluatedConstKind::new_from_def_id(tcx, did),
2714 args,
2715 ),
2716 )
2717 }
2718 Res::Def(kind @ DefKind::Ctor(ctor_of, CtorKind::Const), did) => {
2719 match (&opt_self_ty, &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(opt_self_ty, None);
2720 let generic_segments =
2721 self.probe_generic_path_segments(path.segments, opt_self_ty, kind, did, span);
2722 let indices: FxHashSet<_> =
2723 generic_segments.iter().map(|GenericPathSegment(_, index)| index).collect();
2724 let _ = self.prohibit_generic_args(
2725 path.segments.iter().enumerate().filter_map(|(index, seg)| {
2726 if !indices.contains(&index) { Some(seg) } else { None }
2727 }),
2728 GenericsArgsErrExtend::DefVariant(&path.segments),
2729 );
2730
2731 let parent_did = tcx.parent(did);
2732 let generics_did = match ctor_of {
2733 CtorOf::Variant => tcx.parent(parent_did),
2734 CtorOf::Struct => parent_did,
2735 };
2736 let args = self.lower_generic_args_of_path_segment(
2737 span,
2738 generics_did,
2739 &path.segments[generic_segments[0].1],
2740 );
2741 self.construct_const_ctor_value(did, ctor_of, args)
2742 }
2743 Res::Def(DefKind::Ctor(ctor_of, CtorKind::Fn), did) => {
2744 match (&opt_self_ty, &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(opt_self_ty, None);
2745 let generic_segments = self.probe_generic_path_segments(
2746 path.segments,
2747 opt_self_ty,
2748 DefKind::Ctor(ctor_of, CtorKind::Const),
2749 did,
2750 span,
2751 );
2752 let indices: FxHashSet<_> =
2753 generic_segments.iter().map(|GenericPathSegment(_, index)| index).collect();
2754 let _ = self.prohibit_generic_args(
2755 path.segments.iter().enumerate().filter_map(|(index, seg)| {
2756 if !indices.contains(&index) { Some(seg) } else { None }
2757 }),
2758 GenericsArgsErrExtend::DefVariant(&path.segments),
2759 );
2760
2761 let parent_did = tcx.parent(did);
2762 let generics_did = if let DefKind::Ctor(CtorOf::Variant, _) = tcx.def_kind(did) {
2763 tcx.parent(parent_did)
2764 } else {
2765 parent_did
2766 };
2767 let args = self.lower_generic_args_of_path_segment(
2768 span,
2769 generics_did,
2770 &path.segments[generic_segments[0].1],
2771 );
2772
2773 ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, did, args))
2774 }
2775 Res::Def(DefKind::AssocConst { .. }, did) => {
2776 let trait_segment = if let [modules @ .., trait_, _item] = path.segments {
2777 let _ = self.prohibit_generic_args(modules.iter(), GenericsArgsErrExtend::None);
2778 Some(trait_)
2779 } else {
2780 None
2781 };
2782 self.lower_resolved_assoc_const_path(
2783 span,
2784 opt_self_ty,
2785 did,
2786 trait_segment,
2787 path.segments.last().unwrap(),
2788 )
2789 .unwrap_or_else(|guar| Const::new_error(tcx, guar))
2790 }
2791 Res::Def(DefKind::Static { .. }, _) => {
2792 ::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("use of bare `static` ConstArgKind::Path\'s not yet supported"))span_bug!(span, "use of bare `static` ConstArgKind::Path's not yet supported")
2793 }
2794 Res::Def(DefKind::Fn | DefKind::AssocFn, did) => {
2796 self.dcx().span_delayed_bug(span, "function items cannot be used as const args");
2797 let args = self.lower_generic_args_of_path_segment(
2798 span,
2799 did,
2800 path.segments.last().unwrap(),
2801 );
2802 ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, did, args))
2803 }
2804
2805 res @ (Res::Def(
2808 DefKind::Mod
2809 | DefKind::Enum
2810 | DefKind::Variant
2811 | DefKind::Struct
2812 | DefKind::OpaqueTy
2813 | DefKind::TyAlias
2814 | DefKind::TraitAlias
2815 | DefKind::AssocTy
2816 | DefKind::Union
2817 | DefKind::Trait
2818 | DefKind::ForeignTy
2819 | DefKind::TyParam
2820 | DefKind::Macro(_)
2821 | DefKind::LifetimeParam
2822 | DefKind::Use
2823 | DefKind::ForeignMod
2824 | DefKind::AnonConst
2825 | DefKind::InlineConst
2826 | DefKind::Field
2827 | DefKind::Impl { .. }
2828 | DefKind::Closure
2829 | DefKind::ExternCrate
2830 | DefKind::GlobalAsm
2831 | DefKind::SyntheticCoroutineBody,
2832 _,
2833 )
2834 | Res::PrimTy(_)
2835 | Res::SelfTyParam { .. }
2836 | Res::SelfTyAlias { .. }
2837 | Res::SelfCtor(_)
2838 | Res::Local(_)
2839 | Res::ToolMod
2840 | Res::OpenMod(..)
2841 | Res::NonMacroAttr(_)
2842 | Res::Err) => Const::new_error_with_message(
2843 tcx,
2844 span,
2845 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid Res {0:?} for const path",
res))
})format!("invalid Res {res:?} for const path"),
2846 ),
2847 };
2848 self.check_param_uses_if_mcg(ct, span, false)
2849 }
2850
2851 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("lower_const_arg_anon",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(2852u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["anon"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&anon)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Const<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
let tcx = self.tcx();
let expr = &tcx.hir_body(anon.body).value;
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:2857",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(2857u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["expr"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&expr) as
&dyn Value))])
});
} else { ; }
};
let ty =
tcx.type_of(anon.def_id).instantiate_identity().skip_norm_wip();
match self.try_lower_anon_const_lit(ty, expr) {
Some(v) => v,
None =>
ty::Const::new_unevaluated(tcx,
ty::UnevaluatedConst::new(tcx,
ty::UnevaluatedConstKind::Anon {
def_id: anon.def_id.to_def_id(),
},
ty::GenericArgs::identity_for_item(tcx,
anon.def_id.to_def_id()))),
}
}
}
}#[instrument(skip(self), level = "debug")]
2853 fn lower_const_arg_anon(&self, anon: &AnonConst) -> Const<'tcx> {
2854 let tcx = self.tcx();
2855
2856 let expr = &tcx.hir_body(anon.body).value;
2857 debug!(?expr);
2858
2859 let ty = tcx.type_of(anon.def_id).instantiate_identity().skip_norm_wip();
2863
2864 match self.try_lower_anon_const_lit(ty, expr) {
2865 Some(v) => v,
2866 None => ty::Const::new_unevaluated(
2867 tcx,
2868 ty::UnevaluatedConst::new(
2869 tcx,
2870 ty::UnevaluatedConstKind::Anon { def_id: anon.def_id.to_def_id() },
2871 ty::GenericArgs::identity_for_item(tcx, anon.def_id.to_def_id()),
2872 ),
2873 ),
2874 }
2875 }
2876
2877 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("lower_const_arg_literal",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(2877u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["kind", "neg", "ty",
"span"], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&kind)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&neg as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&ty)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Const<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
let tcx = self.tcx();
let ty = if !ty.has_infer() { Some(ty) } else { None };
if let LitKind::Err(guar) = *kind {
return ty::Const::new_error(tcx, guar);
}
let input = LitToConstInput { lit: *kind, ty, neg };
match tcx.at(span).lit_to_const(input) {
Some(value) =>
ty::Const::new_value(tcx, value.valtree, value.ty),
None => {
let e =
tcx.dcx().span_err(span,
"type annotations needed for the literal");
ty::Const::new_error(tcx, e)
}
}
}
}
}#[instrument(skip(self), level = "debug")]
2878 fn lower_const_arg_literal(
2879 &self,
2880 kind: &LitKind,
2881 neg: bool,
2882 ty: Ty<'tcx>,
2883 span: Span,
2884 ) -> Const<'tcx> {
2885 let tcx = self.tcx();
2886
2887 let ty = if !ty.has_infer() { Some(ty) } else { None };
2888
2889 if let LitKind::Err(guar) = *kind {
2890 return ty::Const::new_error(tcx, guar);
2891 }
2892 let input = LitToConstInput { lit: *kind, ty, neg };
2893 match tcx.at(span).lit_to_const(input) {
2894 Some(value) => ty::Const::new_value(tcx, value.valtree, value.ty),
2895 None => {
2896 let e = tcx.dcx().span_err(span, "type annotations needed for the literal");
2897 ty::Const::new_error(tcx, e)
2898 }
2899 }
2900 }
2901
2902 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("try_lower_anon_const_lit",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(2902u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["ty", "expr"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&ty)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expr)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Option<Const<'tcx>> = loop {};
return __tracing_attr_fake_return;
}
{
let tcx = self.tcx();
let expr =
match &expr.kind {
hir::ExprKind::Block(block, _) if
block.stmts.is_empty() && block.expr.is_some() => {
block.expr.as_ref().unwrap()
}
_ => expr,
};
let lit_input =
match expr.kind {
hir::ExprKind::Lit(lit) => {
Some(LitToConstInput {
lit: lit.node,
ty: Some(ty),
neg: false,
})
}
hir::ExprKind::Unary(hir::UnOp::Neg, expr) =>
match expr.kind {
hir::ExprKind::Lit(lit) => {
Some(LitToConstInput {
lit: lit.node,
ty: Some(ty),
neg: true,
})
}
_ => None,
},
_ => None,
};
lit_input.and_then(|l|
{
if const_lit_matches_ty(tcx, &l.lit, ty, l.neg) {
tcx.at(expr.span).lit_to_const(l).map(|value|
ty::Const::new_value(tcx, value.valtree, value.ty))
} else { None }
})
}
}
}#[instrument(skip(self), level = "debug")]
2903 fn try_lower_anon_const_lit(
2904 &self,
2905 ty: Ty<'tcx>,
2906 expr: &'tcx hir::Expr<'tcx>,
2907 ) -> Option<Const<'tcx>> {
2908 let tcx = self.tcx();
2909
2910 let expr = match &expr.kind {
2913 hir::ExprKind::Block(block, _) if block.stmts.is_empty() && block.expr.is_some() => {
2914 block.expr.as_ref().unwrap()
2915 }
2916 _ => expr,
2917 };
2918
2919 let lit_input = match expr.kind {
2920 hir::ExprKind::Lit(lit) => {
2921 Some(LitToConstInput { lit: lit.node, ty: Some(ty), neg: false })
2922 }
2923 hir::ExprKind::Unary(hir::UnOp::Neg, expr) => match expr.kind {
2924 hir::ExprKind::Lit(lit) => {
2925 Some(LitToConstInput { lit: lit.node, ty: Some(ty), neg: true })
2926 }
2927 _ => None,
2928 },
2929 _ => None,
2930 };
2931
2932 lit_input.and_then(|l| {
2933 if const_lit_matches_ty(tcx, &l.lit, ty, l.neg) {
2934 tcx.at(expr.span)
2935 .lit_to_const(l)
2936 .map(|value| ty::Const::new_value(tcx, value.valtree, value.ty))
2937 } else {
2938 None
2939 }
2940 })
2941 }
2942
2943 fn require_type_const_attribute(
2944 &self,
2945 def_id: DefId,
2946 span: Span,
2947 ) -> Result<(), ErrorGuaranteed> {
2948 let tcx = self.tcx();
2949 let is_inherent_assoc_const = tcx.def_kind(def_id)
2952 == DefKind::AssocConst { is_type_const: false }
2953 && tcx.def_kind(tcx.parent(def_id)) == DefKind::Impl { of_trait: false };
2954 if tcx.is_type_const(def_id)
2955 || tcx.features().generic_const_args() && !is_inherent_assoc_const
2956 {
2957 Ok(())
2958 } else {
2959 let mut err = self.dcx().struct_span_err(
2960 span,
2961 "use of `const` in the type system not defined as `type const`",
2962 );
2963 if def_id.is_local() {
2964 let name = tcx.def_path_str(def_id);
2965 err.span_suggestion_verbose(
2966 tcx.def_span(def_id).shrink_to_lo(),
2967 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("add `type` before `const` for `{0}`",
name))
})format!("add `type` before `const` for `{name}`"),
2968 ::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("type ")) })format!("type "),
2969 Applicability::MaybeIncorrect,
2970 );
2971 } else {
2972 err.note("only consts marked defined as `type const` may be used in types");
2973 }
2974 Err(err.emit())
2975 }
2976 }
2977
2978 fn lower_delegation_ty(&self, infer: hir::InferDelegation<'tcx>) -> Ty<'tcx> {
2979 match infer {
2980 hir::InferDelegation::DefId(def_id) => {
2981 self.tcx().type_of(def_id).instantiate_identity().skip_norm_wip()
2982 }
2983 rustc_hir::InferDelegation::Sig(_, idx) => {
2984 let delegation_sig = self.tcx().inherit_sig_for_delegation_item(self.item_def_id());
2985
2986 match idx {
2987 hir::InferDelegationSig::Input(idx) => delegation_sig[idx],
2988 hir::InferDelegationSig::Output { .. } => *delegation_sig.last().unwrap(),
2989 }
2990 }
2991 }
2992 }
2993
2994 x;#[instrument(level = "debug", skip(self), ret)]
2996 pub fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
2997 let tcx = self.tcx();
2998
2999 let result_ty = match &hir_ty.kind {
3000 hir::TyKind::InferDelegation(infer) => self.lower_delegation_ty(*infer),
3001 hir::TyKind::Slice(ty) => Ty::new_slice(tcx, self.lower_ty(ty)),
3002 hir::TyKind::Ptr(mt) => Ty::new_ptr(tcx, self.lower_ty(mt.ty), mt.mutbl),
3003 hir::TyKind::Ref(region, mt) => {
3004 let r = self.lower_lifetime(region, RegionInferReason::Reference);
3005 debug!(?r);
3006 let t = self.lower_ty(mt.ty);
3007 Ty::new_ref(tcx, r, t, mt.mutbl)
3008 }
3009 hir::TyKind::Never => tcx.types.never,
3010 hir::TyKind::Tup(fields) => {
3011 Ty::new_tup_from_iter(tcx, fields.iter().map(|t| self.lower_ty(t)))
3012 }
3013 hir::TyKind::FnPtr(bf) => {
3014 check_c_variadic_abi(tcx, bf.decl, bf.abi, hir_ty.span);
3015
3016 Ty::new_fn_ptr(
3017 tcx,
3018 self.lower_fn_ty(hir_ty.hir_id, bf.safety, bf.abi, bf.decl, None, Some(hir_ty)),
3019 )
3020 }
3021 hir::TyKind::UnsafeBinder(binder) => Ty::new_unsafe_binder(
3022 tcx,
3023 ty::Binder::bind_with_vars(
3024 self.lower_ty(binder.inner_ty),
3025 tcx.late_bound_vars(hir_ty.hir_id),
3026 ),
3027 ),
3028 hir::TyKind::TraitObject(bounds, tagged_ptr) => {
3029 let lifetime = tagged_ptr.pointer();
3030 let syntax = tagged_ptr.tag();
3031 self.lower_trait_object_ty(hir_ty.span, hir_ty.hir_id, bounds, lifetime, syntax)
3032 }
3033 hir::TyKind::Path(hir::QPath::Resolved(_, path))
3037 if path.segments.last().and_then(|segment| segment.args).is_some_and(|args| {
3038 matches!(args.parenthesized, hir::GenericArgsParentheses::ReturnTypeNotation)
3039 }) =>
3040 {
3041 let guar = self
3042 .dcx()
3043 .emit_err(BadReturnTypeNotation { span: hir_ty.span, suggestion: None });
3044 Ty::new_error(tcx, guar)
3045 }
3046 hir::TyKind::Path(hir::QPath::Resolved(maybe_qself, path)) => {
3047 debug!(?maybe_qself, ?path);
3048 let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
3049 self.lower_resolved_ty_path(opt_self_ty, path, hir_ty.hir_id, PermitVariants::No)
3050 }
3051 &hir::TyKind::OpaqueDef(opaque_ty) => {
3052 let in_trait = match opaque_ty.origin {
3056 hir::OpaqueTyOrigin::FnReturn {
3057 parent,
3058 in_trait_or_impl: Some(hir::RpitContext::Trait),
3059 ..
3060 }
3061 | hir::OpaqueTyOrigin::AsyncFn {
3062 parent,
3063 in_trait_or_impl: Some(hir::RpitContext::Trait),
3064 ..
3065 } => Some(parent),
3066 hir::OpaqueTyOrigin::FnReturn {
3067 in_trait_or_impl: None | Some(hir::RpitContext::TraitImpl),
3068 ..
3069 }
3070 | hir::OpaqueTyOrigin::AsyncFn {
3071 in_trait_or_impl: None | Some(hir::RpitContext::TraitImpl),
3072 ..
3073 }
3074 | hir::OpaqueTyOrigin::TyAlias { .. } => None,
3075 };
3076
3077 self.lower_opaque_ty(opaque_ty.def_id, in_trait)
3078 }
3079 hir::TyKind::TraitAscription(hir_bounds) => {
3080 let self_ty = self.ty_infer(None, hir_ty.span);
3083 let mut bounds = Vec::new();
3084 self.lower_bounds(
3085 self_ty,
3086 hir_bounds.iter(),
3087 &mut bounds,
3088 ty::List::empty(),
3089 PredicateFilter::All,
3090 OverlappingAsssocItemConstraints::Allowed,
3091 );
3092 self.add_implicit_sizedness_bounds(
3093 &mut bounds,
3094 self_ty,
3095 hir_bounds,
3096 ImpliedBoundsContext::AssociatedTypeOrImplTrait,
3097 hir_ty.span,
3098 );
3099 self.register_trait_ascription_bounds(bounds, hir_ty.hir_id, hir_ty.span);
3100 self_ty
3101 }
3102 hir::TyKind::Path(hir::QPath::TypeRelative(hir_self_ty, segment))
3106 if segment.args.is_some_and(|args| {
3107 matches!(args.parenthesized, hir::GenericArgsParentheses::ReturnTypeNotation)
3108 }) =>
3109 {
3110 let guar = if let hir::Node::LetStmt(stmt) = tcx.parent_hir_node(hir_ty.hir_id)
3111 && let None = stmt.init
3112 && let hir::TyKind::Path(hir::QPath::Resolved(_, self_ty_path)) =
3113 hir_self_ty.kind
3114 && let Res::Def(DefKind::Enum | DefKind::Struct | DefKind::Union, def_id) =
3115 self_ty_path.res
3116 && let Some(_) = tcx
3117 .inherent_impls(def_id)
3118 .iter()
3119 .flat_map(|imp| {
3120 tcx.associated_items(*imp).filter_by_name_unhygienic(segment.ident.name)
3121 })
3122 .filter(|assoc| {
3123 matches!(assoc.kind, ty::AssocKind::Fn { has_self: false, .. })
3124 })
3125 .next()
3126 {
3127 let err = tcx
3129 .dcx()
3130 .struct_span_err(
3131 hir_ty.span,
3132 "expected type, found associated function call",
3133 )
3134 .with_span_suggestion_verbose(
3135 stmt.pat.span.between(hir_ty.span),
3136 "use `=` if you meant to assign",
3137 " = ".to_string(),
3138 Applicability::MaybeIncorrect,
3139 );
3140 self.dcx().try_steal_replace_and_emit_err(
3141 hir_ty.span,
3142 StashKey::ReturnTypeNotation,
3143 err,
3144 )
3145 } else if let hir::Node::LetStmt(stmt) = tcx.parent_hir_node(hir_ty.hir_id)
3146 && let None = stmt.init
3147 && let hir::TyKind::Path(hir::QPath::Resolved(_, self_ty_path)) =
3148 hir_self_ty.kind
3149 && let Res::PrimTy(_) = self_ty_path.res
3150 && self.dcx().has_stashed_diagnostic(hir_ty.span, StashKey::ReturnTypeNotation)
3151 {
3152 let err = tcx
3155 .dcx()
3156 .struct_span_err(
3157 hir_ty.span,
3158 "expected type, found associated function call",
3159 )
3160 .with_span_suggestion_verbose(
3161 stmt.pat.span.between(hir_ty.span),
3162 "use `=` if you meant to assign",
3163 " = ".to_string(),
3164 Applicability::MaybeIncorrect,
3165 );
3166 self.dcx().try_steal_replace_and_emit_err(
3167 hir_ty.span,
3168 StashKey::ReturnTypeNotation,
3169 err,
3170 )
3171 } else {
3172 let suggestion = if self
3173 .dcx()
3174 .has_stashed_diagnostic(hir_ty.span, StashKey::ReturnTypeNotation)
3175 {
3176 Some(segment.ident.span.shrink_to_hi().with_hi(hir_ty.span.hi()))
3182 } else {
3183 None
3184 };
3185 let err = self
3186 .dcx()
3187 .create_err(BadReturnTypeNotation { span: hir_ty.span, suggestion });
3188 self.dcx().try_steal_replace_and_emit_err(
3189 hir_ty.span,
3190 StashKey::ReturnTypeNotation,
3191 err,
3192 )
3193 };
3194 Ty::new_error(tcx, guar)
3195 }
3196 hir::TyKind::Path(hir::QPath::TypeRelative(hir_self_ty, segment)) => {
3197 debug!(?hir_self_ty, ?segment);
3198 let self_ty = self.lower_ty(hir_self_ty);
3199 self.lower_type_relative_ty_path(
3200 self_ty,
3201 hir_self_ty,
3202 segment,
3203 hir_ty.hir_id,
3204 hir_ty.span,
3205 PermitVariants::No,
3206 )
3207 .map(|(ty, _, _)| ty)
3208 .unwrap_or_else(|guar| Ty::new_error(tcx, guar))
3209 }
3210 hir::TyKind::Array(ty, length) => {
3211 let length = self.lower_const_arg(length, tcx.types.usize);
3212 Ty::new_array_with_const_len(tcx, self.lower_ty(ty), length)
3213 }
3214 hir::TyKind::Infer(()) => {
3215 self.ty_infer(None, hir_ty.span)
3220 }
3221 hir::TyKind::Pat(ty, pat) => {
3222 let ty_span = ty.span;
3223 let ty = self.lower_ty(ty);
3224 let pat_ty = match self.lower_pat_ty_pat(ty, ty_span, pat) {
3225 Ok(kind) => Ty::new_pat(tcx, ty, tcx.mk_pat(kind)),
3226 Err(guar) => Ty::new_error(tcx, guar),
3227 };
3228 self.record_ty(pat.hir_id, ty, pat.span);
3229 pat_ty
3230 }
3231 hir::TyKind::FieldOf(ty, hir::TyFieldPath { variant, field }) => self.lower_field_of(
3232 self.lower_ty(ty),
3233 self.item_def_id(),
3234 ty.span,
3235 hir_ty.hir_id,
3236 *variant,
3237 *field,
3238 ),
3239 hir::TyKind::Err(guar) => Ty::new_error(tcx, *guar),
3240 };
3241
3242 self.record_ty(hir_ty.hir_id, result_ty, hir_ty.span);
3243 result_ty
3244 }
3245
3246 fn lower_pat_ty_pat(
3247 &self,
3248 ty: Ty<'tcx>,
3249 ty_span: Span,
3250 pat: &hir::TyPat<'tcx>,
3251 ) -> Result<ty::PatternKind<'tcx>, ErrorGuaranteed> {
3252 let tcx = self.tcx();
3253 match pat.kind {
3254 hir::TyPatKind::Range(start, end) => {
3255 match ty.kind() {
3256 ty::Int(_) | ty::Uint(_) | ty::Char => {
3259 let start = self.lower_const_arg(start, ty);
3260 let end = self.lower_const_arg(end, ty);
3261 Ok(ty::PatternKind::Range { start, end })
3262 }
3263 _ => Err(self
3264 .dcx()
3265 .span_delayed_bug(ty_span, "invalid base type for range pattern")),
3266 }
3267 }
3268 hir::TyPatKind::NotNull => Ok(ty::PatternKind::NotNull),
3269 hir::TyPatKind::Or(patterns) => {
3270 self.tcx()
3271 .mk_patterns_from_iter(patterns.iter().map(|pat| {
3272 self.lower_pat_ty_pat(ty, ty_span, pat).map(|pat| tcx.mk_pat(pat))
3273 }))
3274 .map(ty::PatternKind::Or)
3275 }
3276 hir::TyPatKind::Err(e) => Err(e),
3277 }
3278 }
3279
3280 fn lower_field_of(
3281 &self,
3282 ty: Ty<'tcx>,
3283 item_def_id: LocalDefId,
3284 ty_span: Span,
3285 hir_id: HirId,
3286 variant: Option<Ident>,
3287 field: Ident,
3288 ) -> Ty<'tcx> {
3289 let dcx = self.dcx();
3290 let tcx = self.tcx();
3291 match ty.kind() {
3292 ty::Adt(def, _) => {
3293 let base_did = def.did();
3294 let kind_name = tcx.def_descr(base_did);
3295 let (variant_idx, variant) = if def.is_enum() {
3296 let Some(variant) = variant else {
3297 let err = dcx
3298 .create_err(NoVariantNamed { span: field.span, ident: field, ty })
3299 .with_span_help(
3300 field.span.shrink_to_lo(),
3301 "you might be missing a variant here: `Variant.`",
3302 )
3303 .emit();
3304 return Ty::new_error(tcx, err);
3305 };
3306
3307 if let Some(res) = def
3308 .variants()
3309 .iter_enumerated()
3310 .find(|(_, f)| f.ident(tcx).normalize_to_macros_2_0() == variant)
3311 {
3312 res
3313 } else {
3314 let err = dcx
3315 .create_err(NoVariantNamed { span: variant.span, ident: variant, ty })
3316 .emit();
3317 return Ty::new_error(tcx, err);
3318 }
3319 } else {
3320 if let Some(variant) = variant {
3321 let adt_path = tcx.def_path_str(base_did);
3322 {
dcx.struct_span_err(variant.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} `{1}` does not have any variants",
kind_name, adt_path))
})).with_code(E0609)
}struct_span_code_err!(
3323 dcx,
3324 variant.span,
3325 E0609,
3326 "{kind_name} `{adt_path}` does not have any variants",
3327 )
3328 .with_span_label(variant.span, "variant unknown")
3329 .emit();
3330 }
3331 (FIRST_VARIANT, def.non_enum_variant())
3332 };
3333 let block = tcx.local_def_id_to_hir_id(item_def_id);
3334 let (ident, def_scope) = tcx.adjust_ident_and_get_scope(field, def.did(), block);
3335 if let Some((field_idx, field)) = variant
3336 .fields
3337 .iter_enumerated()
3338 .find(|(_, f)| f.ident(tcx).normalize_to_macros_2_0() == ident)
3339 {
3340 if field.vis.is_accessible_from(def_scope, tcx) {
3341 tcx.check_stability(field.did, Some(hir_id), ident.span, None);
3342 } else {
3343 let adt_path = tcx.def_path_str(base_did);
3344 {
dcx.struct_span_err(ident.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("field `{0}` of {1} `{2}` is private",
ident, kind_name, adt_path))
})).with_code(E0616)
}struct_span_code_err!(
3345 dcx,
3346 ident.span,
3347 E0616,
3348 "field `{ident}` of {kind_name} `{adt_path}` is private",
3349 )
3350 .with_span_label(ident.span, "private field")
3351 .emit();
3352 }
3353 Ty::new_field_representing_type(tcx, ty, variant_idx, field_idx)
3354 } else {
3355 let err =
3356 dcx.create_err(NoFieldOnType { span: ident.span, field: ident, ty }).emit();
3357 Ty::new_error(tcx, err)
3358 }
3359 }
3360 ty::Tuple(tys) => {
3361 let index = match field.as_str().parse::<usize>() {
3362 Ok(idx) => idx,
3363 Err(_) => {
3364 let err =
3365 dcx.create_err(NoFieldOnType { span: field.span, field, ty }).emit();
3366 return Ty::new_error(tcx, err);
3367 }
3368 };
3369 if field.name != sym::integer(index) {
3370 ::rustc_middle::util::bug::bug_fmt(format_args!("we parsed above, but now not equal?"));bug!("we parsed above, but now not equal?");
3371 }
3372 if tys.get(index).is_some() {
3373 Ty::new_field_representing_type(tcx, ty, FIRST_VARIANT, index.into())
3374 } else {
3375 let err = dcx.create_err(NoFieldOnType { span: field.span, field, ty }).emit();
3376 Ty::new_error(tcx, err)
3377 }
3378 }
3379 ty::Alias(..) => Ty::new_error(
3392 tcx,
3393 dcx.span_err(ty_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("could not resolve fields of `{0}`",
ty))
})format!("could not resolve fields of `{ty}`")),
3394 ),
3395 ty::Error(err) => Ty::new_error(tcx, *err),
3396 ty::Bool
3397 | ty::Char
3398 | ty::Int(_)
3399 | ty::Uint(_)
3400 | ty::Float(_)
3401 | ty::Foreign(_)
3402 | ty::Str
3403 | ty::RawPtr(_, _)
3404 | ty::Ref(_, _, _)
3405 | ty::FnDef(_, _)
3406 | ty::FnPtr(_, _)
3407 | ty::UnsafeBinder(_)
3408 | ty::Dynamic(_, _)
3409 | ty::Closure(_, _)
3410 | ty::CoroutineClosure(_, _)
3411 | ty::Coroutine(_, _)
3412 | ty::CoroutineWitness(_, _)
3413 | ty::Never
3414 | ty::Param(_)
3415 | ty::Bound(_, _)
3416 | ty::Placeholder(_)
3417 | ty::Slice(..) => Ty::new_error(
3418 tcx,
3419 dcx.span_err(ty_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("type `{0}` doesn\'t have fields",
ty))
})format!("type `{ty}` doesn't have fields")),
3420 ),
3421 ty::Infer(_) => Ty::new_error(
3422 tcx,
3423 dcx.span_err(ty_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot use `{0}` in this position",
ty))
})format!("cannot use `{ty}` in this position")),
3424 ),
3425 ty::Array(..) | ty::Pat(..) => Ty::new_error(
3427 tcx,
3428 dcx.span_err(ty_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("type `{0}` is not yet supported in `field_of!`",
ty))
})format!("type `{ty}` is not yet supported in `field_of!`")),
3429 ),
3430 }
3431 }
3432
3433 x;#[instrument(level = "debug", skip(self), ret)]
3435 fn lower_opaque_ty(&self, def_id: LocalDefId, in_trait: Option<LocalDefId>) -> Ty<'tcx> {
3436 let tcx = self.tcx();
3437
3438 let lifetimes = tcx.opaque_captured_lifetimes(def_id);
3439 debug!(?lifetimes);
3440
3441 let def_id = if let Some(parent_def_id) = in_trait {
3445 *tcx.associated_types_for_impl_traits_in_associated_fn(parent_def_id.to_def_id())
3446 .iter()
3447 .find(|rpitit| match tcx.opt_rpitit_info(**rpitit) {
3448 Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
3449 opaque_def_id.expect_local() == def_id
3450 }
3451 _ => unreachable!(),
3452 })
3453 .unwrap()
3454 } else {
3455 def_id.to_def_id()
3456 };
3457
3458 let generics = tcx.generics_of(def_id);
3459 debug!(?generics);
3460
3461 let offset = generics.count() - lifetimes.len();
3465
3466 let args = ty::GenericArgs::for_item(tcx, def_id, |param, _| {
3467 if let Some(i) = (param.index as usize).checked_sub(offset) {
3468 let (lifetime, _) = lifetimes[i];
3469 self.lower_resolved_lifetime(lifetime).into()
3471 } else {
3472 tcx.mk_param_from_def(param)
3473 }
3474 });
3475 debug!(?args);
3476
3477 if in_trait.is_some() {
3478 Ty::new_projection_from_args(tcx, def_id, args)
3479 } else {
3480 Ty::new_opaque(tcx, def_id, args)
3481 }
3482 }
3483
3484 x;#[instrument(level = "debug", skip(self, hir_id, safety, abi, decl, generics, hir_ty), ret)]
3486 pub fn lower_fn_ty(
3487 &self,
3488 hir_id: HirId,
3489 safety: hir::Safety,
3490 abi: rustc_abi::ExternAbi,
3491 decl: &hir::FnDecl<'tcx>,
3492 generics: Option<&hir::Generics<'_>>,
3493 hir_ty: Option<&hir::Ty<'_>>,
3494 ) -> ty::PolyFnSig<'tcx> {
3495 let tcx = self.tcx();
3496 let bound_vars = tcx.late_bound_vars(hir_id);
3497 debug!(?bound_vars);
3498
3499 let (input_tys, output_ty) = self.lower_fn_sig(decl, generics, hir_id, hir_ty);
3500
3501 debug!(?output_ty);
3502
3503 debug!(?abi, ?safety, ?decl.fn_decl_kind, input_tys_len = ?input_tys.len());
3504 let fn_sig_kind = FnSigKind::default()
3506 .set_abi(abi)
3507 .set_safety(safety)
3508 .set_c_variadic(decl.fn_decl_kind.c_variadic());
3509 let fn_ty = tcx.mk_fn_sig(input_tys, output_ty, fn_sig_kind);
3510 let fn_ptr_ty = ty::Binder::bind_with_vars(fn_ty, bound_vars);
3511
3512 if let hir::Node::Ty(hir::Ty { kind: hir::TyKind::FnPtr(fn_ptr_ty), span, .. }) =
3513 tcx.hir_node(hir_id)
3514 {
3515 check_abi(tcx, hir_id, *span, fn_ptr_ty.abi);
3516 }
3517
3518 cmse::validate_cmse_abi(self.tcx(), self.dcx(), hir_id, abi, fn_ptr_ty);
3520
3521 if !fn_ptr_ty.references_error() {
3522 let inputs = fn_ptr_ty.inputs();
3529 let late_bound_in_args =
3530 tcx.collect_constrained_late_bound_regions(inputs.map_bound(|i| i.to_owned()));
3531 let output = fn_ptr_ty.output();
3532 let late_bound_in_ret = tcx.collect_referenced_late_bound_regions(output);
3533
3534 self.validate_late_bound_regions(late_bound_in_args, late_bound_in_ret, |br_name| {
3535 struct_span_code_err!(
3536 self.dcx(),
3537 decl.output.span(),
3538 E0581,
3539 "return type references {}, which is not constrained by the fn input types",
3540 br_name
3541 )
3542 });
3543 }
3544
3545 fn_ptr_ty
3546 }
3547
3548 pub(super) fn suggest_trait_fn_ty_for_impl_fn_infer(
3553 &self,
3554 fn_hir_id: HirId,
3555 arg_idx: Option<usize>,
3556 ) -> Option<Ty<'tcx>> {
3557 let tcx = self.tcx();
3558 let hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), ident, .. }) =
3559 tcx.hir_node(fn_hir_id)
3560 else {
3561 return None;
3562 };
3563 let i = tcx.parent_hir_node(fn_hir_id).expect_item().expect_impl();
3564
3565 let trait_ref = self.lower_impl_trait_ref(&i.of_trait?.trait_ref, self.lower_ty(i.self_ty));
3566
3567 let assoc = tcx.associated_items(trait_ref.def_id).find_by_ident_and_kind(
3568 tcx,
3569 *ident,
3570 ty::AssocTag::Fn,
3571 trait_ref.def_id,
3572 )?;
3573
3574 let fn_sig = tcx
3575 .fn_sig(assoc.def_id)
3576 .instantiate(
3577 tcx,
3578 trait_ref
3579 .args
3580 .extend_to(tcx, assoc.def_id, |param, _| tcx.mk_param_from_def(param)),
3581 )
3582 .skip_norm_wip();
3583 let fn_sig = tcx.liberate_late_bound_regions(fn_hir_id.expect_owner().to_def_id(), fn_sig);
3584
3585 Some(if let Some(arg_idx) = arg_idx {
3586 *fn_sig.inputs().get(arg_idx)?
3587 } else {
3588 fn_sig.output()
3589 })
3590 }
3591
3592 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("validate_late_bound_regions",
"rustc_hir_analysis::hir_ty_lowering",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
::tracing_core::__macro_support::Option::Some(3592u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
::tracing_core::field::FieldSet::new(&["constrained_regions",
"referenced_regions"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&constrained_regions)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&referenced_regions)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
for br in referenced_regions.difference(&constrained_regions) {
let br_name =
if let Some(name) = br.get_name(self.tcx()) {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("lifetime `{0}`", name))
})
} else { "an anonymous lifetime".to_string() };
let mut err = generate_err(&br_name);
if !br.is_named(self.tcx()) {
err.note("lifetimes appearing in an associated or opaque type are not considered constrained");
err.note("consider introducing a named lifetime parameter");
}
err.emit();
}
}
}
}#[instrument(level = "trace", skip(self, generate_err))]
3593 fn validate_late_bound_regions<'cx>(
3594 &'cx self,
3595 constrained_regions: FxIndexSet<ty::BoundRegionKind<'tcx>>,
3596 referenced_regions: FxIndexSet<ty::BoundRegionKind<'tcx>>,
3597 generate_err: impl Fn(&str) -> Diag<'cx>,
3598 ) {
3599 for br in referenced_regions.difference(&constrained_regions) {
3600 let br_name = if let Some(name) = br.get_name(self.tcx()) {
3601 format!("lifetime `{name}`")
3602 } else {
3603 "an anonymous lifetime".to_string()
3604 };
3605
3606 let mut err = generate_err(&br_name);
3607
3608 if !br.is_named(self.tcx()) {
3609 err.note(
3616 "lifetimes appearing in an associated or opaque type are not considered constrained",
3617 );
3618 err.note("consider introducing a named lifetime parameter");
3619 }
3620
3621 err.emit();
3622 }
3623 }
3624
3625 fn construct_const_ctor_value(
3626 &self,
3627 ctor_def_id: DefId,
3628 ctor_of: CtorOf,
3629 args: GenericArgsRef<'tcx>,
3630 ) -> Const<'tcx> {
3631 let tcx = self.tcx();
3632 let parent_did = tcx.parent(ctor_def_id);
3633
3634 let adt_def = tcx.adt_def(match ctor_of {
3635 CtorOf::Variant => tcx.parent(parent_did),
3636 CtorOf::Struct => parent_did,
3637 });
3638
3639 let variant_idx = adt_def.variant_index_with_id(parent_did);
3640
3641 let valtree = if adt_def.is_enum() {
3642 let discr = ty::ValTree::from_scalar_int(tcx, variant_idx.as_u32().into());
3643 ty::ValTree::from_branches(tcx, [ty::Const::new_value(tcx, discr, tcx.types.u32)])
3644 } else {
3645 ty::ValTree::zst(tcx)
3646 };
3647
3648 let adt_ty = Ty::new_adt(tcx, adt_def, args);
3649 ty::Const::new_value(tcx, valtree, adt_ty)
3650 }
3651}