1 use crate::check
::{FnCtxt, Inherited}
;
2 use crate::constrained_generic_params
::{identify_constrained_generic_params, Parameter}
;
5 use rustc_data_structures
::fx
::{FxHashMap, FxHashSet}
;
6 use rustc_errors
::{struct_span_err, Applicability, DiagnosticBuilder}
;
8 use rustc_hir
::def_id
::{DefId, LocalDefId}
;
9 use rustc_hir
::intravisit
as hir_visit
;
10 use rustc_hir
::intravisit
::Visitor
;
11 use rustc_hir
::itemlikevisit
::ParItemLikeVisitor
;
12 use rustc_hir
::lang_items
::LangItem
;
13 use rustc_hir
::ItemKind
;
14 use rustc_middle
::hir
::map
as hir_map
;
15 use rustc_middle
::ty
::subst
::{GenericArgKind, InternalSubsts, Subst}
;
16 use rustc_middle
::ty
::trait_def
::TraitSpecializationKind
;
17 use rustc_middle
::ty
::{
18 self, AdtKind
, GenericParamDefKind
, ToPredicate
, Ty
, TyCtxt
, TypeFoldable
, WithConstness
,
20 use rustc_session
::parse
::feature_err
;
21 use rustc_span
::symbol
::{sym, Ident, Symbol}
;
23 use rustc_trait_selection
::opaque_types
::may_define_opaque_type
;
24 use rustc_trait_selection
::traits
::query
::evaluate_obligation
::InferCtxtExt
;
25 use rustc_trait_selection
::traits
::{self, ObligationCause, ObligationCauseCode}
;
27 use std
::ops
::ControlFlow
;
29 /// Helper type of a temporary returned by `.for_item(...)`.
30 /// This is necessary because we can't write the following bound:
33 /// F: for<'b, 'tcx> where 'tcx FnOnce(FnCtxt<'b, 'tcx>)
35 struct CheckWfFcxBuilder
<'tcx
> {
36 inherited
: super::InheritedBuilder
<'tcx
>,
39 param_env
: ty
::ParamEnv
<'tcx
>,
42 impl<'tcx
> CheckWfFcxBuilder
<'tcx
> {
43 fn with_fcx
<F
>(&mut self, f
: F
)
45 F
: for<'b
> FnOnce(&FnCtxt
<'b
, 'tcx
>, TyCtxt
<'tcx
>) -> Vec
<Ty
<'tcx
>>,
49 let param_env
= self.param_env
;
50 self.inherited
.enter(|inh
| {
51 let fcx
= FnCtxt
::new(&inh
, param_env
, id
);
52 if !inh
.tcx
.features().trivial_bounds
{
53 // As predicates are cached rather than obligations, this
54 // needs to be called first so that they are checked with an
56 check_false_global_bounds(&fcx
, span
, id
);
58 let wf_tys
= f(&fcx
, fcx
.tcx
);
59 fcx
.select_all_obligations_or_error();
60 fcx
.regionck_item(id
, span
, &wf_tys
);
65 /// Checks that the field types (in a struct def'n) or argument types (in an enum def'n) are
66 /// well-formed, meaning that they do not require any constraints not declared in the struct
67 /// definition itself. For example, this definition would be illegal:
70 /// struct Ref<'a, T> { x: &'a T }
73 /// because the type did not declare that `T:'a`.
75 /// We do this check as a pre-pass before checking fn bodies because if these constraints are
76 /// not included it frequently leads to confusing errors in fn bodies. So it's better to check
78 pub fn check_item_well_formed(tcx
: TyCtxt
<'_
>, def_id
: LocalDefId
) {
79 let hir_id
= tcx
.hir().local_def_id_to_hir_id(def_id
);
80 let item
= tcx
.hir().expect_item(hir_id
);
83 "check_item_well_formed(it.def_id={:?}, it.name={})",
85 tcx
.def_path_str(def_id
.to_def_id())
89 // Right now we check that every default trait implementation
90 // has an implementation of itself. Basically, a case like:
92 // impl Trait for T {}
94 // has a requirement of `T: Trait` which was required for default
95 // method implementations. Although this could be improved now that
96 // there's a better infrastructure in place for this, it's being left
97 // for a follow-up work.
99 // Since there's such a requirement, we need to check *just* positive
100 // implementations, otherwise things like:
102 // impl !Send for T {}
104 // won't be allowed unless there's an *explicit* implementation of `Send`
106 hir
::ItemKind
::Impl(ref impl_
) => {
108 .impl_trait_ref(item
.def_id
)
109 .map_or(false, |trait_ref
| tcx
.trait_is_auto(trait_ref
.def_id
));
110 if let (hir
::Defaultness
::Default { .. }
, true) = (impl_
.defaultness
, is_auto
) {
111 let sp
= impl_
.of_trait
.as_ref().map_or(item
.span
, |t
| t
.path
.span
);
113 tcx
.sess
.struct_span_err(sp
, "impls of auto traits cannot be default");
114 err
.span_labels(impl_
.defaultness_span
, "default because of this");
115 err
.span_label(sp
, "auto trait");
118 // We match on both `ty::ImplPolarity` and `ast::ImplPolarity` just to get the `!` span.
119 match (tcx
.impl_polarity(def_id
), impl_
.polarity
) {
120 (ty
::ImplPolarity
::Positive
, _
) => {
121 check_impl(tcx
, item
, impl_
.self_ty
, &impl_
.of_trait
);
123 (ty
::ImplPolarity
::Negative
, ast
::ImplPolarity
::Negative(span
)) => {
124 // FIXME(#27579): what amount of WF checking do we need for neg impls?
125 if let hir
::Defaultness
::Default { .. }
= impl_
.defaultness
{
126 let mut spans
= vec
![span
];
127 spans
.extend(impl_
.defaultness_span
);
132 "negative impls cannot be default impls"
137 (ty
::ImplPolarity
::Reservation
, _
) => {
138 // FIXME: what amount of WF checking do we need for reservation impls?
143 hir
::ItemKind
::Fn(ref sig
, ..) => {
144 check_item_fn(tcx
, item
.hir_id(), item
.ident
, item
.span
, sig
.decl
);
146 hir
::ItemKind
::Static(ref ty
, ..) => {
147 check_item_type(tcx
, item
.hir_id(), ty
.span
, false);
149 hir
::ItemKind
::Const(ref ty
, ..) => {
150 check_item_type(tcx
, item
.hir_id(), ty
.span
, false);
152 hir
::ItemKind
::ForeignMod { items, .. }
=> {
153 for it
in items
.iter() {
154 let it
= tcx
.hir().foreign_item(it
.id
);
156 hir
::ForeignItemKind
::Fn(ref decl
, ..) => {
157 check_item_fn(tcx
, it
.hir_id(), it
.ident
, it
.span
, decl
)
159 hir
::ForeignItemKind
::Static(ref ty
, ..) => {
160 check_item_type(tcx
, it
.hir_id(), ty
.span
, true)
162 hir
::ForeignItemKind
::Type
=> (),
166 hir
::ItemKind
::Struct(ref struct_def
, ref ast_generics
) => {
167 check_type_defn(tcx
, item
, false, |fcx
| vec
![fcx
.non_enum_variant(struct_def
)]);
169 check_variances_for_type_defn(tcx
, item
, ast_generics
);
171 hir
::ItemKind
::Union(ref struct_def
, ref ast_generics
) => {
172 check_type_defn(tcx
, item
, true, |fcx
| vec
![fcx
.non_enum_variant(struct_def
)]);
174 check_variances_for_type_defn(tcx
, item
, ast_generics
);
176 hir
::ItemKind
::Enum(ref enum_def
, ref ast_generics
) => {
177 check_type_defn(tcx
, item
, true, |fcx
| fcx
.enum_variants(enum_def
));
179 check_variances_for_type_defn(tcx
, item
, ast_generics
);
181 hir
::ItemKind
::Trait(..) => {
182 check_trait(tcx
, item
);
184 hir
::ItemKind
::TraitAlias(..) => {
185 check_trait(tcx
, item
);
191 pub fn check_trait_item(tcx
: TyCtxt
<'_
>, def_id
: LocalDefId
) {
192 let hir_id
= tcx
.hir().local_def_id_to_hir_id(def_id
);
193 let trait_item
= tcx
.hir().expect_trait_item(hir_id
);
195 let method_sig
= match trait_item
.kind
{
196 hir
::TraitItemKind
::Fn(ref sig
, _
) => Some(sig
),
199 check_object_unsafe_self_trait_by_name(tcx
, &trait_item
);
200 check_associated_item(tcx
, trait_item
.hir_id(), trait_item
.span
, method_sig
);
203 fn could_be_self(trait_def_id
: LocalDefId
, ty
: &hir
::Ty
<'_
>) -> bool
{
205 hir
::TyKind
::TraitObject([trait_ref
], ..) => match trait_ref
.trait_ref
.path
.segments
{
206 [s
] => s
.res
.and_then(|r
| r
.opt_def_id()) == Some(trait_def_id
.to_def_id()),
213 /// Detect when an object unsafe trait is referring to itself in one of its associated items.
214 /// When this is done, suggest using `Self` instead.
215 fn check_object_unsafe_self_trait_by_name(tcx
: TyCtxt
<'_
>, item
: &hir
::TraitItem
<'_
>) {
216 let (trait_name
, trait_def_id
) = match tcx
.hir().get(tcx
.hir().get_parent_item(item
.hir_id())) {
217 hir
::Node
::Item(item
) => match item
.kind
{
218 hir
::ItemKind
::Trait(..) => (item
.ident
, item
.def_id
),
223 let mut trait_should_be_self
= vec
![];
225 hir
::TraitItemKind
::Const(ty
, _
) | hir
::TraitItemKind
::Type(_
, Some(ty
))
226 if could_be_self(trait_def_id
, ty
) =>
228 trait_should_be_self
.push(ty
.span
)
230 hir
::TraitItemKind
::Fn(sig
, _
) => {
231 for ty
in sig
.decl
.inputs
{
232 if could_be_self(trait_def_id
, ty
) {
233 trait_should_be_self
.push(ty
.span
);
236 match sig
.decl
.output
{
237 hir
::FnRetTy
::Return(ty
) if could_be_self(trait_def_id
, ty
) => {
238 trait_should_be_self
.push(ty
.span
);
245 if !trait_should_be_self
.is_empty() {
246 if tcx
.object_safety_violations(trait_def_id
).is_empty() {
249 let sugg
= trait_should_be_self
.iter().map(|span
| (*span
, "Self".to_string())).collect();
252 trait_should_be_self
,
253 "associated item referring to unboxed trait object for its own trait",
255 .span_label(trait_name
.span
, "in this trait")
256 .multipart_suggestion(
257 "you might have meant to use `Self` to refer to the implementing type",
259 Applicability
::MachineApplicable
,
265 pub fn check_impl_item(tcx
: TyCtxt
<'_
>, def_id
: LocalDefId
) {
266 let hir_id
= tcx
.hir().local_def_id_to_hir_id(def_id
);
267 let impl_item
= tcx
.hir().expect_impl_item(hir_id
);
269 let method_sig
= match impl_item
.kind
{
270 hir
::ImplItemKind
::Fn(ref sig
, _
) => Some(sig
),
274 check_associated_item(tcx
, impl_item
.hir_id(), impl_item
.span
, method_sig
);
277 fn check_param_wf(tcx
: TyCtxt
<'_
>, param
: &hir
::GenericParam
<'_
>) {
279 // We currently only check wf of const params here.
280 hir
::GenericParamKind
::Lifetime { .. }
| hir
::GenericParamKind
::Type { .. }
=> (),
282 // Const parameters are well formed if their type is structural match.
283 // FIXME(const_generics_defaults): we also need to check that the `default` is wf.
284 hir
::GenericParamKind
::Const { ty: hir_ty, default: _ }
=> {
285 let ty
= tcx
.type_of(tcx
.hir().local_def_id(param
.hir_id
));
288 let mut is_ptr
= true;
289 let err
= if tcx
.features().const_generics
{
290 match ty
.peel_refs().kind() {
291 ty
::FnPtr(_
) => Some("function pointers"),
292 ty
::RawPtr(_
) => Some("raw pointers"),
297 ty
::Bool
| ty
::Char
| ty
::Int(_
) | ty
::Uint(_
) | ty
::Error(_
) => None
,
298 ty
::FnPtr(_
) => Some("function pointers"),
299 ty
::RawPtr(_
) => Some("raw pointers"),
302 err_ty_str
= format
!("`{}`", ty
);
303 Some(err_ty_str
.as_str())
307 if let Some(unsupported_type
) = err
{
312 "using {} as const generic parameters is forbidden",
321 "{} is forbidden as the type of a const generic parameter",
325 .note("the only supported types are integers, `bool` and `char`")
326 .help("more complex types are supported with `#![feature(const_generics)]`")
331 if traits
::search_for_structural_match_violation(param
.hir_id
, param
.span
, tcx
, ty
)
334 // We use the same error code in both branches, because this is really the same
335 // issue: we just special-case the message for type parameters to make it
337 if let ty
::Param(_
) = ty
.peel_refs().kind() {
338 // Const parameters may not have type parameters as their types,
339 // because we cannot be sure that the type parameter derives `PartialEq`
340 // and `Eq` (just implementing them is not enough for `structural_match`).
345 "`{}` is not guaranteed to `#[derive(PartialEq, Eq)]`, so may not be \
346 used as the type of a const parameter",
351 format
!("`{}` may not derive both `PartialEq` and `Eq`", ty
),
354 "it is not currently possible to use a type parameter as the type of a \
363 "`{}` must be annotated with `#[derive(PartialEq, Eq)]` to be used as \
364 the type of a const parameter",
369 format
!("`{}` doesn't derive both `PartialEq` and `Eq`", ty
),
378 fn check_associated_item(
382 sig_if_method
: Option
<&hir
::FnSig
<'_
>>,
384 debug
!("check_associated_item: {:?}", item_id
);
386 let code
= ObligationCauseCode
::MiscObligation
;
387 for_id(tcx
, item_id
, span
).with_fcx(|fcx
, tcx
| {
388 let item
= fcx
.tcx
.associated_item(fcx
.tcx
.hir().local_def_id(item_id
));
390 let (mut implied_bounds
, self_ty
) = match item
.container
{
391 ty
::TraitContainer(_
) => (vec
![], fcx
.tcx
.types
.self_param
),
392 ty
::ImplContainer(def_id
) => {
393 (fcx
.impl_implied_bounds(def_id
, span
), fcx
.tcx
.type_of(def_id
))
398 ty
::AssocKind
::Const
=> {
399 let ty
= fcx
.tcx
.type_of(item
.def_id
);
400 let ty
= fcx
.normalize_associated_types_in(span
, ty
);
401 fcx
.register_wf_obligation(ty
.into(), span
, code
.clone());
403 ty
::AssocKind
::Fn
=> {
404 let sig
= fcx
.tcx
.fn_sig(item
.def_id
);
405 let sig
= fcx
.normalize_associated_types_in(span
, sig
);
406 let hir_sig
= sig_if_method
.expect("bad signature for method");
416 check_method_receiver(fcx
, hir_sig
, &item
, self_ty
);
418 ty
::AssocKind
::Type
=> {
419 if let ty
::AssocItemContainer
::TraitContainer(_
) = item
.container
{
420 check_associated_type_bounds(fcx
, item
, span
)
422 if item
.defaultness
.has_value() {
423 let ty
= fcx
.tcx
.type_of(item
.def_id
);
424 let ty
= fcx
.normalize_associated_types_in(span
, ty
);
425 fcx
.register_wf_obligation(ty
.into(), span
, code
.clone());
434 fn for_item
<'tcx
>(tcx
: TyCtxt
<'tcx
>, item
: &hir
::Item
<'_
>) -> CheckWfFcxBuilder
<'tcx
> {
435 for_id(tcx
, item
.hir_id(), item
.span
)
438 fn for_id(tcx
: TyCtxt
<'_
>, id
: hir
::HirId
, span
: Span
) -> CheckWfFcxBuilder
<'_
> {
439 let def_id
= tcx
.hir().local_def_id(id
);
441 inherited
: Inherited
::build(tcx
, def_id
),
444 param_env
: tcx
.param_env(def_id
),
448 fn item_adt_kind(kind
: &ItemKind
<'_
>) -> Option
<AdtKind
> {
450 ItemKind
::Struct(..) => Some(AdtKind
::Struct
),
451 ItemKind
::Union(..) => Some(AdtKind
::Union
),
452 ItemKind
::Enum(..) => Some(AdtKind
::Enum
),
457 /// In a type definition, we check that to ensure that the types of the fields are well-formed.
458 fn check_type_defn
<'tcx
, F
>(
460 item
: &hir
::Item
<'tcx
>,
462 mut lookup_fields
: F
,
464 F
: for<'fcx
> FnMut(&FnCtxt
<'fcx
, 'tcx
>) -> Vec
<AdtVariant
<'tcx
>>,
466 for_item(tcx
, item
).with_fcx(|fcx
, fcx_tcx
| {
467 let variants
= lookup_fields(fcx
);
468 let packed
= fcx
.tcx
.adt_def(item
.def_id
).repr
.packed();
470 for variant
in &variants
{
471 // For DST, or when drop needs to copy things around, all
472 // intermediate types must be sized.
473 let needs_drop_copy
= || {
475 let ty
= variant
.fields
.last().unwrap().ty
;
476 let ty
= fcx
.tcx
.erase_regions(ty
);
477 if ty
.needs_infer() {
480 .delay_span_bug(item
.span
, &format
!("inference variables in {:?}", ty
));
481 // Just treat unresolved type expression as if it needs drop.
484 ty
.needs_drop(fcx_tcx
, fcx_tcx
.param_env(item
.def_id
))
488 let all_sized
= all_sized
|| variant
.fields
.is_empty() || needs_drop_copy();
489 let unsized_len
= if all_sized { 0 }
else { 1 }
;
491 variant
.fields
[..variant
.fields
.len() - unsized_len
].iter().enumerate()
493 let last
= idx
== variant
.fields
.len() - 1;
496 fcx
.tcx
.require_lang_item(LangItem
::Sized
, None
),
497 traits
::ObligationCause
::new(
501 adt_kind
: match item_adt_kind(&item
.kind
) {
512 // All field types must be well-formed.
513 for field
in &variant
.fields
{
514 fcx
.register_wf_obligation(
517 ObligationCauseCode
::MiscObligation
,
521 // Explicit `enum` discriminant values must const-evaluate successfully.
522 if let Some(discr_def_id
) = variant
.explicit_discr
{
524 InternalSubsts
::identity_for_item(fcx
.tcx
, discr_def_id
.to_def_id());
526 let cause
= traits
::ObligationCause
::new(
527 fcx
.tcx
.def_span(discr_def_id
),
529 traits
::MiscObligation
,
531 fcx
.register_predicate(traits
::Obligation
::new(
534 ty
::PredicateKind
::ConstEvaluatable(
535 ty
::WithOptConstParam
::unknown(discr_def_id
.to_def_id()),
538 .to_predicate(fcx
.tcx
),
543 check_where_clauses(tcx
, fcx
, item
.span
, item
.def_id
.to_def_id(), None
);
545 // No implied bounds in a struct definition.
550 fn check_trait(tcx
: TyCtxt
<'_
>, item
: &hir
::Item
<'_
>) {
551 debug
!("check_trait: {:?}", item
.def_id
);
553 let trait_def
= tcx
.trait_def(item
.def_id
);
554 if trait_def
.is_marker
555 || matches
!(trait_def
.specialization_kind
, TraitSpecializationKind
::Marker
)
557 for associated_def_id
in &*tcx
.associated_item_def_ids(item
.def_id
) {
560 tcx
.def_span(*associated_def_id
),
562 "marker traits cannot have associated items",
568 for_item(tcx
, item
).with_fcx(|fcx
, _
| {
569 check_where_clauses(tcx
, fcx
, item
.span
, item
.def_id
.to_def_id(), None
);
575 /// Checks all associated type defaults of trait `trait_def_id`.
577 /// Assuming the defaults are used, check that all predicates (bounds on the
578 /// assoc type and where clauses on the trait) hold.
579 fn check_associated_type_bounds(fcx
: &FnCtxt
<'_
, '_
>, item
: &ty
::AssocItem
, span
: Span
) {
582 let bounds
= tcx
.explicit_item_bounds(item
.def_id
);
584 debug
!("check_associated_type_bounds: bounds={:?}", bounds
);
585 let wf_obligations
= bounds
.iter().flat_map(|&(bound
, bound_span
)| {
586 let normalized_bound
= fcx
.normalize_associated_types_in(span
, bound
);
587 traits
::wf
::predicate_obligations(
596 for obligation
in wf_obligations
{
597 debug
!("next obligation cause: {:?}", obligation
.cause
);
598 fcx
.register_predicate(obligation
);
607 decl
: &hir
::FnDecl
<'_
>,
609 for_id(tcx
, item_id
, span
).with_fcx(|fcx
, tcx
| {
610 let def_id
= fcx
.tcx
.hir().local_def_id(item_id
);
611 let sig
= fcx
.tcx
.fn_sig(def_id
);
612 let sig
= fcx
.normalize_associated_types_in(span
, sig
);
613 let mut implied_bounds
= vec
![];
627 fn check_item_type(tcx
: TyCtxt
<'_
>, item_id
: hir
::HirId
, ty_span
: Span
, allow_foreign_ty
: bool
) {
628 debug
!("check_item_type: {:?}", item_id
);
630 for_id(tcx
, item_id
, ty_span
).with_fcx(|fcx
, tcx
| {
631 let ty
= tcx
.type_of(tcx
.hir().local_def_id(item_id
));
632 let item_ty
= fcx
.normalize_associated_types_in(ty_span
, ty
);
634 let mut forbid_unsized
= true;
635 if allow_foreign_ty
{
636 let tail
= fcx
.tcx
.struct_tail_erasing_lifetimes(item_ty
, fcx
.param_env
);
637 if let ty
::Foreign(_
) = tail
.kind() {
638 forbid_unsized
= false;
642 fcx
.register_wf_obligation(item_ty
.into(), ty_span
, ObligationCauseCode
::MiscObligation
);
646 fcx
.tcx
.require_lang_item(LangItem
::Sized
, None
),
647 traits
::ObligationCause
::new(ty_span
, fcx
.body_id
, traits
::MiscObligation
),
651 // No implied bounds in a const, etc.
658 item
: &'tcx hir
::Item
<'tcx
>,
659 ast_self_ty
: &hir
::Ty
<'_
>,
660 ast_trait_ref
: &Option
<hir
::TraitRef
<'_
>>,
662 debug
!("check_impl: {:?}", item
);
664 for_item(tcx
, item
).with_fcx(|fcx
, tcx
| {
665 match *ast_trait_ref
{
666 Some(ref ast_trait_ref
) => {
667 // `#[rustc_reservation_impl]` impls are not real impls and
668 // therefore don't need to be WF (the trait's `Self: Trait` predicate
670 let trait_ref
= fcx
.tcx
.impl_trait_ref(item
.def_id
).unwrap();
672 fcx
.normalize_associated_types_in(ast_trait_ref
.path
.span
, trait_ref
);
673 let obligations
= traits
::wf
::trait_obligations(
678 ast_trait_ref
.path
.span
,
681 for obligation
in obligations
{
682 fcx
.register_predicate(obligation
);
686 let self_ty
= fcx
.tcx
.type_of(item
.def_id
);
687 let self_ty
= fcx
.normalize_associated_types_in(item
.span
, self_ty
);
688 fcx
.register_wf_obligation(
691 ObligationCauseCode
::MiscObligation
,
696 check_where_clauses(tcx
, fcx
, item
.span
, item
.def_id
.to_def_id(), None
);
698 fcx
.impl_implied_bounds(item
.def_id
.to_def_id(), item
.span
)
702 /// Checks where-clauses and inline bounds that are declared on `def_id`.
703 fn check_where_clauses
<'tcx
, 'fcx
>(
705 fcx
: &FnCtxt
<'fcx
, 'tcx
>,
708 return_ty
: Option
<(Ty
<'tcx
>, Span
)>,
710 debug
!("check_where_clauses(def_id={:?}, return_ty={:?})", def_id
, return_ty
);
712 let predicates
= fcx
.tcx
.predicates_of(def_id
);
713 let generics
= tcx
.generics_of(def_id
);
715 let is_our_default
= |def
: &ty
::GenericParamDef
| match def
.kind
{
716 GenericParamDefKind
::Type { has_default, .. }
=> {
717 has_default
&& def
.index
>= generics
.parent_count
as u32
722 // Check that concrete defaults are well-formed. See test `type-check-defaults.rs`.
723 // For example, this forbids the declaration:
725 // struct Foo<T = Vec<[u32]>> { .. }
727 // Here, the default `Vec<[u32]>` is not WF because `[u32]: Sized` does not hold.
728 for param
in &generics
.params
{
729 if let GenericParamDefKind
::Type { .. }
= param
.kind
{
730 if is_our_default(¶m
) {
731 let ty
= fcx
.tcx
.type_of(param
.def_id
);
732 // Ignore dependent defaults -- that is, where the default of one type
733 // parameter includes another (e.g., `<T, U = T>`). In those cases, we can't
734 // be sure if it will error or not as user might always specify the other.
735 if !ty
.needs_subst() {
736 fcx
.register_wf_obligation(
738 fcx
.tcx
.def_span(param
.def_id
),
739 ObligationCauseCode
::MiscObligation
,
746 // Check that trait predicates are WF when params are substituted by their defaults.
747 // We don't want to overly constrain the predicates that may be written but we want to
748 // catch cases where a default my never be applied such as `struct Foo<T: Copy = String>`.
749 // Therefore we check if a predicate which contains a single type param
750 // with a concrete default is WF with that default substituted.
751 // For more examples see tests `defaults-well-formedness.rs` and `type-check-defaults.rs`.
753 // First we build the defaulted substitution.
754 let substs
= InternalSubsts
::for_item(fcx
.tcx
, def_id
, |param
, _
| {
756 GenericParamDefKind
::Lifetime
=> {
757 // All regions are identity.
758 fcx
.tcx
.mk_param_from_def(param
)
761 GenericParamDefKind
::Type { .. }
=> {
762 // If the param has a default, ...
763 if is_our_default(param
) {
764 let default_ty
= fcx
.tcx
.type_of(param
.def_id
);
765 // ... and it's not a dependent default, ...
766 if !default_ty
.needs_subst() {
767 // ... then substitute it with the default.
768 return default_ty
.into();
772 fcx
.tcx
.mk_param_from_def(param
)
775 GenericParamDefKind
::Const
=> {
776 // FIXME(const_generics_defaults)
777 fcx
.tcx
.mk_param_from_def(param
)
782 // Now we build the substituted predicates.
783 let default_obligations
= predicates
786 .flat_map(|&(pred
, sp
)| {
789 params
: FxHashSet
<u32>,
791 impl<'tcx
> ty
::fold
::TypeVisitor
<'tcx
> for CountParams
{
794 fn visit_ty(&mut self, t
: Ty
<'tcx
>) -> ControlFlow
<Self::BreakTy
> {
795 if let ty
::Param(param
) = t
.kind() {
796 self.params
.insert(param
.index
);
798 t
.super_visit_with(self)
801 fn visit_region(&mut self, _
: ty
::Region
<'tcx
>) -> ControlFlow
<Self::BreakTy
> {
805 fn visit_const(&mut self, c
: &'tcx ty
::Const
<'tcx
>) -> ControlFlow
<Self::BreakTy
> {
806 if let ty
::ConstKind
::Param(param
) = c
.val
{
807 self.params
.insert(param
.index
);
809 c
.super_visit_with(self)
812 let mut param_count
= CountParams
::default();
813 let has_region
= pred
.visit_with(&mut param_count
).is_break();
814 let substituted_pred
= pred
.subst(fcx
.tcx
, substs
);
815 // Don't check non-defaulted params, dependent defaults (including lifetimes)
816 // or preds with multiple params.
817 if substituted_pred
.has_param_types_or_consts()
818 || param_count
.params
.len() > 1
822 } else if predicates
.predicates
.iter().any(|&(p
, _
)| p
== substituted_pred
) {
823 // Avoid duplication of predicates that contain no parameters, for example.
826 Some((substituted_pred
, sp
))
830 // Convert each of those into an obligation. So if you have
831 // something like `struct Foo<T: Copy = String>`, we would
832 // take that predicate `T: Copy`, substitute to `String: Copy`
833 // (actually that happens in the previous `flat_map` call),
834 // and then try to prove it (in this case, we'll fail).
836 // Note the subtle difference from how we handle `predicates`
837 // below: there, we are not trying to prove those predicates
838 // to be *true* but merely *well-formed*.
839 let pred
= fcx
.normalize_associated_types_in(sp
, pred
);
841 traits
::ObligationCause
::new(sp
, fcx
.body_id
, traits
::ItemObligation(def_id
));
842 traits
::Obligation
::new(cause
, fcx
.param_env
, pred
)
845 let predicates
= predicates
.instantiate_identity(fcx
.tcx
);
847 if let Some((mut return_ty
, span
)) = return_ty
{
848 if return_ty
.has_infer_types_or_consts() {
849 fcx
.select_obligations_where_possible(false, |_
| {}
);
850 return_ty
= fcx
.resolve_vars_if_possible(return_ty
);
852 check_opaque_types(tcx
, fcx
, def_id
.expect_local(), span
, return_ty
);
855 let predicates
= fcx
.normalize_associated_types_in(span
, predicates
);
857 debug
!("check_where_clauses: predicates={:?}", predicates
.predicates
);
858 assert_eq
!(predicates
.predicates
.len(), predicates
.spans
.len());
860 predicates
.predicates
.iter().zip(predicates
.spans
.iter()).flat_map(|(&p
, &sp
)| {
861 traits
::wf
::predicate_obligations(fcx
, fcx
.param_env
, fcx
.body_id
, p
, sp
)
864 for obligation
in wf_obligations
.chain(default_obligations
) {
865 debug
!("next obligation cause: {:?}", obligation
.cause
);
866 fcx
.register_predicate(obligation
);
870 fn check_fn_or_method
<'fcx
, 'tcx
>(
872 fcx
: &FnCtxt
<'fcx
, 'tcx
>,
874 sig
: ty
::PolyFnSig
<'tcx
>,
875 hir_decl
: &hir
::FnDecl
<'_
>,
877 implied_bounds
: &mut Vec
<Ty
<'tcx
>>,
879 let sig
= fcx
.normalize_associated_types_in(span
, sig
);
880 let sig
= fcx
.tcx
.liberate_late_bound_regions(def_id
, sig
);
882 for (&input_ty
, span
) in sig
.inputs().iter().zip(hir_decl
.inputs
.iter().map(|t
| t
.span
)) {
883 fcx
.register_wf_obligation(input_ty
.into(), span
, ObligationCauseCode
::MiscObligation
);
885 implied_bounds
.extend(sig
.inputs());
887 fcx
.register_wf_obligation(
889 hir_decl
.output
.span(),
890 ObligationCauseCode
::ReturnType
,
893 // FIXME(#25759) return types should not be implied bounds
894 implied_bounds
.push(sig
.output());
896 check_where_clauses(tcx
, fcx
, span
, def_id
, Some((sig
.output(), hir_decl
.output
.span())));
899 /// Checks "defining uses" of opaque `impl Trait` types to ensure that they meet the restrictions
900 /// laid for "higher-order pattern unification".
901 /// This ensures that inference is tractable.
902 /// In particular, definitions of opaque types can only use other generics as arguments,
903 /// and they cannot repeat an argument. Example:
906 /// type Foo<A, B> = impl Bar<A, B>;
908 /// // Okay -- `Foo` is applied to two distinct, generic types.
909 /// fn a<T, U>() -> Foo<T, U> { .. }
911 /// // Not okay -- `Foo` is applied to `T` twice.
912 /// fn b<T>() -> Foo<T, T> { .. }
914 /// // Not okay -- `Foo` is applied to a non-generic type.
915 /// fn b<T>() -> Foo<T, u32> { .. }
918 fn check_opaque_types
<'fcx
, 'tcx
>(
920 fcx
: &FnCtxt
<'fcx
, 'tcx
>,
921 fn_def_id
: LocalDefId
,
925 trace
!("check_opaque_types(ty={:?})", ty
);
926 ty
.fold_with(&mut ty
::fold
::BottomUpFolder
{
929 if let ty
::Opaque(def_id
, substs
) = *ty
.kind() {
930 trace
!("check_opaque_types: opaque_ty, {:?}, {:?}", def_id
, substs
);
931 let generics
= tcx
.generics_of(def_id
);
933 let opaque_hir_id
= if let Some(local_id
) = def_id
.as_local() {
934 tcx
.hir().local_def_id_to_hir_id(local_id
)
936 // Opaque types from other crates won't have defining uses in this crate.
939 if let hir
::ItemKind
::OpaqueTy(hir
::OpaqueTy { impl_trait_fn: Some(_), .. }
) =
940 tcx
.hir().expect_item(opaque_hir_id
).kind
942 // No need to check return position impl trait (RPIT)
943 // because for type and const parameters they are correct
944 // by construction: we convert
946 // fn foo<P0..Pn>() -> impl Trait
951 // fn foo<P0..Pn>() -> Foo<P0...Pn>.
953 // For lifetime parameters we convert
955 // fn foo<'l0..'ln>() -> impl Trait<'l0..'lm>
959 // type foo::<'p0..'pn>::Foo<'q0..'qm>
960 // fn foo<l0..'ln>() -> foo::<'static..'static>::Foo<'l0..'lm>.
962 // which would error here on all of the `'static` args.
965 if !may_define_opaque_type(tcx
, fn_def_id
, opaque_hir_id
) {
968 trace
!("check_opaque_types: may define, generics={:#?}", generics
);
969 let mut seen_params
: FxHashMap
<_
, Vec
<_
>> = FxHashMap
::default();
970 for (i
, arg
) in substs
.iter().enumerate() {
971 let arg_is_param
= match arg
.unpack() {
972 GenericArgKind
::Type(ty
) => matches
!(ty
.kind(), ty
::Param(_
)),
974 GenericArgKind
::Lifetime(region
) => {
975 if let ty
::ReStatic
= region
{
979 "non-defining opaque type use in defining scope",
982 tcx
.def_span(generics
.param_at(i
, tcx
).def_id
),
983 "cannot use static lifetime; use a bound lifetime \
984 instead or remove the lifetime parameter from the \
994 GenericArgKind
::Const(ct
) => matches
!(ct
.val
, ty
::ConstKind
::Param(_
)),
998 seen_params
.entry(arg
).or_default().push(i
);
1000 // Prevent `fn foo() -> Foo<u32>` from being defining.
1001 let opaque_param
= generics
.param_at(i
, tcx
);
1003 .struct_span_err(span
, "non-defining opaque type use in defining scope")
1005 tcx
.def_span(opaque_param
.def_id
),
1007 "used non-generic {} `{}` for generic parameter",
1008 opaque_param
.kind
.descr(),
1014 } // for (arg, param)
1016 for (_
, indices
) in seen_params
{
1017 if indices
.len() > 1 {
1018 let descr
= generics
.param_at(indices
[0], tcx
).kind
.descr();
1019 let spans
: Vec
<_
> = indices
1021 .map(|i
| tcx
.def_span(generics
.param_at(i
, tcx
).def_id
))
1024 .struct_span_err(span
, "non-defining opaque type use in defining scope")
1025 .span_note(spans
, &format
!("{} used multiple times", descr
))
1037 const HELP_FOR_SELF_TYPE
: &str = "consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, \
1038 `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one \
1039 of the previous types except `Self`)";
1041 fn check_method_receiver
<'fcx
, 'tcx
>(
1042 fcx
: &FnCtxt
<'fcx
, 'tcx
>,
1043 fn_sig
: &hir
::FnSig
<'_
>,
1044 method
: &ty
::AssocItem
,
1047 // Check that the method has a valid receiver type, given the type `Self`.
1048 debug
!("check_method_receiver({:?}, self_ty={:?})", method
, self_ty
);
1050 if !method
.fn_has_self_parameter
{
1054 let span
= fn_sig
.decl
.inputs
[0].span
;
1056 let sig
= fcx
.tcx
.fn_sig(method
.def_id
);
1057 let sig
= fcx
.normalize_associated_types_in(span
, sig
);
1058 let sig
= fcx
.tcx
.liberate_late_bound_regions(method
.def_id
, sig
);
1060 debug
!("check_method_receiver: sig={:?}", sig
);
1062 let self_ty
= fcx
.normalize_associated_types_in(span
, self_ty
);
1063 let self_ty
= fcx
.tcx
.liberate_late_bound_regions(method
.def_id
, ty
::Binder
::bind(self_ty
));
1065 let receiver_ty
= sig
.inputs()[0];
1067 let receiver_ty
= fcx
.normalize_associated_types_in(span
, receiver_ty
);
1069 fcx
.tcx
.liberate_late_bound_regions(method
.def_id
, ty
::Binder
::bind(receiver_ty
));
1071 if fcx
.tcx
.features().arbitrary_self_types
{
1072 if !receiver_is_valid(fcx
, span
, receiver_ty
, self_ty
, true) {
1073 // Report error; `arbitrary_self_types` was enabled.
1074 e0307(fcx
, span
, receiver_ty
);
1077 if !receiver_is_valid(fcx
, span
, receiver_ty
, self_ty
, false) {
1078 if receiver_is_valid(fcx
, span
, receiver_ty
, self_ty
, true) {
1079 // Report error; would have worked with `arbitrary_self_types`.
1081 &fcx
.tcx
.sess
.parse_sess
,
1082 sym
::arbitrary_self_types
,
1085 "`{}` cannot be used as the type of `self` without \
1086 the `arbitrary_self_types` feature",
1090 .help(HELP_FOR_SELF_TYPE
)
1093 // Report error; would not have worked with `arbitrary_self_types`.
1094 e0307(fcx
, span
, receiver_ty
);
1100 fn e0307(fcx
: &FnCtxt
<'fcx
, 'tcx
>, span
: Span
, receiver_ty
: Ty
<'_
>) {
1102 fcx
.tcx
.sess
.diagnostic(),
1105 "invalid `self` parameter type: {}",
1108 .note("type of `self` must be `Self` or a type that dereferences to it")
1109 .help(HELP_FOR_SELF_TYPE
)
1113 /// Returns whether `receiver_ty` would be considered a valid receiver type for `self_ty`. If
1114 /// `arbitrary_self_types` is enabled, `receiver_ty` must transitively deref to `self_ty`, possibly
1115 /// through a `*const/mut T` raw pointer. If the feature is not enabled, the requirements are more
1116 /// strict: `receiver_ty` must implement `Receiver` and directly implement
1117 /// `Deref<Target = self_ty>`.
1119 /// N.B., there are cases this function returns `true` but causes an error to be emitted,
1120 /// particularly when `receiver_ty` derefs to a type that is the same as `self_ty` but has the
1121 /// wrong lifetime. Be careful of this if you are calling this function speculatively.
1122 fn receiver_is_valid
<'fcx
, 'tcx
>(
1123 fcx
: &FnCtxt
<'fcx
, 'tcx
>,
1125 receiver_ty
: Ty
<'tcx
>,
1127 arbitrary_self_types_enabled
: bool
,
1129 let cause
= fcx
.cause(span
, traits
::ObligationCauseCode
::MethodReceiver
);
1131 let can_eq_self
= |ty
| fcx
.infcx
.can_eq(fcx
.param_env
, self_ty
, ty
).is_ok();
1133 // `self: Self` is always valid.
1134 if can_eq_self(receiver_ty
) {
1135 if let Some(mut err
) = fcx
.demand_eqtype_with_origin(&cause
, self_ty
, receiver_ty
) {
1141 let mut autoderef
= fcx
.autoderef(span
, receiver_ty
);
1143 // The `arbitrary_self_types` feature allows raw pointer receivers like `self: *const Self`.
1144 if arbitrary_self_types_enabled
{
1145 autoderef
= autoderef
.include_raw_pointers();
1148 // The first type is `receiver_ty`, which we know its not equal to `self_ty`; skip it.
1151 let receiver_trait_def_id
= fcx
.tcx
.require_lang_item(LangItem
::Receiver
, None
);
1153 // Keep dereferencing `receiver_ty` until we get to `self_ty`.
1155 if let Some((potential_self_ty
, _
)) = autoderef
.next() {
1157 "receiver_is_valid: potential self type `{:?}` to match `{:?}`",
1158 potential_self_ty
, self_ty
1161 if can_eq_self(potential_self_ty
) {
1162 fcx
.register_predicates(autoderef
.into_obligations());
1164 if let Some(mut err
) =
1165 fcx
.demand_eqtype_with_origin(&cause
, self_ty
, potential_self_ty
)
1172 // Without `feature(arbitrary_self_types)`, we require that each step in the
1173 // deref chain implement `receiver`
1174 if !arbitrary_self_types_enabled
1175 && !receiver_is_implemented(
1177 receiver_trait_def_id
,
1186 debug
!("receiver_is_valid: type `{:?}` does not deref to `{:?}`", receiver_ty
, self_ty
);
1187 // If he receiver already has errors reported due to it, consider it valid to avoid
1188 // unnecessary errors (#58712).
1189 return receiver_ty
.references_error();
1193 // Without `feature(arbitrary_self_types)`, we require that `receiver_ty` implements `Receiver`.
1194 if !arbitrary_self_types_enabled
1195 && !receiver_is_implemented(fcx
, receiver_trait_def_id
, cause
.clone(), receiver_ty
)
1203 fn receiver_is_implemented(
1204 fcx
: &FnCtxt
<'_
, 'tcx
>,
1205 receiver_trait_def_id
: DefId
,
1206 cause
: ObligationCause
<'tcx
>,
1207 receiver_ty
: Ty
<'tcx
>,
1209 let trait_ref
= ty
::TraitRef
{
1210 def_id
: receiver_trait_def_id
,
1211 substs
: fcx
.tcx
.mk_substs_trait(receiver_ty
, &[]),
1214 let obligation
= traits
::Obligation
::new(
1217 trait_ref
.without_const().to_predicate(fcx
.tcx
),
1220 if fcx
.predicate_must_hold_modulo_regions(&obligation
) {
1224 "receiver_is_implemented: type `{:?}` does not implement `Receiver` trait",
1231 fn check_variances_for_type_defn
<'tcx
>(
1233 item
: &hir
::Item
<'tcx
>,
1234 hir_generics
: &hir
::Generics
<'_
>,
1236 let ty
= tcx
.type_of(item
.def_id
);
1237 if tcx
.has_error_field(ty
) {
1241 let ty_predicates
= tcx
.predicates_of(item
.def_id
);
1242 assert_eq
!(ty_predicates
.parent
, None
);
1243 let variances
= tcx
.variances_of(item
.def_id
);
1245 let mut constrained_parameters
: FxHashSet
<_
> = variances
1248 .filter(|&(_
, &variance
)| variance
!= ty
::Bivariant
)
1249 .map(|(index
, _
)| Parameter(index
as u32))
1252 identify_constrained_generic_params(tcx
, ty_predicates
, None
, &mut constrained_parameters
);
1254 for (index
, _
) in variances
.iter().enumerate() {
1255 if constrained_parameters
.contains(&Parameter(index
as u32)) {
1259 let param
= &hir_generics
.params
[index
];
1262 hir
::ParamName
::Error
=> {}
1263 _
=> report_bivariance(tcx
, param
.span
, param
.name
.ident().name
),
1268 fn report_bivariance(tcx
: TyCtxt
<'_
>, span
: Span
, param_name
: Symbol
) {
1269 let mut err
= error_392(tcx
, span
, param_name
);
1271 let suggested_marker_id
= tcx
.lang_items().phantom_data();
1272 // Help is available only in presence of lang items.
1273 let msg
= if let Some(def_id
) = suggested_marker_id
{
1275 "consider removing `{}`, referring to it in a field, or using a marker such as `{}`",
1277 tcx
.def_path_str(def_id
),
1280 format
!("consider removing `{}` or referring to it in a field", param_name
)
1286 /// Feature gates RFC 2056 -- trivial bounds, checking for global bounds that
1288 fn check_false_global_bounds(fcx
: &FnCtxt
<'_
, '_
>, span
: Span
, id
: hir
::HirId
) {
1289 let empty_env
= ty
::ParamEnv
::empty();
1291 let def_id
= fcx
.tcx
.hir().local_def_id(id
);
1292 let predicates
= fcx
.tcx
.predicates_of(def_id
).predicates
.iter().map(|(p
, _
)| *p
);
1293 // Check elaborated bounds.
1294 let implied_obligations
= traits
::elaborate_predicates(fcx
.tcx
, predicates
);
1296 for obligation
in implied_obligations
{
1297 let pred
= obligation
.predicate
;
1298 // Match the existing behavior.
1299 if pred
.is_global() && !pred
.has_late_bound_regions() {
1300 let pred
= fcx
.normalize_associated_types_in(span
, pred
);
1301 let obligation
= traits
::Obligation
::new(
1302 traits
::ObligationCause
::new(span
, id
, traits
::TrivialBound
),
1306 fcx
.register_predicate(obligation
);
1310 fcx
.select_all_obligations_or_error();
1313 #[derive(Clone, Copy)]
1314 pub struct CheckTypeWellFormedVisitor
<'tcx
> {
1318 impl CheckTypeWellFormedVisitor
<'tcx
> {
1319 pub fn new(tcx
: TyCtxt
<'tcx
>) -> CheckTypeWellFormedVisitor
<'tcx
> {
1320 CheckTypeWellFormedVisitor { tcx }
1324 impl ParItemLikeVisitor
<'tcx
> for CheckTypeWellFormedVisitor
<'tcx
> {
1325 fn visit_item(&self, i
: &'tcx hir
::Item
<'tcx
>) {
1326 Visitor
::visit_item(&mut self.clone(), i
);
1329 fn visit_trait_item(&self, trait_item
: &'tcx hir
::TraitItem
<'tcx
>) {
1330 Visitor
::visit_trait_item(&mut self.clone(), trait_item
);
1333 fn visit_impl_item(&self, impl_item
: &'tcx hir
::ImplItem
<'tcx
>) {
1334 Visitor
::visit_impl_item(&mut self.clone(), impl_item
);
1337 fn visit_foreign_item(&self, foreign_item
: &'tcx hir
::ForeignItem
<'tcx
>) {
1338 Visitor
::visit_foreign_item(&mut self.clone(), foreign_item
)
1342 impl Visitor
<'tcx
> for CheckTypeWellFormedVisitor
<'tcx
> {
1343 type Map
= hir_map
::Map
<'tcx
>;
1345 fn nested_visit_map(&mut self) -> hir_visit
::NestedVisitorMap
<Self::Map
> {
1346 hir_visit
::NestedVisitorMap
::OnlyBodies(self.tcx
.hir())
1349 fn visit_item(&mut self, i
: &'tcx hir
::Item
<'tcx
>) {
1350 debug
!("visit_item: {:?}", i
);
1351 self.tcx
.ensure().check_item_well_formed(i
.def_id
);
1352 hir_visit
::walk_item(self, i
);
1355 fn visit_trait_item(&mut self, trait_item
: &'tcx hir
::TraitItem
<'tcx
>) {
1356 debug
!("visit_trait_item: {:?}", trait_item
);
1357 self.tcx
.ensure().check_trait_item_well_formed(trait_item
.def_id
);
1358 hir_visit
::walk_trait_item(self, trait_item
);
1361 fn visit_impl_item(&mut self, impl_item
: &'tcx hir
::ImplItem
<'tcx
>) {
1362 debug
!("visit_impl_item: {:?}", impl_item
);
1363 self.tcx
.ensure().check_impl_item_well_formed(impl_item
.def_id
);
1364 hir_visit
::walk_impl_item(self, impl_item
);
1367 fn visit_generic_param(&mut self, p
: &'tcx hir
::GenericParam
<'tcx
>) {
1368 check_param_wf(self.tcx
, p
);
1369 hir_visit
::walk_generic_param(self, p
);
1373 ///////////////////////////////////////////////////////////////////////////
1376 // FIXME(eddyb) replace this with getting fields/discriminants through `ty::AdtDef`.
1377 struct AdtVariant
<'tcx
> {
1378 /// Types of fields in the variant, that must be well-formed.
1379 fields
: Vec
<AdtField
<'tcx
>>,
1381 /// Explicit discriminant of this variant (e.g. `A = 123`),
1382 /// that must evaluate to a constant value.
1383 explicit_discr
: Option
<LocalDefId
>,
1386 struct AdtField
<'tcx
> {
1391 impl<'a
, 'tcx
> FnCtxt
<'a
, 'tcx
> {
1392 // FIXME(eddyb) replace this with getting fields through `ty::AdtDef`.
1393 fn non_enum_variant(&self, struct_def
: &hir
::VariantData
<'_
>) -> AdtVariant
<'tcx
> {
1394 let fields
= struct_def
1398 let field_ty
= self.tcx
.type_of(self.tcx
.hir().local_def_id(field
.hir_id
));
1399 let field_ty
= self.normalize_associated_types_in(field
.ty
.span
, field_ty
);
1400 let field_ty
= self.resolve_vars_if_possible(field_ty
);
1401 debug
!("non_enum_variant: type of field {:?} is {:?}", field
, field_ty
);
1402 AdtField { ty: field_ty, span: field.ty.span }
1405 AdtVariant { fields, explicit_discr: None }
1408 fn enum_variants(&self, enum_def
: &hir
::EnumDef
<'_
>) -> Vec
<AdtVariant
<'tcx
>> {
1412 .map(|variant
| AdtVariant
{
1413 fields
: self.non_enum_variant(&variant
.data
).fields
,
1414 explicit_discr
: variant
1416 .map(|explicit_discr
| self.tcx
.hir().local_def_id(explicit_discr
.hir_id
)),
1421 pub(super) fn impl_implied_bounds(&self, impl_def_id
: DefId
, span
: Span
) -> Vec
<Ty
<'tcx
>> {
1422 match self.tcx
.impl_trait_ref(impl_def_id
) {
1423 Some(trait_ref
) => {
1424 // Trait impl: take implied bounds from all types that
1425 // appear in the trait reference.
1426 let trait_ref
= self.normalize_associated_types_in(span
, trait_ref
);
1427 trait_ref
.substs
.types().collect()
1431 // Inherent impl: take implied bounds from the `self` type.
1432 let self_ty
= self.tcx
.type_of(impl_def_id
);
1433 let self_ty
= self.normalize_associated_types_in(span
, self_ty
);
1440 fn error_392(tcx
: TyCtxt
<'_
>, span
: Span
, param_name
: Symbol
) -> DiagnosticBuilder
<'_
> {
1442 struct_span_err
!(tcx
.sess
, span
, E0392
, "parameter `{}` is never used", param_name
);
1443 err
.span_label(span
, "unused parameter");