2017-09-19 12:43:15 +00:00
|
|
|
//! "Collection" is the process of determining the type and other external
|
|
|
|
//! details of each item in Rust. Collection is specifically concerned
|
2019-02-28 22:43:53 +00:00
|
|
|
//! with *inter-procedural* things -- for example, for a function
|
2017-09-19 12:43:15 +00:00
|
|
|
//! definition, collection will figure out the type and signature of the
|
|
|
|
//! function, but it will not visit the *body* of the function in any way,
|
|
|
|
//! nor examine type annotations on local variables (that's the job of
|
|
|
|
//! type *checking*).
|
|
|
|
//!
|
|
|
|
//! Collecting is ultimately defined by a bundle of queries that
|
|
|
|
//! inquire after various facts about the items in the crate (e.g.,
|
|
|
|
//! `type_of`, `generics_of`, `predicates_of`, etc). See the `provide` function
|
|
|
|
//! for the full set.
|
|
|
|
//!
|
|
|
|
//! At present, however, we do run collection across all items in the
|
|
|
|
//! crate as a kind of pass. This should eventually be factored away.
|
2015-02-11 15:25:52 +00:00
|
|
|
|
2020-01-06 06:03:46 +00:00
|
|
|
use rustc_data_structures::captures::Captures;
|
2022-09-29 09:31:46 +00:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2023-12-21 09:52:27 +00:00
|
|
|
use rustc_data_structures::unord::UnordMap;
|
2023-04-06 23:04:42 +00:00
|
|
|
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, StashKey};
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir as hir;
|
2023-10-04 23:08:05 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2023-04-26 18:53:51 +00:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
|
2021-11-03 23:03:12 +00:00
|
|
|
use rustc_hir::intravisit::{self, Visitor};
|
2022-12-08 03:53:35 +00:00
|
|
|
use rustc_hir::{GenericParamKind, Node};
|
2023-01-22 05:11:24 +00:00
|
|
|
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
|
2022-12-27 23:56:46 +00:00
|
|
|
use rustc_infer::traits::ObligationCause;
|
2021-11-03 23:03:12 +00:00
|
|
|
use rustc_middle::hir::nested_filter;
|
2023-05-15 04:24:45 +00:00
|
|
|
use rustc_middle::query::Providers;
|
2022-09-29 09:31:46 +00:00
|
|
|
use rustc_middle::ty::util::{Discr, IntTypeExt};
|
2022-12-27 21:44:39 +00:00
|
|
|
use rustc_middle::ty::{self, AdtKind, Const, IsSuggestable, ToPredicate, Ty, TyCtxt};
|
2020-04-19 11:00:18 +00:00
|
|
|
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
2022-09-29 09:31:46 +00:00
|
|
|
use rustc_span::Span;
|
2024-01-04 13:45:06 +00:00
|
|
|
use rustc_target::abi::FieldIdx;
|
2022-12-08 03:53:35 +00:00
|
|
|
use rustc_target::spec::abi;
|
2022-12-27 01:44:16 +00:00
|
|
|
use rustc_trait_selection::infer::InferCtxtExt;
|
2020-04-05 22:33:33 +00:00
|
|
|
use rustc_trait_selection::traits::error_reporting::suggestions::NextTypeParamName;
|
2022-12-27 01:44:16 +00:00
|
|
|
use rustc_trait_selection::traits::ObligationCtxt;
|
2024-01-10 10:31:06 +00:00
|
|
|
use std::cell::Cell;
|
2021-03-08 23:32:41 +00:00
|
|
|
use std::iter;
|
2023-08-27 21:04:34 +00:00
|
|
|
use std::ops::Bound;
|
2016-06-21 22:08:13 +00:00
|
|
|
|
2023-10-04 23:08:05 +00:00
|
|
|
use crate::astconv::AstConv;
|
|
|
|
use crate::check::intrinsic::intrinsic_operation_unsafety;
|
|
|
|
use crate::errors;
|
|
|
|
pub use type_of::test_opaque_hidden_types;
|
|
|
|
|
2022-09-29 09:31:46 +00:00
|
|
|
mod generics_of;
|
2020-06-23 17:18:06 +00:00
|
|
|
mod item_bounds;
|
2022-09-29 09:31:46 +00:00
|
|
|
mod predicates_of;
|
2023-02-06 18:38:52 +00:00
|
|
|
mod resolve_bound_vars;
|
2020-01-17 21:15:03 +00:00
|
|
|
mod type_of;
|
|
|
|
|
2014-08-28 01:46:52 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Main entry point
|
2013-08-14 14:56:28 +00:00
|
|
|
|
2023-04-26 18:53:51 +00:00
|
|
|
fn collect_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
|
2022-07-03 13:28:57 +00:00
|
|
|
tcx.hir().visit_item_likes_in_module(module_def_id, &mut CollectItemTypesVisitor { tcx });
|
2012-05-16 02:36:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 20:00:14 +00:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2023-02-06 18:38:52 +00:00
|
|
|
resolve_bound_vars::provide(providers);
|
2017-02-13 23:11:24 +00:00
|
|
|
*providers = Providers {
|
2020-01-17 21:15:03 +00:00
|
|
|
type_of: type_of::type_of,
|
2023-08-27 22:02:54 +00:00
|
|
|
type_of_opaque: type_of::type_of_opaque,
|
2023-09-26 02:15:32 +00:00
|
|
|
type_alias_is_lazy: type_of::type_alias_is_lazy,
|
2020-06-23 17:18:06 +00:00
|
|
|
item_bounds: item_bounds::item_bounds,
|
2020-06-24 18:13:44 +00:00
|
|
|
explicit_item_bounds: item_bounds::explicit_item_bounds,
|
2022-09-29 09:31:46 +00:00
|
|
|
generics_of: generics_of::generics_of,
|
|
|
|
predicates_of: predicates_of::predicates_of,
|
2018-07-02 14:35:30 +00:00
|
|
|
predicates_defined_on,
|
2022-09-29 09:31:46 +00:00
|
|
|
explicit_predicates_of: predicates_of::explicit_predicates_of,
|
|
|
|
super_predicates_of: predicates_of::super_predicates_of,
|
2023-02-02 20:37:02 +00:00
|
|
|
implied_predicates_of: predicates_of::implied_predicates_of,
|
2023-05-03 20:13:32 +00:00
|
|
|
super_predicates_that_define_assoc_item:
|
|
|
|
predicates_of::super_predicates_that_define_assoc_item,
|
2022-09-29 09:31:46 +00:00
|
|
|
trait_explicit_predicates_and_bounds: predicates_of::trait_explicit_predicates_and_bounds,
|
|
|
|
type_param_predicates: predicates_of::type_param_predicates,
|
2017-02-13 23:11:24 +00:00
|
|
|
trait_def,
|
|
|
|
adt_def,
|
2017-05-13 14:11:52 +00:00
|
|
|
fn_sig,
|
2017-02-14 09:32:00 +00:00
|
|
|
impl_trait_ref,
|
2017-04-20 17:37:02 +00:00
|
|
|
impl_polarity,
|
2023-10-19 21:46:28 +00:00
|
|
|
coroutine_kind,
|
2024-01-24 22:27:25 +00:00
|
|
|
coroutine_for_closure,
|
2018-06-06 20:13:52 +00:00
|
|
|
collect_mod_item_types,
|
2023-01-18 18:03:06 +00:00
|
|
|
is_type_alias_impl_trait,
|
2024-01-04 13:45:06 +00:00
|
|
|
find_field,
|
2017-02-13 23:11:24 +00:00
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-01-05 09:24:00 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-02-18 01:46:26 +00:00
|
|
|
/// Context specific to some particular item. This is what implements
|
2022-08-08 04:16:47 +00:00
|
|
|
/// [`AstConv`].
|
2022-08-08 02:11:47 +00:00
|
|
|
///
|
2022-08-08 04:16:47 +00:00
|
|
|
/// # `ItemCtxt` vs `FnCtxt`
|
2022-08-08 02:11:47 +00:00
|
|
|
///
|
2022-08-08 04:16:47 +00:00
|
|
|
/// `ItemCtxt` is primarily used to type-check item signatures and lower them
|
|
|
|
/// from HIR to their [`ty::Ty`] representation, which is exposed using [`AstConv`].
|
|
|
|
/// It's also used for the bodies of items like structs where the body (the fields)
|
|
|
|
/// are just signatures.
|
|
|
|
///
|
2022-10-20 21:47:49 +00:00
|
|
|
/// This is in contrast to `FnCtxt`, which is used to type-check bodies of
|
2022-08-08 04:16:47 +00:00
|
|
|
/// functions, closures, and `const`s -- anywhere that expressions and statements show up.
|
|
|
|
///
|
|
|
|
/// An important thing to note is that `ItemCtxt` does no inference -- it has no [`InferCtxt`] --
|
|
|
|
/// while `FnCtxt` does do inference.
|
|
|
|
///
|
|
|
|
/// [`InferCtxt`]: rustc_infer::infer::InferCtxt
|
|
|
|
///
|
|
|
|
/// # Trait predicates
|
|
|
|
///
|
|
|
|
/// `ItemCtxt` has information about the predicates that are defined
|
2015-02-18 01:46:26 +00:00
|
|
|
/// on the trait. Unfortunately, this predicate information is
|
|
|
|
/// available in various different forms at various points in the
|
2018-11-27 02:59:49 +00:00
|
|
|
/// process. So we can't just store a pointer to e.g., the AST or the
|
2015-02-18 15:39:39 +00:00
|
|
|
/// parsed ty form, we have to be more flexible. To this end, the
|
2017-02-02 12:18:13 +00:00
|
|
|
/// `ItemCtxt` is parameterized by a `DefId` that it uses to satisfy
|
|
|
|
/// `get_type_parameter_bounds` requests, drawing the information from
|
|
|
|
/// the AST (`hir::Generics`), recursively.
|
2019-06-11 19:03:44 +00:00
|
|
|
pub struct ItemCtxt<'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-03-13 19:06:41 +00:00
|
|
|
item_def_id: LocalDefId,
|
2024-01-10 10:31:06 +00:00
|
|
|
tainted_by_errors: Cell<Option<ErrorGuaranteed>>,
|
2015-02-16 20:48:31 +00:00
|
|
|
}
|
|
|
|
|
2014-08-28 01:46:52 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-12-30 19:45:48 +00:00
|
|
|
#[derive(Default)]
|
2022-05-20 23:51:09 +00:00
|
|
|
pub(crate) struct HirPlaceholderCollector(pub(crate) Vec<Span>);
|
2019-12-24 21:18:09 +00:00
|
|
|
|
2022-01-05 10:43:21 +00:00
|
|
|
impl<'v> Visitor<'v> for HirPlaceholderCollector {
|
2019-12-24 21:18:09 +00:00
|
|
|
fn visit_ty(&mut self, t: &'v hir::Ty<'v>) {
|
|
|
|
if let hir::TyKind::Infer = t.kind {
|
|
|
|
self.0.push(t.span);
|
|
|
|
}
|
2020-01-05 01:37:57 +00:00
|
|
|
intravisit::walk_ty(self, t)
|
2019-12-24 21:18:09 +00:00
|
|
|
}
|
2021-04-24 21:41:57 +00:00
|
|
|
fn visit_generic_arg(&mut self, generic_arg: &'v hir::GenericArg<'v>) {
|
|
|
|
match generic_arg {
|
|
|
|
hir::GenericArg::Infer(inf) => {
|
|
|
|
self.0.push(inf.span);
|
2021-04-26 18:19:23 +00:00
|
|
|
intravisit::walk_inf(self, inf);
|
2021-04-24 21:41:57 +00:00
|
|
|
}
|
|
|
|
hir::GenericArg::Type(t) => self.visit_ty(t),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2022-01-05 10:43:21 +00:00
|
|
|
fn visit_array_length(&mut self, length: &'v hir::ArrayLen) {
|
2024-01-27 15:59:20 +00:00
|
|
|
if let hir::ArrayLen::Infer(inf) = length {
|
|
|
|
self.0.push(inf.span);
|
2022-01-05 10:43:21 +00:00
|
|
|
}
|
|
|
|
intravisit::walk_array_len(self, length)
|
|
|
|
}
|
2019-12-24 21:18:09 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
struct CollectItemTypesVisitor<'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2014-08-28 01:46:52 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 19:45:48 +00:00
|
|
|
/// If there are any placeholder types (`_`), emit an error explaining that this is not allowed
|
|
|
|
/// and suggest adding type parameters in the appropriate place, taking into consideration any and
|
|
|
|
/// all already existing generic type parameters to avoid suggesting a name that is already in use.
|
2022-05-20 23:51:09 +00:00
|
|
|
pub(crate) fn placeholder_type_error<'tcx>(
|
2019-12-24 21:18:09 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2022-02-07 21:58:30 +00:00
|
|
|
generics: Option<&hir::Generics<'_>>,
|
2019-12-24 21:18:09 +00:00
|
|
|
placeholder_types: Vec<Span>,
|
|
|
|
suggest: bool,
|
2021-02-10 15:49:23 +00:00
|
|
|
hir_ty: Option<&hir::Ty<'_>>,
|
2021-06-18 23:01:37 +00:00
|
|
|
kind: &'static str,
|
2019-12-24 21:18:09 +00:00
|
|
|
) {
|
2019-12-26 22:01:45 +00:00
|
|
|
if placeholder_types.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-07 21:58:30 +00:00
|
|
|
placeholder_type_error_diag(tcx, generics, placeholder_types, vec![], suggest, hir_ty, kind)
|
|
|
|
.emit();
|
2022-03-28 02:43:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:51:09 +00:00
|
|
|
pub(crate) fn placeholder_type_error_diag<'tcx>(
|
2022-03-28 02:43:05 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2022-02-07 21:58:30 +00:00
|
|
|
generics: Option<&hir::Generics<'_>>,
|
2022-03-28 02:43:05 +00:00
|
|
|
placeholder_types: Vec<Span>,
|
|
|
|
additional_spans: Vec<Span>,
|
|
|
|
suggest: bool,
|
|
|
|
hir_ty: Option<&hir::Ty<'_>>,
|
|
|
|
kind: &'static str,
|
2023-12-19 04:26:24 +00:00
|
|
|
) -> DiagnosticBuilder<'tcx> {
|
2022-03-28 02:43:05 +00:00
|
|
|
if placeholder_types.is_empty() {
|
|
|
|
return bad_placeholder(tcx, additional_spans, kind);
|
|
|
|
}
|
|
|
|
|
2022-02-07 21:58:30 +00:00
|
|
|
let params = generics.map(|g| g.params).unwrap_or_default();
|
|
|
|
let type_name = params.next_type_param_name(None);
|
2019-12-26 22:01:45 +00:00
|
|
|
let mut sugg: Vec<_> =
|
2020-03-05 13:29:58 +00:00
|
|
|
placeholder_types.iter().map(|sp| (*sp, (*type_name).to_string())).collect();
|
2020-07-12 15:40:22 +00:00
|
|
|
|
2022-02-07 21:58:30 +00:00
|
|
|
if let Some(generics) = generics {
|
|
|
|
if let Some(arg) = params.iter().find(|arg| {
|
|
|
|
matches!(arg.name, hir::ParamName::Plain(Ident { name: kw::Underscore, .. }))
|
|
|
|
}) {
|
|
|
|
// Account for `_` already present in cases like `struct S<_>(_);` and suggest
|
|
|
|
// `struct S<T>(T);` instead of `struct S<_, T>(T);`.
|
|
|
|
sugg.push((arg.span, (*type_name).to_string()));
|
|
|
|
} else if let Some(span) = generics.span_for_param_suggestion() {
|
|
|
|
// Account for bounds, we want `fn foo<T: E, K>(_: K)` not `fn foo<T, K: E>(_: K)`.
|
2023-07-25 21:17:39 +00:00
|
|
|
sugg.push((span, format!(", {type_name}")));
|
2022-02-07 21:58:30 +00:00
|
|
|
} else {
|
2023-07-25 21:17:39 +00:00
|
|
|
sugg.push((generics.span, format!("<{type_name}>")));
|
2020-07-12 15:40:22 +00:00
|
|
|
}
|
2019-12-26 22:01:45 +00:00
|
|
|
}
|
2020-07-12 15:40:22 +00:00
|
|
|
|
2022-03-28 02:43:05 +00:00
|
|
|
let mut err =
|
|
|
|
bad_placeholder(tcx, placeholder_types.into_iter().chain(additional_spans).collect(), kind);
|
2021-02-09 08:42:08 +00:00
|
|
|
|
2021-02-10 15:49:23 +00:00
|
|
|
// Suggest, but only if it is not a function in const or static
|
2019-12-26 22:01:45 +00:00
|
|
|
if suggest {
|
2021-02-10 15:49:23 +00:00
|
|
|
let mut is_fn = false;
|
2021-06-16 09:47:26 +00:00
|
|
|
let mut is_const_or_static = false;
|
2021-02-10 15:49:23 +00:00
|
|
|
|
2022-02-26 10:43:47 +00:00
|
|
|
if let Some(hir_ty) = hir_ty
|
|
|
|
&& let hir::TyKind::BareFn(_) = hir_ty.kind
|
|
|
|
{
|
|
|
|
is_fn = true;
|
|
|
|
|
|
|
|
// Check if parent is const or static
|
|
|
|
is_const_or_static = matches!(
|
2024-02-09 20:58:36 +00:00
|
|
|
tcx.parent_hir_node(hir_ty.hir_id),
|
2022-02-26 10:43:47 +00:00
|
|
|
Node::Item(&hir::Item {
|
|
|
|
kind: hir::ItemKind::Const(..) | hir::ItemKind::Static(..),
|
|
|
|
..
|
|
|
|
}) | Node::TraitItem(&hir::TraitItem { kind: hir::TraitItemKind::Const(..), .. })
|
|
|
|
| Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. })
|
|
|
|
);
|
2021-02-10 15:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if function is wrapped around a const or static,
|
|
|
|
// then don't show the suggestion
|
2021-06-16 09:47:26 +00:00
|
|
|
if !(is_fn && is_const_or_static) {
|
2021-02-10 15:49:23 +00:00
|
|
|
err.multipart_suggestion(
|
|
|
|
"use type parameters instead",
|
|
|
|
sugg,
|
|
|
|
Applicability::HasPlaceholders,
|
|
|
|
);
|
|
|
|
}
|
2019-12-24 21:18:09 +00:00
|
|
|
}
|
2022-03-28 02:43:05 +00:00
|
|
|
|
|
|
|
err
|
2019-12-24 21:18:09 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 13:26:57 +00:00
|
|
|
fn reject_placeholder_type_signatures_in_item<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
item: &'tcx hir::Item<'tcx>,
|
|
|
|
) {
|
2019-12-24 21:18:09 +00:00
|
|
|
let (generics, suggest) = match &item.kind {
|
|
|
|
hir::ItemKind::Union(_, generics)
|
|
|
|
| hir::ItemKind::Enum(_, generics)
|
2020-01-09 21:46:37 +00:00
|
|
|
| hir::ItemKind::TraitAlias(generics, _)
|
|
|
|
| hir::ItemKind::Trait(_, _, generics, ..)
|
2020-11-22 22:46:21 +00:00
|
|
|
| hir::ItemKind::Impl(hir::Impl { generics, .. })
|
2020-01-09 21:46:37 +00:00
|
|
|
| hir::ItemKind::Struct(_, generics) => (generics, true),
|
|
|
|
hir::ItemKind::OpaqueTy(hir::OpaqueTy { generics, .. })
|
|
|
|
| hir::ItemKind::TyAlias(_, generics) => (generics, false),
|
2019-12-27 23:45:39 +00:00
|
|
|
// `static`, `fn` and `const` are handled elsewhere to suggest appropriate type.
|
2019-12-24 21:18:09 +00:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
2022-01-05 10:43:21 +00:00
|
|
|
let mut visitor = HirPlaceholderCollector::default();
|
2019-12-24 21:18:09 +00:00
|
|
|
visitor.visit_item(item);
|
|
|
|
|
2022-02-07 21:58:30 +00:00
|
|
|
placeholder_type_error(tcx, Some(generics), visitor.0, suggest, None, item.kind.descr());
|
2019-12-24 21:18:09 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 01:45:08 +00:00
|
|
|
impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
|
2021-11-03 23:03:12 +00:00
|
|
|
type NestedFilter = nested_filter::OnlyBodies;
|
2020-01-07 16:25:33 +00:00
|
|
|
|
2021-11-03 23:03:12 +00:00
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
|
|
|
self.tcx.hir()
|
2016-11-19 20:14:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 18:28:50 +00:00
|
|
|
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
|
2021-01-30 16:47:51 +00:00
|
|
|
convert_item(self.tcx, item.item_id());
|
2019-12-24 21:18:09 +00:00
|
|
|
reject_placeholder_type_signatures_in_item(self.tcx, item);
|
2016-11-10 14:49:53 +00:00
|
|
|
intravisit::walk_item(self, item);
|
|
|
|
}
|
|
|
|
|
2019-12-01 15:08:58 +00:00
|
|
|
fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
|
2019-12-01 16:10:12 +00:00
|
|
|
for param in generics.params {
|
2018-05-25 23:27:54 +00:00
|
|
|
match param.kind {
|
|
|
|
hir::GenericParamKind::Lifetime { .. } => {}
|
2018-08-19 16:34:21 +00:00
|
|
|
hir::GenericParamKind::Type { default: Some(_), .. } => {
|
2022-11-06 18:26:36 +00:00
|
|
|
self.tcx.ensure().type_of(param.def_id);
|
2018-05-25 23:27:54 +00:00
|
|
|
}
|
2018-05-27 20:54:10 +00:00
|
|
|
hir::GenericParamKind::Type { .. } => {}
|
2020-12-30 15:34:53 +00:00
|
|
|
hir::GenericParamKind::Const { default, .. } => {
|
2022-11-06 18:26:36 +00:00
|
|
|
self.tcx.ensure().type_of(param.def_id);
|
2020-12-30 15:34:53 +00:00
|
|
|
if let Some(default) = default {
|
2021-03-03 06:38:02 +00:00
|
|
|
// need to store default and type of default
|
2022-11-06 19:17:57 +00:00
|
|
|
self.tcx.ensure().type_of(default.def_id);
|
2022-11-06 18:26:36 +00:00
|
|
|
self.tcx.ensure().const_param_default(param.def_id);
|
2020-12-30 15:34:53 +00:00
|
|
|
}
|
2019-02-15 22:22:54 +00:00
|
|
|
}
|
2017-01-25 20:01:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
intravisit::walk_generics(self, generics);
|
|
|
|
}
|
|
|
|
|
2019-11-30 14:08:22 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
|
2022-11-06 13:29:21 +00:00
|
|
|
if let hir::ExprKind::Closure(closure) = expr.kind {
|
|
|
|
self.tcx.ensure().generics_of(closure.def_id);
|
|
|
|
self.tcx.ensure().codegen_fn_attrs(closure.def_id);
|
2021-11-19 21:18:28 +00:00
|
|
|
// We do not call `type_of` for closures here as that
|
|
|
|
// depends on typecheck and would therefore hide
|
|
|
|
// any further errors in case one typeck fails.
|
2016-11-12 21:20:02 +00:00
|
|
|
}
|
|
|
|
intravisit::walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
|
2019-11-28 20:47:10 +00:00
|
|
|
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
|
2021-01-30 19:46:50 +00:00
|
|
|
convert_trait_item(self.tcx, trait_item.trait_item_id());
|
2016-12-04 02:21:06 +00:00
|
|
|
intravisit::walk_trait_item(self, trait_item);
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:16:44 +00:00
|
|
|
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
|
2021-01-30 22:25:03 +00:00
|
|
|
convert_impl_item(self.tcx, impl_item.impl_item_id());
|
2016-11-08 18:23:59 +00:00
|
|
|
intravisit::walk_impl_item(self, impl_item);
|
2016-11-04 22:20:15 +00:00
|
|
|
}
|
2014-08-28 01:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utility types and common code for the above passes.
|
|
|
|
|
2021-12-23 09:01:51 +00:00
|
|
|
fn bad_placeholder<'tcx>(
|
2019-12-27 12:15:48 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
mut spans: Vec<Span>,
|
2021-06-18 23:01:37 +00:00
|
|
|
kind: &'static str,
|
2023-12-19 04:26:24 +00:00
|
|
|
) -> DiagnosticBuilder<'tcx> {
|
2023-07-25 21:17:39 +00:00
|
|
|
let kind = if kind.ends_with('s') { format!("{kind}es") } else { format!("{kind}s") };
|
2021-06-18 23:01:37 +00:00
|
|
|
|
2019-12-27 12:15:48 +00:00
|
|
|
spans.sort();
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().create_err(errors::PlaceholderNotAllowedItemSignatures { spans, kind })
|
2019-07-17 06:55:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 01:45:08 +00:00
|
|
|
impl<'tcx> ItemCtxt<'tcx> {
|
2023-03-13 19:06:41 +00:00
|
|
|
pub fn new(tcx: TyCtxt<'tcx>, item_def_id: LocalDefId) -> ItemCtxt<'tcx> {
|
2024-01-10 10:31:06 +00:00
|
|
|
ItemCtxt { tcx, item_def_id, tainted_by_errors: Cell::new(None) }
|
2015-02-16 20:48:31 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:23:51 +00:00
|
|
|
pub fn to_ty(&self, ast_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
|
2023-01-11 18:58:44 +00:00
|
|
|
self.astconv().ast_ty_to_ty(ast_ty)
|
2012-05-16 02:36:04 +00:00
|
|
|
}
|
2020-02-11 03:14:31 +00:00
|
|
|
|
|
|
|
pub fn hir_id(&self) -> hir::HirId {
|
2023-11-24 16:28:19 +00:00
|
|
|
self.tcx.local_def_id_to_hir_id(self.item_def_id)
|
2020-02-11 03:14:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn node(&self) -> hir::Node<'tcx> {
|
2023-12-01 13:28:34 +00:00
|
|
|
self.tcx.hir_node(self.hir_id())
|
2020-02-11 03:14:31 +00:00
|
|
|
}
|
2024-01-10 10:31:06 +00:00
|
|
|
|
|
|
|
fn check_tainted_by_errors(&self) -> Result<(), ErrorGuaranteed> {
|
|
|
|
match self.tainted_by_errors.get() {
|
|
|
|
Some(err) => Err(err),
|
|
|
|
None => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
2012-05-16 02:36:04 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 01:45:08 +00:00
|
|
|
impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
2018-08-19 16:34:21 +00:00
|
|
|
self.tcx
|
|
|
|
}
|
2012-05-16 02:36:04 +00:00
|
|
|
|
2022-10-31 16:19:36 +00:00
|
|
|
fn item_def_id(&self) -> DefId {
|
2023-03-13 19:06:41 +00:00
|
|
|
self.item_def_id.to_def_id()
|
2019-11-01 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 23:10:55 +00:00
|
|
|
fn get_type_parameter_bounds(
|
|
|
|
&self,
|
|
|
|
span: Span,
|
2023-03-13 19:06:41 +00:00
|
|
|
def_id: LocalDefId,
|
2020-12-03 23:10:55 +00:00
|
|
|
assoc_name: Ident,
|
|
|
|
) -> ty::GenericPredicates<'tcx> {
|
2023-03-13 19:06:41 +00:00
|
|
|
self.tcx.at(span).type_param_predicates((self.item_def_id, def_id, assoc_name))
|
2015-02-17 16:04:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 00:55:34 +00:00
|
|
|
fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option<ty::Region<'tcx>> {
|
2017-01-25 15:32:44 +00:00
|
|
|
None
|
2017-01-13 13:09:56 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 22:16:34 +00:00
|
|
|
fn allow_ty_infer(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
2018-09-26 15:32:23 +00:00
|
|
|
|
2019-06-06 00:55:34 +00:00
|
|
|
fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
|
2023-07-05 19:13:26 +00:00
|
|
|
Ty::new_error_with_message(self.tcx(), span, "bad placeholder type")
|
2012-05-16 02:36:04 +00:00
|
|
|
}
|
2014-08-06 02:44:21 +00:00
|
|
|
|
2022-02-02 03:24:45 +00:00
|
|
|
fn ct_infer(&self, ty: Ty<'tcx>, _: Option<&ty::GenericParamDef>, span: Span) -> Const<'tcx> {
|
2022-06-27 13:55:03 +00:00
|
|
|
let ty = self.tcx.fold_regions(ty, |r, _| match *r {
|
2023-04-26 00:14:16 +00:00
|
|
|
// This is never reached in practice. If it ever is reached,
|
|
|
|
// `ReErased` should be changed to `ReStatic`, and any other region
|
|
|
|
// left alone.
|
|
|
|
r => bug!("unexpected region: {r:?}"),
|
2021-02-24 20:21:18 +00:00
|
|
|
});
|
2023-07-04 13:46:32 +00:00
|
|
|
ty::Const::new_error_with_message(self.tcx(), ty, span, "bad placeholder constant")
|
2019-06-06 00:55:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-19 16:34:21 +00:00
|
|
|
fn projected_ty_from_poly_trait_ref(
|
|
|
|
&self,
|
|
|
|
span: Span,
|
|
|
|
item_def_id: DefId,
|
2023-02-01 14:23:51 +00:00
|
|
|
item_segment: &hir::PathSegment<'tcx>,
|
2018-08-19 16:34:21 +00:00
|
|
|
poly_trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
) -> Ty<'tcx> {
|
2018-10-24 20:30:34 +00:00
|
|
|
if let Some(trait_ref) = poly_trait_ref.no_bound_vars() {
|
2023-07-11 21:35:29 +00:00
|
|
|
let item_args = self.astconv().create_args_for_associated_item(
|
2019-12-08 17:04:17 +00:00
|
|
|
span,
|
|
|
|
item_def_id,
|
|
|
|
item_segment,
|
2023-07-11 21:35:29 +00:00
|
|
|
trait_ref.args,
|
2019-12-08 17:04:17 +00:00
|
|
|
);
|
2023-07-11 21:35:29 +00:00
|
|
|
Ty::new_projection(self.tcx(), item_def_id, item_args)
|
2016-05-03 02:23:22 +00:00
|
|
|
} else {
|
2019-06-17 22:40:24 +00:00
|
|
|
// There are no late-bound regions; we can just ignore the binder.
|
2023-04-06 23:04:42 +00:00
|
|
|
let (mut mpart_sugg, mut inferred_sugg) = (None, None);
|
|
|
|
let mut bound = String::new();
|
2020-02-11 03:14:31 +00:00
|
|
|
|
|
|
|
match self.node() {
|
2020-02-11 20:37:12 +00:00
|
|
|
hir::Node::Field(_) | hir::Node::Ctor(_) | hir::Node::Variant(_) => {
|
2022-09-20 05:11:23 +00:00
|
|
|
let item = self
|
|
|
|
.tcx
|
|
|
|
.hir()
|
|
|
|
.expect_item(self.tcx.hir().get_parent_item(self.hir_id()).def_id);
|
2020-02-11 20:37:12 +00:00
|
|
|
match &item.kind {
|
|
|
|
hir::ItemKind::Enum(_, generics)
|
|
|
|
| hir::ItemKind::Struct(_, generics)
|
|
|
|
| hir::ItemKind::Union(_, generics) => {
|
2020-02-14 06:45:48 +00:00
|
|
|
let lt_name = get_new_lifetime_name(self.tcx, poly_trait_ref, generics);
|
2021-02-15 23:30:06 +00:00
|
|
|
let (lt_sp, sugg) = match generics.params {
|
2023-07-25 21:17:39 +00:00
|
|
|
[] => (generics.span, format!("<{lt_name}>")),
|
|
|
|
[bound, ..] => (bound.span.shrink_to_lo(), format!("{lt_name}, ")),
|
2020-02-11 20:37:12 +00:00
|
|
|
};
|
2023-04-06 23:04:42 +00:00
|
|
|
mpart_sugg = Some(errors::AssociatedTypeTraitUninferredGenericParamsMultipartSuggestion {
|
|
|
|
fspan: lt_sp,
|
|
|
|
first: sugg,
|
|
|
|
sspan: span.with_hi(item_segment.ident.span.lo()),
|
|
|
|
second: format!(
|
|
|
|
"{}::",
|
|
|
|
// Replace the existing lifetimes with a new named lifetime.
|
2023-11-17 09:29:48 +00:00
|
|
|
self.tcx.instantiate_bound_regions_uncached(
|
2023-04-06 23:04:42 +00:00
|
|
|
poly_trait_ref,
|
|
|
|
|_| {
|
2023-11-14 13:13:27 +00:00
|
|
|
ty::Region::new_early_param(self.tcx, ty::EarlyParamRegion {
|
2023-04-06 23:04:42 +00:00
|
|
|
def_id: item_def_id,
|
|
|
|
index: 0,
|
|
|
|
name: Symbol::intern(<_name),
|
|
|
|
})
|
|
|
|
}
|
2020-02-11 20:37:12 +00:00
|
|
|
),
|
|
|
|
),
|
2023-04-06 23:04:42 +00:00
|
|
|
});
|
2020-02-11 20:37:12 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2020-02-11 03:14:31 +00:00
|
|
|
}
|
2020-04-17 00:38:52 +00:00
|
|
|
hir::Node::Item(hir::Item {
|
|
|
|
kind:
|
|
|
|
hir::ItemKind::Struct(..) | hir::ItemKind::Enum(..) | hir::ItemKind::Union(..),
|
|
|
|
..
|
|
|
|
}) => {}
|
2020-02-11 03:14:31 +00:00
|
|
|
hir::Node::Item(_)
|
|
|
|
| hir::Node::ForeignItem(_)
|
|
|
|
| hir::Node::TraitItem(_)
|
|
|
|
| hir::Node::ImplItem(_) => {
|
2023-04-06 23:04:42 +00:00
|
|
|
inferred_sugg = Some(span.with_hi(item_segment.ident.span.lo()));
|
|
|
|
bound = format!(
|
|
|
|
"{}::",
|
|
|
|
// Erase named lt, we want `<A as B<'_>::C`, not `<A as B<'a>::C`.
|
|
|
|
self.tcx.anonymize_bound_vars(poly_trait_ref).skip_binder(),
|
2020-02-11 03:14:31 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2023-07-05 19:13:26 +00:00
|
|
|
Ty::new_error(
|
|
|
|
self.tcx(),
|
2023-12-18 11:21:37 +00:00
|
|
|
self.tcx().dcx().emit_err(errors::AssociatedTypeTraitUninferredGenericParams {
|
2023-04-06 23:04:42 +00:00
|
|
|
span,
|
|
|
|
inferred_sugg,
|
|
|
|
bound,
|
|
|
|
mpart_sugg,
|
2023-07-05 19:13:26 +00:00
|
|
|
}),
|
|
|
|
)
|
2016-05-03 02:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 13:19:57 +00:00
|
|
|
fn probe_adt(&self, _span: Span, ty: Ty<'tcx>) -> Option<ty::AdtDef<'tcx>> {
|
|
|
|
// FIXME(#103640): Should we handle the case where `ty` is a projection?
|
|
|
|
ty.ty_adt_def()
|
|
|
|
}
|
|
|
|
|
2024-01-10 10:31:06 +00:00
|
|
|
fn set_tainted_by_errors(&self, err: ErrorGuaranteed) {
|
|
|
|
self.tainted_by_errors.set(Some(err));
|
2016-03-15 08:49:10 +00:00
|
|
|
}
|
2017-09-15 23:33:41 +00:00
|
|
|
|
|
|
|
fn record_ty(&self, _hir_id: hir::HirId, _ty: Ty<'tcx>, _span: Span) {
|
2019-06-17 22:40:24 +00:00
|
|
|
// There's no place to record types from signatures?
|
2017-09-15 23:33:41 +00:00
|
|
|
}
|
2023-01-22 05:11:24 +00:00
|
|
|
|
|
|
|
fn infcx(&self) -> Option<&InferCtxt<'tcx>> {
|
|
|
|
None
|
|
|
|
}
|
2012-05-16 02:36:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 06:45:48 +00:00
|
|
|
/// Synthesize a new lifetime name that doesn't clash with any of the lifetimes already present.
|
|
|
|
fn get_new_lifetime_name<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
poly_trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
generics: &hir::Generics<'tcx>,
|
|
|
|
) -> String {
|
|
|
|
let existing_lifetimes = tcx
|
|
|
|
.collect_referenced_late_bound_regions(&poly_trait_ref)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|lt| {
|
2020-12-18 18:24:55 +00:00
|
|
|
if let ty::BoundRegionKind::BrNamed(_, name) = lt {
|
2020-02-14 06:45:48 +00:00
|
|
|
Some(name.as_str().to_string())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.chain(generics.params.iter().filter_map(|param| {
|
|
|
|
if let hir::GenericParamKind::Lifetime { .. } = ¶m.kind {
|
|
|
|
Some(param.name.ident().as_str().to_string())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
.collect::<FxHashSet<String>>();
|
|
|
|
|
|
|
|
let a_to_z_repeat_n = |n| {
|
|
|
|
(b'a'..=b'z').map(move |c| {
|
2020-02-28 23:35:24 +00:00
|
|
|
let mut s = '\''.to_string();
|
2020-02-14 06:45:48 +00:00
|
|
|
s.extend(std::iter::repeat(char::from(c)).take(n));
|
|
|
|
s
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
// If all single char lifetime names are present, we wrap around and double the chars.
|
|
|
|
(1..).flat_map(a_to_z_repeat_n).find(|lt| !existing_lifetimes.contains(lt.as_str())).unwrap()
|
|
|
|
}
|
|
|
|
|
2021-01-30 16:47:51 +00:00
|
|
|
fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
|
|
|
|
let it = tcx.hir().item(item_id);
|
|
|
|
debug!("convert: item {} with id {}", it.ident, it.hir_id());
|
2022-10-27 03:02:18 +00:00
|
|
|
let def_id = item_id.owner_id.def_id;
|
2021-02-10 15:49:23 +00:00
|
|
|
|
2023-01-09 16:30:40 +00:00
|
|
|
match &it.kind {
|
2014-02-17 05:52:11 +00:00
|
|
|
// These don't define types.
|
2018-08-19 16:34:21 +00:00
|
|
|
hir::ItemKind::ExternCrate(_)
|
|
|
|
| hir::ItemKind::Use(..)
|
2021-12-11 11:52:23 +00:00
|
|
|
| hir::ItemKind::Macro(..)
|
2018-08-19 16:34:21 +00:00
|
|
|
| hir::ItemKind::Mod(_)
|
|
|
|
| hir::ItemKind::GlobalAsm(_) => {}
|
2020-11-11 21:40:09 +00:00
|
|
|
hir::ItemKind::ForeignMod { items, .. } => {
|
2023-01-09 16:30:40 +00:00
|
|
|
for item in *items {
|
2020-11-11 20:57:54 +00:00
|
|
|
let item = tcx.hir().foreign_item(item.id);
|
2022-10-27 03:02:18 +00:00
|
|
|
tcx.ensure().generics_of(item.owner_id);
|
|
|
|
tcx.ensure().type_of(item.owner_id);
|
|
|
|
tcx.ensure().predicates_of(item.owner_id);
|
2021-04-01 01:45:42 +00:00
|
|
|
match item.kind {
|
2022-11-06 16:28:07 +00:00
|
|
|
hir::ForeignItemKind::Fn(..) => {
|
|
|
|
tcx.ensure().codegen_fn_attrs(item.owner_id);
|
|
|
|
tcx.ensure().fn_sig(item.owner_id)
|
|
|
|
}
|
2021-04-01 01:45:42 +00:00
|
|
|
hir::ForeignItemKind::Static(..) => {
|
2022-11-06 16:28:07 +00:00
|
|
|
tcx.ensure().codegen_fn_attrs(item.owner_id);
|
2022-01-05 10:43:21 +00:00
|
|
|
let mut visitor = HirPlaceholderCollector::default();
|
2021-04-01 01:45:42 +00:00
|
|
|
visitor.visit_foreign_item(item);
|
2021-06-18 23:01:37 +00:00
|
|
|
placeholder_type_error(
|
|
|
|
tcx,
|
|
|
|
None,
|
|
|
|
visitor.0,
|
|
|
|
false,
|
|
|
|
None,
|
|
|
|
"static variable",
|
|
|
|
);
|
2021-04-01 01:45:42 +00:00
|
|
|
}
|
|
|
|
_ => (),
|
2017-05-13 14:11:52 +00:00
|
|
|
}
|
2015-12-22 21:35:02 +00:00
|
|
|
}
|
2015-02-11 15:25:52 +00:00
|
|
|
}
|
2022-10-30 09:17:16 +00:00
|
|
|
hir::ItemKind::Enum(..) => {
|
2020-05-01 09:32:20 +00:00
|
|
|
tcx.ensure().generics_of(def_id);
|
|
|
|
tcx.ensure().type_of(def_id);
|
|
|
|
tcx.ensure().predicates_of(def_id);
|
2022-10-30 09:17:16 +00:00
|
|
|
convert_enum_variant_types(tcx, def_id.to_def_id());
|
2018-08-19 16:34:21 +00:00
|
|
|
}
|
2020-01-18 00:14:29 +00:00
|
|
|
hir::ItemKind::Impl { .. } => {
|
2020-05-01 09:32:20 +00:00
|
|
|
tcx.ensure().generics_of(def_id);
|
|
|
|
tcx.ensure().type_of(def_id);
|
|
|
|
tcx.ensure().impl_trait_ref(def_id);
|
|
|
|
tcx.ensure().predicates_of(def_id);
|
2018-08-19 16:34:21 +00:00
|
|
|
}
|
2018-07-11 15:36:06 +00:00
|
|
|
hir::ItemKind::Trait(..) => {
|
2020-05-01 09:32:20 +00:00
|
|
|
tcx.ensure().generics_of(def_id);
|
|
|
|
tcx.ensure().trait_def(def_id);
|
2017-04-24 15:06:39 +00:00
|
|
|
tcx.at(it.span).super_predicates_of(def_id);
|
2020-05-01 09:32:20 +00:00
|
|
|
tcx.ensure().predicates_of(def_id);
|
2018-08-19 16:34:21 +00:00
|
|
|
}
|
2018-07-11 15:36:06 +00:00
|
|
|
hir::ItemKind::TraitAlias(..) => {
|
2020-05-01 09:32:20 +00:00
|
|
|
tcx.ensure().generics_of(def_id);
|
2023-02-02 20:37:02 +00:00
|
|
|
tcx.at(it.span).implied_predicates_of(def_id);
|
2018-10-12 00:50:03 +00:00
|
|
|
tcx.at(it.span).super_predicates_of(def_id);
|
2020-05-01 09:32:20 +00:00
|
|
|
tcx.ensure().predicates_of(def_id);
|
2018-08-19 16:34:21 +00:00
|
|
|
}
|
2023-01-09 16:30:40 +00:00
|
|
|
hir::ItemKind::Struct(struct_def, _) | hir::ItemKind::Union(struct_def, _) => {
|
2020-05-01 09:32:20 +00:00
|
|
|
tcx.ensure().generics_of(def_id);
|
|
|
|
tcx.ensure().type_of(def_id);
|
|
|
|
tcx.ensure().predicates_of(def_id);
|
2017-02-14 09:32:00 +00:00
|
|
|
|
|
|
|
for f in struct_def.fields() {
|
2022-11-06 19:46:55 +00:00
|
|
|
tcx.ensure().generics_of(f.def_id);
|
|
|
|
tcx.ensure().type_of(f.def_id);
|
|
|
|
tcx.ensure().predicates_of(f.def_id);
|
2015-08-05 22:29:52 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 19:46:55 +00:00
|
|
|
if let Some(ctor_def_id) = struct_def.ctor_def_id() {
|
2022-10-30 09:17:16 +00:00
|
|
|
convert_variant_ctor(tcx, ctor_def_id);
|
2015-07-22 18:53:52 +00:00
|
|
|
}
|
2018-08-19 16:34:21 +00:00
|
|
|
}
|
2018-07-03 17:38:14 +00:00
|
|
|
|
2020-09-26 16:56:03 +00:00
|
|
|
// Don't call `type_of` on opaque types, since that depends on type
|
|
|
|
// checking function bodies. `check_item_type` ensures that it's called
|
|
|
|
// instead.
|
|
|
|
hir::ItemKind::OpaqueTy(..) => {
|
|
|
|
tcx.ensure().generics_of(def_id);
|
|
|
|
tcx.ensure().predicates_of(def_id);
|
2020-09-07 09:01:45 +00:00
|
|
|
tcx.ensure().explicit_item_bounds(def_id);
|
2022-11-11 10:29:27 +00:00
|
|
|
tcx.ensure().item_bounds(def_id);
|
2020-09-26 16:56:03 +00:00
|
|
|
}
|
2022-11-11 10:29:27 +00:00
|
|
|
|
|
|
|
hir::ItemKind::TyAlias(..) => {
|
2020-05-01 09:32:20 +00:00
|
|
|
tcx.ensure().generics_of(def_id);
|
|
|
|
tcx.ensure().type_of(def_id);
|
|
|
|
tcx.ensure().predicates_of(def_id);
|
2022-11-11 10:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hir::ItemKind::Static(ty, ..) | hir::ItemKind::Const(ty, ..) => {
|
|
|
|
tcx.ensure().generics_of(def_id);
|
|
|
|
tcx.ensure().type_of(def_id);
|
|
|
|
tcx.ensure().predicates_of(def_id);
|
2024-01-09 18:12:56 +00:00
|
|
|
if !ty.is_suggestable_infer_ty() {
|
2022-11-11 10:29:27 +00:00
|
|
|
let mut visitor = HirPlaceholderCollector::default();
|
|
|
|
visitor.visit_item(it);
|
|
|
|
placeholder_type_error(tcx, None, visitor.0, false, None, it.kind.descr());
|
2017-05-13 14:11:52 +00:00
|
|
|
}
|
2017-03-17 03:16:40 +00:00
|
|
|
}
|
2022-11-11 10:29:27 +00:00
|
|
|
|
|
|
|
hir::ItemKind::Fn(..) => {
|
|
|
|
tcx.ensure().generics_of(def_id);
|
|
|
|
tcx.ensure().type_of(def_id);
|
|
|
|
tcx.ensure().predicates_of(def_id);
|
|
|
|
tcx.ensure().fn_sig(def_id);
|
2022-11-06 16:28:07 +00:00
|
|
|
tcx.ensure().codegen_fn_attrs(def_id);
|
2022-11-11 10:29:27 +00:00
|
|
|
}
|
2012-05-15 15:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-07 23:46:19 +00:00
|
|
|
|
2021-01-30 19:46:50 +00:00
|
|
|
fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
|
|
|
|
let trait_item = tcx.hir().trait_item(trait_item_id);
|
2022-10-27 03:02:18 +00:00
|
|
|
let def_id = trait_item_id.owner_id;
|
2022-09-20 05:11:23 +00:00
|
|
|
tcx.ensure().generics_of(def_id);
|
2017-02-14 09:32:00 +00:00
|
|
|
|
2019-09-26 16:07:54 +00:00
|
|
|
match trait_item.kind {
|
2020-03-03 18:46:22 +00:00
|
|
|
hir::TraitItemKind::Fn(..) => {
|
2022-11-06 16:28:07 +00:00
|
|
|
tcx.ensure().codegen_fn_attrs(def_id);
|
2022-09-20 05:11:23 +00:00
|
|
|
tcx.ensure().type_of(def_id);
|
|
|
|
tcx.ensure().fn_sig(def_id);
|
2020-02-17 20:07:50 +00:00
|
|
|
}
|
|
|
|
|
2023-06-10 21:50:36 +00:00
|
|
|
hir::TraitItemKind::Const(ty, body_id) => {
|
2022-09-20 05:11:23 +00:00
|
|
|
tcx.ensure().type_of(def_id);
|
2023-12-18 11:21:37 +00:00
|
|
|
if !tcx.dcx().has_stashed_diagnostic(ty.span, StashKey::ItemNoType)
|
2024-01-09 18:12:56 +00:00
|
|
|
&& !(ty.is_suggestable_infer_ty() && body_id.is_some())
|
2023-06-10 21:50:36 +00:00
|
|
|
{
|
|
|
|
// Account for `const C: _;`.
|
|
|
|
let mut visitor = HirPlaceholderCollector::default();
|
|
|
|
visitor.visit_trait_item(trait_item);
|
|
|
|
placeholder_type_error(tcx, None, visitor.0, false, None, "associated constant");
|
2022-09-16 02:24:14 +00:00
|
|
|
}
|
2020-06-27 20:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hir::TraitItemKind::Type(_, Some(_)) => {
|
2022-09-20 05:11:23 +00:00
|
|
|
tcx.ensure().item_bounds(def_id);
|
|
|
|
tcx.ensure().type_of(def_id);
|
2020-06-27 20:36:35 +00:00
|
|
|
// Account for `type T = _;`.
|
2022-01-05 10:43:21 +00:00
|
|
|
let mut visitor = HirPlaceholderCollector::default();
|
2020-02-17 20:07:50 +00:00
|
|
|
visitor.visit_trait_item(trait_item);
|
2022-02-07 21:58:30 +00:00
|
|
|
placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
|
2016-12-04 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 05:42:45 +00:00
|
|
|
hir::TraitItemKind::Type(_, None) => {
|
2022-09-20 05:11:23 +00:00
|
|
|
tcx.ensure().item_bounds(def_id);
|
2020-07-22 05:42:45 +00:00
|
|
|
// #74612: Visit and try to find bad placeholders
|
|
|
|
// even if there is no concrete type.
|
2022-01-05 10:43:21 +00:00
|
|
|
let mut visitor = HirPlaceholderCollector::default();
|
2020-07-22 05:42:45 +00:00
|
|
|
visitor.visit_trait_item(trait_item);
|
2021-02-10 15:49:23 +00:00
|
|
|
|
2022-02-07 21:58:30 +00:00
|
|
|
placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
|
2020-07-22 05:42:45 +00:00
|
|
|
}
|
2017-02-14 09:32:00 +00:00
|
|
|
};
|
2016-12-04 02:21:06 +00:00
|
|
|
|
2022-09-20 05:11:23 +00:00
|
|
|
tcx.ensure().predicates_of(def_id);
|
2016-12-04 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 22:25:03 +00:00
|
|
|
fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
|
2022-10-27 03:02:18 +00:00
|
|
|
let def_id = impl_item_id.owner_id;
|
2020-05-01 09:32:20 +00:00
|
|
|
tcx.ensure().generics_of(def_id);
|
|
|
|
tcx.ensure().type_of(def_id);
|
|
|
|
tcx.ensure().predicates_of(def_id);
|
2021-01-30 22:25:03 +00:00
|
|
|
let impl_item = tcx.hir().impl_item(impl_item_id);
|
2020-02-17 20:07:50 +00:00
|
|
|
match impl_item.kind {
|
2020-03-05 15:57:34 +00:00
|
|
|
hir::ImplItemKind::Fn(..) => {
|
2022-11-06 16:28:07 +00:00
|
|
|
tcx.ensure().codegen_fn_attrs(def_id);
|
2020-05-01 09:32:20 +00:00
|
|
|
tcx.ensure().fn_sig(def_id);
|
2020-02-17 20:07:50 +00:00
|
|
|
}
|
2022-10-09 07:09:57 +00:00
|
|
|
hir::ImplItemKind::Type(_) => {
|
2020-02-17 20:07:50 +00:00
|
|
|
// Account for `type T = _;`
|
2022-01-05 10:43:21 +00:00
|
|
|
let mut visitor = HirPlaceholderCollector::default();
|
2020-02-17 20:07:50 +00:00
|
|
|
visitor.visit_impl_item(impl_item);
|
2021-02-10 15:49:23 +00:00
|
|
|
|
2022-02-07 21:58:30 +00:00
|
|
|
placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
|
2020-02-17 20:07:50 +00:00
|
|
|
}
|
2023-06-10 21:50:36 +00:00
|
|
|
hir::ImplItemKind::Const(ty, _) => {
|
|
|
|
// Account for `const T: _ = ..;`
|
2024-01-09 18:12:56 +00:00
|
|
|
if !ty.is_suggestable_infer_ty() {
|
2023-06-10 21:50:36 +00:00
|
|
|
let mut visitor = HirPlaceholderCollector::default();
|
|
|
|
visitor.visit_impl_item(impl_item);
|
|
|
|
placeholder_type_error(tcx, None, visitor.0, false, None, "associated constant");
|
|
|
|
}
|
|
|
|
}
|
2017-05-13 14:11:52 +00:00
|
|
|
}
|
2016-11-08 18:23:59 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 09:17:16 +00:00
|
|
|
fn convert_variant_ctor(tcx: TyCtxt<'_>, def_id: LocalDefId) {
|
2020-05-01 09:32:20 +00:00
|
|
|
tcx.ensure().generics_of(def_id);
|
|
|
|
tcx.ensure().type_of(def_id);
|
|
|
|
tcx.ensure().predicates_of(def_id);
|
2015-07-22 18:53:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 09:17:16 +00:00
|
|
|
fn convert_enum_variant_types(tcx: TyCtxt<'_>, def_id: DefId) {
|
2017-04-24 12:20:46 +00:00
|
|
|
let def = tcx.adt_def(def_id);
|
2022-03-04 20:28:41 +00:00
|
|
|
let repr_type = def.repr().discr_type();
|
2017-02-05 05:01:48 +00:00
|
|
|
let initial = repr_type.initial_discriminant(tcx);
|
2019-09-09 12:26:25 +00:00
|
|
|
let mut prev_discr = None::<Discr<'_>>;
|
2017-02-05 05:01:48 +00:00
|
|
|
|
|
|
|
// fill the discriminant values and field types
|
2022-10-30 09:17:16 +00:00
|
|
|
for variant in def.variants() {
|
2018-01-25 15:44:45 +00:00
|
|
|
let wrapped_discr = prev_discr.map_or(initial, |d| d.wrap_incr(tcx));
|
2018-08-19 16:34:21 +00:00
|
|
|
prev_discr = Some(
|
2022-10-30 09:17:16 +00:00
|
|
|
if let ty::VariantDiscr::Explicit(const_def_id) = variant.discr {
|
|
|
|
def.eval_explicit_discr(tcx, const_def_id)
|
2018-08-19 16:34:21 +00:00
|
|
|
} else if let Some(discr) = repr_type.disr_incr(tcx, prev_discr) {
|
|
|
|
Some(discr)
|
|
|
|
} else {
|
2022-10-30 09:17:16 +00:00
|
|
|
let span = tcx.def_span(variant.def_id);
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(errors::EnumDiscriminantOverflowed {
|
2023-04-06 23:04:42 +00:00
|
|
|
span,
|
|
|
|
discr: prev_discr.unwrap().to_string(),
|
|
|
|
item_name: tcx.item_name(variant.def_id),
|
|
|
|
wrapped_discr: wrapped_discr.to_string(),
|
|
|
|
});
|
2018-08-19 16:34:21 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
.unwrap_or(wrapped_discr),
|
|
|
|
);
|
2017-02-05 05:01:48 +00:00
|
|
|
|
2022-10-30 09:17:16 +00:00
|
|
|
for f in &variant.fields {
|
|
|
|
tcx.ensure().generics_of(f.did);
|
|
|
|
tcx.ensure().type_of(f.did);
|
|
|
|
tcx.ensure().predicates_of(f.did);
|
2015-10-01 21:03:22 +00:00
|
|
|
}
|
2015-07-22 18:53:52 +00:00
|
|
|
|
2015-08-05 22:29:52 +00:00
|
|
|
// Convert the ctor, if any. This also registers the variant as
|
|
|
|
// an item.
|
2022-10-25 16:15:15 +00:00
|
|
|
if let Some(ctor_def_id) = variant.ctor_def_id() {
|
2022-10-30 09:17:16 +00:00
|
|
|
convert_variant_ctor(tcx, ctor_def_id.expect_local());
|
2019-03-21 22:38:50 +00:00
|
|
|
}
|
2015-07-22 18:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-24 07:17:02 +00:00
|
|
|
|
2024-01-04 13:56:45 +00:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
struct NestedSpan {
|
|
|
|
span: Span,
|
|
|
|
nested_field_span: Span,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum FieldDeclSpan {
|
|
|
|
NotNested(Span),
|
|
|
|
Nested(NestedSpan),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Span> for FieldDeclSpan {
|
|
|
|
fn from(span: Span) -> Self {
|
|
|
|
Self::NotNested(span)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<NestedSpan> for FieldDeclSpan {
|
|
|
|
fn from(span: NestedSpan) -> Self {
|
|
|
|
Self::Nested(span)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check the uniqueness of fields across adt where there are
|
|
|
|
/// nested fields imported from an unnamed field.
|
|
|
|
fn check_field_uniqueness_in_nested_adt(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
adt_def: ty::AdtDef<'_>,
|
|
|
|
check: &mut impl FnMut(Ident, /* nested_field_span */ Span),
|
|
|
|
) {
|
|
|
|
for field in adt_def.all_fields() {
|
|
|
|
if field.is_unnamed() {
|
|
|
|
// Here we don't care about the generic parameters, so `instantiate_identity` is enough.
|
|
|
|
match tcx.type_of(field.did).instantiate_identity().kind() {
|
|
|
|
ty::Adt(adt_def, _) => {
|
|
|
|
check_field_uniqueness_in_nested_adt(tcx, *adt_def, &mut *check);
|
2024-01-04 13:53:06 +00:00
|
|
|
}
|
2024-01-04 13:56:45 +00:00
|
|
|
ty_kind => bug!(
|
|
|
|
"Unexpected ty kind in check_field_uniqueness_in_nested_adt(): {ty_kind:?}"
|
|
|
|
),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
check(field.ident(tcx), tcx.def_span(field.did));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check the uniqueness of fields in a struct variant, and recursively
|
|
|
|
/// check the nested fields if it is an unnamed field with type of an
|
|
|
|
/// annoymous adt.
|
|
|
|
fn check_field_uniqueness(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
field: &hir::FieldDef<'_>,
|
|
|
|
check: &mut impl FnMut(Ident, FieldDeclSpan),
|
|
|
|
) {
|
|
|
|
if field.ident.name == kw::Underscore {
|
|
|
|
let ty_span = field.ty.span;
|
|
|
|
match &field.ty.kind {
|
|
|
|
hir::TyKind::AnonAdt(item_id) => {
|
|
|
|
match &tcx.hir_node(item_id.hir_id()).expect_item().kind {
|
|
|
|
hir::ItemKind::Struct(variant_data, ..)
|
|
|
|
| hir::ItemKind::Union(variant_data, ..) => {
|
|
|
|
variant_data
|
|
|
|
.fields()
|
|
|
|
.iter()
|
|
|
|
.for_each(|f| check_field_uniqueness(tcx, f, &mut *check));
|
2024-01-04 13:53:06 +00:00
|
|
|
}
|
2024-01-04 13:56:45 +00:00
|
|
|
item_kind => span_bug!(
|
|
|
|
ty_span,
|
|
|
|
"Unexpected item kind in check_field_uniqueness(): {item_kind:?}"
|
|
|
|
),
|
2024-01-04 13:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-04 13:56:45 +00:00
|
|
|
hir::TyKind::Path(hir::QPath::Resolved(_, hir::Path { res, .. })) => {
|
|
|
|
check_field_uniqueness_in_nested_adt(
|
|
|
|
tcx,
|
|
|
|
tcx.adt_def(res.def_id()),
|
|
|
|
&mut |ident, nested_field_span| {
|
|
|
|
check(ident, NestedSpan { span: field.span, nested_field_span }.into())
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// Abort due to errors (there must be an error if an unnamed field
|
|
|
|
// has any type kind other than an anonymous adt or a named adt)
|
|
|
|
_ => {
|
2024-01-04 13:45:06 +00:00
|
|
|
debug_assert!(tcx.dcx().has_errors().is_some());
|
|
|
|
tcx.dcx().abort_if_errors()
|
2024-01-04 13:56:45 +00:00
|
|
|
}
|
2024-01-04 13:53:06 +00:00
|
|
|
}
|
2024-01-04 13:56:45 +00:00
|
|
|
return;
|
2024-01-04 13:53:06 +00:00
|
|
|
}
|
2024-01-04 13:56:45 +00:00
|
|
|
check(field.ident, field.span.into());
|
2024-01-04 13:53:06 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 13:45:06 +00:00
|
|
|
fn find_field(tcx: TyCtxt<'_>, (def_id, ident): (DefId, Ident)) -> Option<FieldIdx> {
|
|
|
|
tcx.adt_def(def_id).non_enum_variant().fields.iter_enumerated().find_map(|(idx, field)| {
|
|
|
|
if field.is_unnamed() {
|
|
|
|
let field_ty = tcx.type_of(field.did).instantiate_identity();
|
|
|
|
let adt_def = field_ty.ty_adt_def().expect("expect Adt for unnamed field");
|
|
|
|
tcx.find_field((adt_def.did(), ident)).map(|_| idx)
|
|
|
|
} else {
|
|
|
|
(field.ident(tcx).normalize_to_macros_2_0() == ident).then_some(idx)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-21 21:49:03 +00:00
|
|
|
fn convert_variant(
|
|
|
|
tcx: TyCtxt<'_>,
|
2020-04-09 08:43:00 +00:00
|
|
|
variant_did: Option<LocalDefId>,
|
2018-12-01 02:47:08 +00:00
|
|
|
ident: Ident,
|
2018-08-19 16:34:21 +00:00
|
|
|
discr: ty::VariantDiscr,
|
2019-11-29 08:26:18 +00:00
|
|
|
def: &hir::VariantData<'_>,
|
2018-09-22 18:53:58 +00:00
|
|
|
adt_kind: ty::AdtKind,
|
2020-04-12 12:45:41 +00:00
|
|
|
parent_did: LocalDefId,
|
2024-01-04 13:56:45 +00:00
|
|
|
is_anonymous: bool,
|
2018-08-19 16:34:21 +00:00
|
|
|
) -> ty::VariantDef {
|
2024-01-04 13:53:06 +00:00
|
|
|
let mut has_unnamed_fields = false;
|
2024-01-04 13:56:45 +00:00
|
|
|
let mut seen_fields: FxHashMap<Ident, FieldDeclSpan> = Default::default();
|
2018-08-19 16:34:21 +00:00
|
|
|
let fields = def
|
|
|
|
.fields()
|
|
|
|
.iter()
|
2024-01-04 13:53:06 +00:00
|
|
|
.inspect(|f| {
|
2024-01-04 13:56:45 +00:00
|
|
|
has_unnamed_fields |= f.ident.name == kw::Underscore;
|
|
|
|
if !is_anonymous {
|
|
|
|
check_field_uniqueness(tcx, f, &mut |ident, field_decl| {
|
|
|
|
use FieldDeclSpan::*;
|
|
|
|
let field_name = ident.name;
|
|
|
|
let ident = ident.normalize_to_macros_2_0();
|
|
|
|
match (field_decl, seen_fields.get(&ident).copied()) {
|
|
|
|
(NotNested(span), Some(NotNested(prev_span))) => {
|
2024-01-04 13:45:06 +00:00
|
|
|
tcx.dcx().emit_err(errors::FieldAlreadyDeclared::NotNested {
|
2024-01-04 13:56:45 +00:00
|
|
|
field_name,
|
|
|
|
span,
|
|
|
|
prev_span,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
(NotNested(span), Some(Nested(prev))) => {
|
2024-01-04 13:45:06 +00:00
|
|
|
tcx.dcx().emit_err(errors::FieldAlreadyDeclared::PreviousNested {
|
2024-01-04 13:56:45 +00:00
|
|
|
field_name,
|
|
|
|
span,
|
|
|
|
prev_span: prev.span,
|
|
|
|
prev_nested_field_span: prev.nested_field_span,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
(
|
|
|
|
Nested(NestedSpan { span, nested_field_span }),
|
|
|
|
Some(NotNested(prev_span)),
|
|
|
|
) => {
|
2024-01-04 13:45:06 +00:00
|
|
|
tcx.dcx().emit_err(errors::FieldAlreadyDeclared::CurrentNested {
|
2024-01-04 13:56:45 +00:00
|
|
|
field_name,
|
|
|
|
span,
|
|
|
|
nested_field_span,
|
|
|
|
prev_span,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
(Nested(NestedSpan { span, nested_field_span }), Some(Nested(prev))) => {
|
2024-01-04 13:45:06 +00:00
|
|
|
tcx.dcx().emit_err(errors::FieldAlreadyDeclared::BothNested {
|
2024-01-04 13:56:45 +00:00
|
|
|
field_name,
|
|
|
|
span,
|
|
|
|
nested_field_span,
|
|
|
|
prev_span: prev.span,
|
|
|
|
prev_nested_field_span: prev.nested_field_span,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
(field_decl, None) => {
|
|
|
|
seen_fields.insert(ident, field_decl);
|
|
|
|
}
|
|
|
|
}
|
2020-08-27 10:00:21 +00:00
|
|
|
});
|
2018-08-19 16:34:21 +00:00
|
|
|
}
|
2024-01-04 13:53:06 +00:00
|
|
|
})
|
|
|
|
.map(|f| ty::FieldDef {
|
|
|
|
did: f.def_id.to_def_id(),
|
|
|
|
name: f.ident.name,
|
|
|
|
vis: tcx.visibility(f.def_id),
|
2018-08-19 16:34:21 +00:00
|
|
|
})
|
|
|
|
.collect();
|
2019-03-18 03:09:53 +00:00
|
|
|
let recovered = match def {
|
2023-12-19 22:47:30 +00:00
|
|
|
hir::VariantData::Struct { recovered, .. } => *recovered,
|
2019-03-18 03:09:53 +00:00
|
|
|
_ => false,
|
|
|
|
};
|
2019-03-21 22:38:50 +00:00
|
|
|
ty::VariantDef::new(
|
2022-01-03 03:37:05 +00:00
|
|
|
ident.name,
|
2020-04-09 08:43:00 +00:00
|
|
|
variant_did.map(LocalDefId::to_def_id),
|
2022-10-25 16:15:15 +00:00
|
|
|
def.ctor().map(|(kind, _, def_id)| (kind, def_id.to_def_id())),
|
2017-08-07 05:54:09 +00:00
|
|
|
discr,
|
|
|
|
fields,
|
2019-03-21 22:38:50 +00:00
|
|
|
adt_kind,
|
2020-04-12 12:45:41 +00:00
|
|
|
parent_did.to_def_id(),
|
2019-03-18 03:09:53 +00:00
|
|
|
recovered,
|
2023-03-13 18:54:05 +00:00
|
|
|
adt_kind == AdtKind::Struct && tcx.has_attr(parent_did, sym::non_exhaustive)
|
|
|
|
|| variant_did
|
2023-05-24 14:19:22 +00:00
|
|
|
.is_some_and(|variant_did| tcx.has_attr(variant_did, sym::non_exhaustive)),
|
2024-01-04 13:53:06 +00:00
|
|
|
has_unnamed_fields,
|
2018-12-01 02:47:08 +00:00
|
|
|
)
|
2015-07-22 18:53:52 +00:00
|
|
|
}
|
2014-02-24 07:17:02 +00:00
|
|
|
|
2023-03-13 18:54:05 +00:00
|
|
|
fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir::*;
|
2016-11-24 23:33:29 +00:00
|
|
|
|
2023-12-01 13:28:34 +00:00
|
|
|
let Node::Item(item) = tcx.hir_node_by_def_id(def_id) else {
|
2023-12-15 03:19:46 +00:00
|
|
|
bug!("expected ADT to be an item");
|
2017-02-13 23:11:24 +00:00
|
|
|
};
|
2017-02-05 05:01:48 +00:00
|
|
|
|
2024-01-04 13:53:06 +00:00
|
|
|
let is_anonymous = item.ident.name == kw::Empty;
|
2022-11-06 21:06:11 +00:00
|
|
|
let repr = tcx.repr_options_of_def(def_id.to_def_id());
|
2023-01-09 16:30:40 +00:00
|
|
|
let (kind, variants) = match &item.kind {
|
|
|
|
ItemKind::Enum(def, _) => {
|
2017-02-13 23:11:24 +00:00
|
|
|
let mut distance_from_explicit = 0;
|
2019-03-21 22:38:50 +00:00
|
|
|
let variants = def
|
|
|
|
.variants
|
|
|
|
.iter()
|
|
|
|
.map(|v| {
|
2023-01-09 16:30:40 +00:00
|
|
|
let discr = if let Some(e) = &v.disr_expr {
|
2019-03-21 22:38:50 +00:00
|
|
|
distance_from_explicit = 0;
|
2022-11-06 19:17:57 +00:00
|
|
|
ty::VariantDiscr::Explicit(e.def_id.to_def_id())
|
2019-03-21 22:38:50 +00:00
|
|
|
} else {
|
|
|
|
ty::VariantDiscr::Relative(distance_from_explicit)
|
|
|
|
};
|
|
|
|
distance_from_explicit += 1;
|
|
|
|
|
2019-08-14 00:40:21 +00:00
|
|
|
convert_variant(
|
|
|
|
tcx,
|
2022-11-06 19:46:55 +00:00
|
|
|
Some(v.def_id),
|
2019-08-14 00:40:21 +00:00
|
|
|
v.ident,
|
|
|
|
discr,
|
|
|
|
&v.data,
|
|
|
|
AdtKind::Enum,
|
|
|
|
def_id,
|
2024-01-04 13:56:45 +00:00
|
|
|
is_anonymous,
|
2019-08-14 00:40:21 +00:00
|
|
|
)
|
2019-03-21 22:38:50 +00:00
|
|
|
})
|
|
|
|
.collect();
|
2017-02-13 23:11:24 +00:00
|
|
|
|
2019-03-21 22:38:50 +00:00
|
|
|
(AdtKind::Enum, variants)
|
2017-02-13 23:11:24 +00:00
|
|
|
}
|
2023-01-09 16:30:40 +00:00
|
|
|
ItemKind::Struct(def, _) | ItemKind::Union(def, _) => {
|
2022-10-25 16:15:15 +00:00
|
|
|
let adt_kind = match item.kind {
|
|
|
|
ItemKind::Struct(..) => AdtKind::Struct,
|
|
|
|
_ => AdtKind::Union,
|
|
|
|
};
|
2019-03-21 22:38:50 +00:00
|
|
|
let variants = std::iter::once(convert_variant(
|
|
|
|
tcx,
|
2022-11-06 19:46:55 +00:00
|
|
|
None,
|
2019-03-21 22:38:50 +00:00
|
|
|
item.ident,
|
|
|
|
ty::VariantDiscr::Relative(0),
|
|
|
|
def,
|
2022-10-25 16:15:15 +00:00
|
|
|
adt_kind,
|
2019-03-21 22:38:50 +00:00
|
|
|
def_id,
|
2024-01-04 13:56:45 +00:00
|
|
|
is_anonymous,
|
2019-03-21 22:38:50 +00:00
|
|
|
))
|
|
|
|
.collect();
|
|
|
|
|
2022-10-25 16:15:15 +00:00
|
|
|
(adt_kind, variants)
|
2019-03-21 22:38:50 +00:00
|
|
|
}
|
2023-12-15 03:19:46 +00:00
|
|
|
_ => bug!("{:?} is not an ADT", item.owner_id.def_id),
|
2017-02-13 23:11:24 +00:00
|
|
|
};
|
2024-01-04 13:53:06 +00:00
|
|
|
tcx.mk_adt_def(def_id.to_def_id(), kind, variants, repr, is_anonymous)
|
2016-08-08 22:18:47 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 18:54:05 +00:00
|
|
|
fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
|
|
|
|
let item = tcx.hir().expect_item(def_id);
|
2015-02-11 15:25:52 +00:00
|
|
|
|
2021-12-21 15:40:50 +00:00
|
|
|
let (is_auto, unsafety, items) = match item.kind {
|
|
|
|
hir::ItemKind::Trait(is_auto, unsafety, .., items) => {
|
|
|
|
(is_auto == hir::IsAuto::Yes, unsafety, items)
|
|
|
|
}
|
|
|
|
hir::ItemKind::TraitAlias(..) => (false, hir::Unsafety::Normal, &[][..]),
|
2017-02-13 23:11:24 +00:00
|
|
|
_ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"),
|
|
|
|
};
|
2013-12-19 00:05:47 +00:00
|
|
|
|
2019-05-08 03:21:18 +00:00
|
|
|
let paren_sugar = tcx.has_attr(def_id, sym::rustc_paren_sugar);
|
2018-02-14 15:11:02 +00:00
|
|
|
if paren_sugar && !tcx.features().unboxed_closures {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(errors::ParenSugarAttribute { span: item.span });
|
2017-02-13 23:11:24 +00:00
|
|
|
}
|
2016-09-12 22:09:49 +00:00
|
|
|
|
2019-05-08 03:21:18 +00:00
|
|
|
let is_marker = tcx.has_attr(def_id, sym::marker);
|
2023-02-14 09:17:19 +00:00
|
|
|
let rustc_coinductive = tcx.has_attr(def_id, sym::rustc_coinductive);
|
2021-04-12 23:03:53 +00:00
|
|
|
let skip_array_during_method_dispatch =
|
|
|
|
tcx.has_attr(def_id, sym::rustc_skip_array_during_method_dispatch);
|
2023-02-14 09:17:19 +00:00
|
|
|
let specialization_kind = if tcx.has_attr(def_id, sym::rustc_unsafe_specialization_marker) {
|
2020-02-08 17:56:25 +00:00
|
|
|
ty::trait_def::TraitSpecializationKind::Marker
|
|
|
|
} else if tcx.has_attr(def_id, sym::rustc_specialization_trait) {
|
|
|
|
ty::trait_def::TraitSpecializationKind::AlwaysApplicable
|
|
|
|
} else {
|
|
|
|
ty::trait_def::TraitSpecializationKind::None
|
|
|
|
};
|
2021-12-21 15:40:50 +00:00
|
|
|
let must_implement_one_of = tcx
|
2022-05-02 07:31:56 +00:00
|
|
|
.get_attr(def_id, sym::rustc_must_implement_one_of)
|
2021-12-21 15:40:50 +00:00
|
|
|
// Check that there are at least 2 arguments of `#[rustc_must_implement_one_of]`
|
|
|
|
// and that they are all identifiers
|
|
|
|
.and_then(|attr| match attr.meta_item_list() {
|
|
|
|
Some(items) if items.len() < 2 => {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(errors::MustImplementOneOfAttribute { span: attr.span });
|
2021-12-21 15:40:50 +00:00
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
Some(items) => items
|
|
|
|
.into_iter()
|
|
|
|
.map(|item| item.ident().ok_or(item.span()))
|
|
|
|
.collect::<Result<Box<[_]>, _>>()
|
|
|
|
.map_err(|span| {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(errors::MustBeNameOfAssociatedFunction { span });
|
2021-12-21 15:40:50 +00:00
|
|
|
})
|
|
|
|
.ok()
|
|
|
|
.zip(Some(attr.span)),
|
|
|
|
// Error is reported by `rustc_attr!`
|
|
|
|
None => None,
|
|
|
|
})
|
|
|
|
// Check that all arguments of `#[rustc_must_implement_one_of]` reference
|
2022-01-04 10:21:56 +00:00
|
|
|
// functions in the trait with default implementations
|
2021-12-21 15:40:50 +00:00
|
|
|
.and_then(|(list, attr_span)| {
|
|
|
|
let errors = list.iter().filter_map(|ident| {
|
|
|
|
let item = items.iter().find(|item| item.ident == *ident);
|
|
|
|
|
|
|
|
match item {
|
|
|
|
Some(item) if matches!(item.kind, hir::AssocItemKind::Fn { .. }) => {
|
2023-06-01 06:14:06 +00:00
|
|
|
if !tcx.defaultness(item.id.owner_id).has_value() {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(errors::FunctionNotHaveDefaultImplementation {
|
2023-04-06 23:04:42 +00:00
|
|
|
span: item.span,
|
|
|
|
note_span: attr_span,
|
|
|
|
});
|
2021-12-21 15:40:50 +00:00
|
|
|
|
|
|
|
return Some(());
|
|
|
|
}
|
|
|
|
|
|
|
|
return None;
|
|
|
|
}
|
2022-01-27 09:44:25 +00:00
|
|
|
Some(item) => {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(errors::MustImplementNotFunction {
|
2023-04-06 23:04:42 +00:00
|
|
|
span: item.span,
|
|
|
|
span_note: errors::MustImplementNotFunctionSpanNote { span: attr_span },
|
|
|
|
note: errors::MustImplementNotFunctionNote {},
|
|
|
|
});
|
2022-01-27 09:44:25 +00:00
|
|
|
}
|
|
|
|
None => {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(errors::FunctionNotFoundInTrait { span: ident.span });
|
2022-01-27 09:44:25 +00:00
|
|
|
}
|
2021-12-21 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(())
|
|
|
|
});
|
|
|
|
|
|
|
|
(errors.count() == 0).then_some(list)
|
2022-01-14 13:38:47 +00:00
|
|
|
})
|
|
|
|
// Check for duplicates
|
|
|
|
.and_then(|list| {
|
2023-12-21 09:52:27 +00:00
|
|
|
let mut set: UnordMap<Symbol, Span> = Default::default();
|
2022-01-14 13:38:47 +00:00
|
|
|
let mut no_dups = true;
|
|
|
|
|
|
|
|
for ident in &*list {
|
2022-01-14 23:04:58 +00:00
|
|
|
if let Some(dup) = set.insert(ident.name, ident.span) {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx()
|
2023-04-06 23:04:42 +00:00
|
|
|
.emit_err(errors::FunctionNamesDuplicated { spans: vec![dup, ident.span] });
|
2022-01-14 13:38:47 +00:00
|
|
|
|
|
|
|
no_dups = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
no_dups.then_some(list)
|
2021-12-21 15:40:50 +00:00
|
|
|
});
|
|
|
|
|
2023-06-16 23:45:01 +00:00
|
|
|
let mut deny_explicit_impl = false;
|
|
|
|
let mut implement_via_object = true;
|
|
|
|
if let Some(attr) = tcx.get_attr(def_id, sym::rustc_deny_explicit_impl) {
|
|
|
|
deny_explicit_impl = true;
|
|
|
|
let mut seen_attr = false;
|
|
|
|
for meta in attr.meta_item_list().iter().flatten() {
|
|
|
|
if let Some(meta) = meta.meta_item()
|
|
|
|
&& meta.name_or_empty() == sym::implement_via_object
|
|
|
|
&& let Some(lit) = meta.name_value_literal()
|
|
|
|
{
|
|
|
|
if seen_attr {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().span_err(meta.span, "duplicated `implement_via_object` meta item");
|
2023-06-16 23:45:01 +00:00
|
|
|
}
|
|
|
|
seen_attr = true;
|
|
|
|
|
|
|
|
match lit.symbol {
|
|
|
|
kw::True => {
|
|
|
|
implement_via_object = true;
|
|
|
|
}
|
|
|
|
kw::False => {
|
|
|
|
implement_via_object = false;
|
|
|
|
}
|
|
|
|
_ => {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().span_err(
|
2023-06-16 23:45:01 +00:00
|
|
|
meta.span,
|
|
|
|
format!(
|
|
|
|
"unknown literal passed to `implement_via_object` attribute: {}",
|
|
|
|
lit.symbol
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().span_err(
|
2023-06-16 23:45:01 +00:00
|
|
|
meta.span(),
|
2023-07-25 21:17:39 +00:00
|
|
|
format!("unknown meta item passed to `rustc_deny_explicit_impl` {meta:?}"),
|
2023-06-16 23:45:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !seen_attr {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().span_err(attr.span, "missing `implement_via_object` meta item");
|
2023-06-16 23:45:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-21 15:40:50 +00:00
|
|
|
|
2023-02-14 09:17:19 +00:00
|
|
|
ty::TraitDef {
|
2023-03-13 18:54:05 +00:00
|
|
|
def_id: def_id.to_def_id(),
|
2021-04-12 23:03:53 +00:00
|
|
|
unsafety,
|
|
|
|
paren_sugar,
|
2023-02-14 09:17:19 +00:00
|
|
|
has_auto_impl: is_auto,
|
2021-04-12 23:03:53 +00:00
|
|
|
is_marker,
|
2023-02-14 09:17:19 +00:00
|
|
|
is_coinductive: rustc_coinductive || is_auto,
|
2021-04-12 23:03:53 +00:00
|
|
|
skip_array_during_method_dispatch,
|
2023-02-14 09:17:19 +00:00
|
|
|
specialization_kind,
|
2021-12-21 15:40:50 +00:00
|
|
|
must_implement_one_of,
|
2023-06-13 22:31:25 +00:00
|
|
|
implement_via_object,
|
|
|
|
deny_explicit_impl,
|
2023-02-14 09:17:19 +00:00
|
|
|
}
|
2012-05-16 02:36:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-26 04:10:07 +00:00
|
|
|
#[instrument(level = "debug", skip(tcx))]
|
2023-03-13 18:54:05 +00:00
|
|
|
fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<ty::PolyFnSig<'_>> {
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir::Node::*;
|
|
|
|
use rustc_hir::*;
|
2017-05-13 14:11:52 +00:00
|
|
|
|
2023-11-24 16:28:19 +00:00
|
|
|
let hir_id = tcx.local_def_id_to_hir_id(def_id);
|
2017-05-13 14:11:52 +00:00
|
|
|
|
2023-03-13 19:06:41 +00:00
|
|
|
let icx = ItemCtxt::new(tcx, def_id);
|
2017-05-13 14:11:52 +00:00
|
|
|
|
2023-12-01 13:28:34 +00:00
|
|
|
let output = match tcx.hir_node(hir_id) {
|
2018-08-22 22:05:26 +00:00
|
|
|
TraitItem(hir::TraitItem {
|
2020-03-05 15:57:34 +00:00
|
|
|
kind: TraitItemKind::Fn(sig, TraitFn::Provided(_)),
|
2019-12-23 22:16:34 +00:00
|
|
|
generics,
|
2018-08-19 16:34:21 +00:00
|
|
|
..
|
|
|
|
})
|
2022-02-07 21:58:30 +00:00
|
|
|
| Item(hir::Item { kind: ItemKind::Fn(sig, generics, _), .. }) => {
|
|
|
|
infer_return_ty_for_fn_sig(tcx, sig, generics, def_id, &icx)
|
2022-03-28 02:43:08 +00:00
|
|
|
}
|
2021-02-24 20:21:18 +00:00
|
|
|
|
2022-02-07 21:58:30 +00:00
|
|
|
ImplItem(hir::ImplItem { kind: ImplItemKind::Fn(sig, _), generics, .. }) => {
|
2022-10-20 09:39:09 +00:00
|
|
|
// Do not try to infer the return type for a impl method coming from a trait
|
2024-02-09 20:58:36 +00:00
|
|
|
if let Item(hir::Item { kind: ItemKind::Impl(i), .. }) = tcx.parent_hir_node(hir_id)
|
2022-03-28 02:43:08 +00:00
|
|
|
&& i.of_trait.is_some()
|
|
|
|
{
|
2023-01-11 18:58:44 +00:00
|
|
|
icx.astconv().ty_of_fn(
|
2020-10-26 18:18:31 +00:00
|
|
|
hir_id,
|
2019-12-23 22:16:34 +00:00
|
|
|
sig.header.unsafety,
|
|
|
|
sig.header.abi,
|
2021-09-30 17:38:50 +00:00
|
|
|
sig.decl,
|
2022-02-07 21:58:30 +00:00
|
|
|
Some(generics),
|
2021-02-10 15:49:23 +00:00
|
|
|
None,
|
2022-03-28 02:43:08 +00:00
|
|
|
)
|
|
|
|
} else {
|
2022-02-07 21:58:30 +00:00
|
|
|
infer_return_ty_for_fn_sig(tcx, sig, generics, def_id, &icx)
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2019-07-15 16:30:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TraitItem(hir::TraitItem {
|
2020-08-12 21:02:14 +00:00
|
|
|
kind: TraitItemKind::Fn(FnSig { header, decl, span: _ }, _),
|
2019-12-23 22:16:34 +00:00
|
|
|
generics,
|
2019-07-15 16:30:48 +00:00
|
|
|
..
|
2023-01-11 18:58:44 +00:00
|
|
|
}) => {
|
|
|
|
icx.astconv().ty_of_fn(hir_id, header.unsafety, header.abi, decl, Some(generics), None)
|
|
|
|
}
|
2018-08-19 16:34:21 +00:00
|
|
|
|
2022-02-07 21:58:30 +00:00
|
|
|
ForeignItem(&hir::ForeignItem { kind: ForeignItemKind::Fn(fn_decl, _, _), .. }) => {
|
2019-06-14 16:58:55 +00:00
|
|
|
let abi = tcx.hir().get_foreign_abi(hir_id);
|
2023-03-13 19:06:41 +00:00
|
|
|
compute_sig_of_foreign_fn_decl(tcx, def_id, fn_decl, abi)
|
2017-05-13 14:11:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 16:15:15 +00:00
|
|
|
Ctor(data) | Variant(hir::Variant { data, .. }) if data.ctor().is_some() => {
|
2023-08-27 21:04:34 +00:00
|
|
|
let adt_def_id = tcx.hir().get_parent_item(hir_id).def_id.to_def_id();
|
|
|
|
let ty = tcx.type_of(adt_def_id).instantiate_identity();
|
2023-07-11 21:35:29 +00:00
|
|
|
let inputs = data.fields().iter().map(|f| tcx.type_of(f.def_id).instantiate_identity());
|
2023-08-27 21:04:34 +00:00
|
|
|
// constructors for structs with `layout_scalar_valid_range` are unsafe to call
|
|
|
|
let safety = match tcx.layout_scalar_valid_range(adt_def_id) {
|
|
|
|
(Bound::Unbounded, Bound::Unbounded) => hir::Unsafety::Normal,
|
|
|
|
_ => hir::Unsafety::Unsafe,
|
|
|
|
};
|
|
|
|
ty::Binder::dummy(tcx.mk_fn_sig(inputs, ty, false, safety, abi::Abi::Rust))
|
2017-05-13 14:11:52 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 19:25:25 +00:00
|
|
|
Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
|
2017-12-06 20:15:27 +00:00
|
|
|
// Closure signatures are not like other function
|
|
|
|
// signatures and cannot be accessed through `fn_sig`. For
|
|
|
|
// example, a closure signature excludes the `self`
|
|
|
|
// argument. In any case they are embedded within the
|
2023-07-11 21:35:29 +00:00
|
|
|
// closure type as part of the `ClosureArgs`.
|
2017-12-01 14:26:13 +00:00
|
|
|
//
|
2020-03-18 00:16:01 +00:00
|
|
|
// To get the signature of a closure, you should use the
|
2023-07-11 21:35:29 +00:00
|
|
|
// `sig` method on the `ClosureArgs`:
|
2017-12-01 14:26:13 +00:00
|
|
|
//
|
2023-07-11 21:35:29 +00:00
|
|
|
// args.as_closure().sig(def_id, tcx)
|
|
|
|
bug!("to get the signature of a closure, use `args.as_closure().sig()` not `fn_sig()`",);
|
2017-05-13 14:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
x => {
|
|
|
|
bug!("unexpected sort of node in fn_sig(): {:?}", x);
|
|
|
|
}
|
2023-01-18 23:52:47 +00:00
|
|
|
};
|
2023-05-29 11:46:10 +00:00
|
|
|
ty::EarlyBinder::bind(output)
|
2017-05-13 14:11:52 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 02:43:08 +00:00
|
|
|
fn infer_return_ty_for_fn_sig<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-02-01 14:23:51 +00:00
|
|
|
sig: &hir::FnSig<'tcx>,
|
2022-03-28 02:43:08 +00:00
|
|
|
generics: &hir::Generics<'_>,
|
|
|
|
def_id: LocalDefId,
|
|
|
|
icx: &ItemCtxt<'tcx>,
|
|
|
|
) -> ty::PolyFnSig<'tcx> {
|
2023-11-24 16:28:19 +00:00
|
|
|
let hir_id = tcx.local_def_id_to_hir_id(def_id);
|
2022-03-28 02:43:08 +00:00
|
|
|
|
2024-01-09 18:12:56 +00:00
|
|
|
match sig.decl.output.get_infer_ret_ty() {
|
2022-03-28 02:43:08 +00:00
|
|
|
Some(ty) => {
|
|
|
|
let fn_sig = tcx.typeck(def_id).liberated_fn_sigs()[hir_id];
|
|
|
|
// Typeck doesn't expect erased regions to be returned from `type_of`.
|
2022-06-27 13:55:03 +00:00
|
|
|
let fn_sig = tcx.fold_regions(fn_sig, |r, _| match *r {
|
2022-03-28 02:43:08 +00:00
|
|
|
ty::ReErased => tcx.lifetimes.re_static,
|
|
|
|
_ => r,
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut visitor = HirPlaceholderCollector::default();
|
|
|
|
visitor.visit_ty(ty);
|
2023-04-22 19:29:30 +00:00
|
|
|
|
2022-03-28 02:43:08 +00:00
|
|
|
let mut diag = bad_placeholder(tcx, visitor.0, "return type");
|
2022-12-27 23:56:46 +00:00
|
|
|
let ret_ty = fn_sig.output();
|
2023-04-22 19:29:30 +00:00
|
|
|
// Don't leak types into signatures unless they're nameable!
|
|
|
|
// For example, if a function returns itself, we don't want that
|
|
|
|
// recursive function definition to leak out into the fn sig.
|
|
|
|
let mut should_recover = false;
|
|
|
|
|
2023-01-14 22:55:27 +00:00
|
|
|
if let Some(ret_ty) = ret_ty.make_suggestable(tcx, false) {
|
2022-04-25 03:11:51 +00:00
|
|
|
diag.span_suggestion(
|
|
|
|
ty.span,
|
|
|
|
"replace with the correct return type",
|
2022-06-13 06:48:40 +00:00
|
|
|
ret_ty,
|
2022-04-25 03:11:51 +00:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2023-04-22 19:29:30 +00:00
|
|
|
should_recover = true;
|
2023-03-22 09:36:30 +00:00
|
|
|
} else if let Some(sugg) = suggest_impl_trait(tcx, ret_ty, ty.span, def_id) {
|
2022-12-27 23:56:46 +00:00
|
|
|
diag.span_suggestion(
|
|
|
|
ty.span,
|
|
|
|
"replace with an appropriate return type",
|
|
|
|
sugg,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2022-04-25 03:11:51 +00:00
|
|
|
} else if ret_ty.is_closure() {
|
|
|
|
diag.help("consider using an `Fn`, `FnMut`, or `FnOnce` trait bound");
|
2023-01-04 00:26:53 +00:00
|
|
|
}
|
|
|
|
// Also note how `Fn` traits work just in case!
|
|
|
|
if ret_ty.is_closure() {
|
2022-12-27 01:44:16 +00:00
|
|
|
diag.note(
|
|
|
|
"for more information on `Fn` traits and closure types, see \
|
|
|
|
https://doc.rust-lang.org/book/ch13-01-closures.html",
|
|
|
|
);
|
2022-03-28 02:43:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-22 19:29:30 +00:00
|
|
|
let guar = diag.emit();
|
|
|
|
|
|
|
|
if should_recover {
|
|
|
|
ty::Binder::dummy(fn_sig)
|
|
|
|
} else {
|
|
|
|
ty::Binder::dummy(tcx.mk_fn_sig(
|
|
|
|
fn_sig.inputs().iter().copied(),
|
2023-07-05 19:13:26 +00:00
|
|
|
Ty::new_error(tcx, guar),
|
2023-04-22 19:29:30 +00:00
|
|
|
fn_sig.c_variadic,
|
|
|
|
fn_sig.unsafety,
|
|
|
|
fn_sig.abi,
|
|
|
|
))
|
|
|
|
}
|
2022-03-28 02:43:08 +00:00
|
|
|
}
|
2023-01-11 18:58:44 +00:00
|
|
|
None => icx.astconv().ty_of_fn(
|
2022-03-28 02:43:08 +00:00
|
|
|
hir_id,
|
|
|
|
sig.header.unsafety,
|
|
|
|
sig.header.abi,
|
|
|
|
sig.decl,
|
2022-02-07 21:58:30 +00:00
|
|
|
Some(generics),
|
2022-03-28 02:43:08 +00:00
|
|
|
None,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 23:56:46 +00:00
|
|
|
fn suggest_impl_trait<'tcx>(
|
2022-12-27 01:44:16 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
ret_ty: Ty<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
def_id: LocalDefId,
|
2022-12-27 23:56:46 +00:00
|
|
|
) -> Option<String> {
|
|
|
|
let format_as_assoc: fn(_, _, _, _, _) -> _ =
|
|
|
|
|tcx: TyCtxt<'tcx>,
|
2023-07-11 21:35:29 +00:00
|
|
|
_: ty::GenericArgsRef<'tcx>,
|
2022-12-27 23:56:46 +00:00
|
|
|
trait_def_id: DefId,
|
|
|
|
assoc_item_def_id: DefId,
|
|
|
|
item_ty: Ty<'tcx>| {
|
|
|
|
let trait_name = tcx.item_name(trait_def_id);
|
|
|
|
let assoc_name = tcx.item_name(assoc_item_def_id);
|
|
|
|
Some(format!("impl {trait_name}<{assoc_name} = {item_ty}>"))
|
|
|
|
};
|
|
|
|
let format_as_parenthesized: fn(_, _, _, _, _) -> _ =
|
|
|
|
|tcx: TyCtxt<'tcx>,
|
2023-07-11 21:35:29 +00:00
|
|
|
args: ty::GenericArgsRef<'tcx>,
|
2022-12-27 23:56:46 +00:00
|
|
|
trait_def_id: DefId,
|
|
|
|
_: DefId,
|
|
|
|
item_ty: Ty<'tcx>| {
|
|
|
|
let trait_name = tcx.item_name(trait_def_id);
|
2023-07-11 21:35:29 +00:00
|
|
|
let args_tuple = args.type_at(1);
|
2022-12-27 23:56:46 +00:00
|
|
|
let ty::Tuple(types) = *args_tuple.kind() else {
|
|
|
|
return None;
|
|
|
|
};
|
2023-01-14 22:55:27 +00:00
|
|
|
let types = types.make_suggestable(tcx, false)?;
|
2022-12-27 23:56:46 +00:00
|
|
|
let maybe_ret =
|
|
|
|
if item_ty.is_unit() { String::new() } else { format!(" -> {item_ty}") };
|
|
|
|
Some(format!(
|
|
|
|
"impl {trait_name}({}){maybe_ret}",
|
|
|
|
types.iter().map(|ty| ty.to_string()).collect::<Vec<_>>().join(", ")
|
|
|
|
))
|
|
|
|
};
|
|
|
|
|
|
|
|
for (trait_def_id, assoc_item_def_id, formatter) in [
|
|
|
|
(
|
|
|
|
tcx.get_diagnostic_item(sym::Iterator),
|
|
|
|
tcx.get_diagnostic_item(sym::IteratorItem),
|
|
|
|
format_as_assoc,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
tcx.lang_items().future_trait(),
|
|
|
|
tcx.get_diagnostic_item(sym::FutureOutput),
|
|
|
|
format_as_assoc,
|
|
|
|
),
|
|
|
|
(tcx.lang_items().fn_trait(), tcx.lang_items().fn_once_output(), format_as_parenthesized),
|
|
|
|
(
|
|
|
|
tcx.lang_items().fn_mut_trait(),
|
|
|
|
tcx.lang_items().fn_once_output(),
|
|
|
|
format_as_parenthesized,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
tcx.lang_items().fn_once_trait(),
|
|
|
|
tcx.lang_items().fn_once_output(),
|
|
|
|
format_as_parenthesized,
|
|
|
|
),
|
|
|
|
] {
|
|
|
|
let Some(trait_def_id) = trait_def_id else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
let Some(assoc_item_def_id) = assoc_item_def_id else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
if tcx.def_kind(assoc_item_def_id) != DefKind::AssocTy {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let param_env = tcx.param_env(def_id);
|
|
|
|
let infcx = tcx.infer_ctxt().build();
|
2023-07-11 21:35:29 +00:00
|
|
|
let args = ty::GenericArgs::for_item(tcx, trait_def_id, |param, _| {
|
2022-12-27 23:56:46 +00:00
|
|
|
if param.index == 0 { ret_ty.into() } else { infcx.var_for_def(span, param) }
|
|
|
|
});
|
2023-07-11 21:35:29 +00:00
|
|
|
if !infcx.type_implements_trait(trait_def_id, args, param_env).must_apply_modulo_regions() {
|
2022-12-27 23:56:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-06-29 08:02:26 +00:00
|
|
|
let ocx = ObligationCtxt::new(&infcx);
|
2022-12-27 23:56:46 +00:00
|
|
|
let item_ty = ocx.normalize(
|
2023-01-15 11:58:46 +00:00
|
|
|
&ObligationCause::misc(span, def_id),
|
2022-12-27 23:56:46 +00:00
|
|
|
param_env,
|
2023-07-11 21:35:29 +00:00
|
|
|
Ty::new_projection(tcx, assoc_item_def_id, args),
|
2022-12-27 23:56:46 +00:00
|
|
|
);
|
|
|
|
// FIXME(compiler-errors): We may benefit from resolving regions here.
|
|
|
|
if ocx.select_where_possible().is_empty()
|
|
|
|
&& let item_ty = infcx.resolve_vars_if_possible(item_ty)
|
2023-01-14 22:55:27 +00:00
|
|
|
&& let Some(item_ty) = item_ty.make_suggestable(tcx, false)
|
2023-07-11 21:35:29 +00:00
|
|
|
&& let Some(sugg) = formatter(
|
|
|
|
tcx,
|
|
|
|
infcx.resolve_vars_if_possible(args),
|
|
|
|
trait_def_id,
|
|
|
|
assoc_item_def_id,
|
|
|
|
item_ty,
|
|
|
|
)
|
2022-12-27 23:56:46 +00:00
|
|
|
{
|
|
|
|
return Some(sugg);
|
|
|
|
}
|
2022-12-27 01:44:16 +00:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2023-03-13 18:54:05 +00:00
|
|
|
fn impl_trait_ref(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
def_id: LocalDefId,
|
|
|
|
) -> Option<ty::EarlyBinder<ty::TraitRef<'_>>> {
|
2023-03-13 19:06:41 +00:00
|
|
|
let icx = ItemCtxt::new(tcx, def_id);
|
2023-03-13 18:54:05 +00:00
|
|
|
let impl_ = tcx.hir().expect_item(def_id).expect_impl();
|
2023-01-09 15:21:08 +00:00
|
|
|
impl_
|
|
|
|
.of_trait
|
|
|
|
.as_ref()
|
|
|
|
.map(|ast_trait_ref| {
|
2023-08-06 17:00:26 +00:00
|
|
|
let selfty = tcx.type_of(def_id).instantiate_identity();
|
|
|
|
|
|
|
|
if let Some(ErrorGuaranteed { .. }) = check_impl_constness(
|
2023-07-25 05:58:53 +00:00
|
|
|
tcx,
|
|
|
|
tcx.is_const_trait_impl_raw(def_id.to_def_id()),
|
2023-11-21 19:07:32 +00:00
|
|
|
ast_trait_ref,
|
2023-08-06 17:00:26 +00:00
|
|
|
) {
|
|
|
|
// we have a const impl, but for a trait without `#[const_trait]`, so
|
|
|
|
// without the host param. If we continue with the HIR trait ref, we get
|
|
|
|
// ICEs for generic arg count mismatch. We do a little HIR editing to
|
|
|
|
// make astconv happy.
|
|
|
|
let mut path_segments = ast_trait_ref.path.segments.to_vec();
|
|
|
|
let last_segment = path_segments.len() - 1;
|
2023-09-27 22:20:32 +00:00
|
|
|
let mut args = *path_segments[last_segment].args();
|
2023-08-06 17:00:26 +00:00
|
|
|
let last_arg = args.args.len() - 1;
|
2023-12-04 14:22:05 +00:00
|
|
|
assert!(matches!(args.args[last_arg], hir::GenericArg::Const(anon_const) if anon_const.is_desugared_from_effects));
|
2023-08-06 17:00:26 +00:00
|
|
|
args.args = &args.args[..args.args.len() - 1];
|
2023-02-01 14:23:51 +00:00
|
|
|
path_segments[last_segment].args = Some(tcx.hir_arena.alloc(args));
|
2023-08-06 17:00:26 +00:00
|
|
|
let path = hir::Path {
|
|
|
|
span: ast_trait_ref.path.span,
|
|
|
|
res: ast_trait_ref.path.res,
|
2023-02-01 14:23:51 +00:00
|
|
|
segments: tcx.hir_arena.alloc_slice(&path_segments),
|
2023-08-06 17:00:26 +00:00
|
|
|
};
|
2023-02-01 14:23:51 +00:00
|
|
|
let trait_ref = tcx.hir_arena.alloc(hir::TraitRef { path: tcx.hir_arena.alloc(path), hir_ref_id: ast_trait_ref.hir_ref_id });
|
|
|
|
icx.astconv().instantiate_mono_trait_ref(trait_ref, selfty)
|
2023-08-06 17:00:26 +00:00
|
|
|
} else {
|
2023-11-21 19:07:32 +00:00
|
|
|
icx.astconv().instantiate_mono_trait_ref(ast_trait_ref, selfty)
|
2023-08-06 17:00:26 +00:00
|
|
|
}
|
2023-01-09 15:21:08 +00:00
|
|
|
})
|
2023-05-29 11:46:10 +00:00
|
|
|
.map(ty::EarlyBinder::bind)
|
2015-02-11 15:25:52 +00:00
|
|
|
}
|
|
|
|
|
2023-08-06 17:00:26 +00:00
|
|
|
fn check_impl_constness(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
is_const: bool,
|
|
|
|
ast_trait_ref: &hir::TraitRef<'_>,
|
|
|
|
) -> Option<ErrorGuaranteed> {
|
|
|
|
if !is_const {
|
|
|
|
return None;
|
2022-10-25 18:28:04 +00:00
|
|
|
}
|
2023-08-06 17:00:26 +00:00
|
|
|
|
|
|
|
let trait_def_id = ast_trait_ref.trait_def_id()?;
|
|
|
|
if tcx.has_attr(trait_def_id, sym::const_trait) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let trait_name = tcx.item_name(trait_def_id).to_string();
|
2023-12-18 11:21:37 +00:00
|
|
|
Some(tcx.dcx().emit_err(errors::ConstImplForNonConstTrait {
|
2023-08-06 17:00:26 +00:00
|
|
|
trait_ref_span: ast_trait_ref.path.span,
|
|
|
|
trait_name,
|
|
|
|
local_trait_span:
|
|
|
|
trait_def_id.as_local().map(|_| tcx.def_span(trait_def_id).shrink_to_lo()),
|
|
|
|
marking: (),
|
|
|
|
adding: (),
|
|
|
|
}))
|
2022-10-25 18:28:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 18:54:05 +00:00
|
|
|
fn impl_polarity(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::ImplPolarity {
|
2019-07-13 21:09:46 +00:00
|
|
|
let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
|
2023-03-13 18:54:05 +00:00
|
|
|
let item = tcx.hir().expect_item(def_id);
|
2019-09-26 16:51:36 +00:00
|
|
|
match &item.kind {
|
2020-11-22 22:46:21 +00:00
|
|
|
hir::ItemKind::Impl(hir::Impl {
|
|
|
|
polarity: hir::ImplPolarity::Negative(span),
|
|
|
|
of_trait,
|
|
|
|
..
|
|
|
|
}) => {
|
2019-07-13 21:09:46 +00:00
|
|
|
if is_rustc_reservation {
|
2021-01-11 19:45:33 +00:00
|
|
|
let span = span.to(of_trait.as_ref().map_or(*span, |t| t.path.span));
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().span_err(span, "reservation impls can't be negative");
|
2019-07-13 21:09:46 +00:00
|
|
|
}
|
|
|
|
ty::ImplPolarity::Negative
|
|
|
|
}
|
2020-11-22 22:46:21 +00:00
|
|
|
hir::ItemKind::Impl(hir::Impl {
|
|
|
|
polarity: hir::ImplPolarity::Positive,
|
|
|
|
of_trait: None,
|
|
|
|
..
|
|
|
|
}) => {
|
2019-07-13 21:09:46 +00:00
|
|
|
if is_rustc_reservation {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().span_err(item.span, "reservation impls can't be inherent");
|
2019-07-13 21:09:46 +00:00
|
|
|
}
|
|
|
|
ty::ImplPolarity::Positive
|
|
|
|
}
|
2020-11-22 22:46:21 +00:00
|
|
|
hir::ItemKind::Impl(hir::Impl {
|
|
|
|
polarity: hir::ImplPolarity::Positive,
|
|
|
|
of_trait: Some(_),
|
|
|
|
..
|
|
|
|
}) => {
|
2019-07-13 21:09:46 +00:00
|
|
|
if is_rustc_reservation {
|
|
|
|
ty::ImplPolarity::Reservation
|
|
|
|
} else {
|
|
|
|
ty::ImplPolarity::Positive
|
|
|
|
}
|
|
|
|
}
|
2020-11-22 22:46:21 +00:00
|
|
|
item => bug!("impl_polarity: {:?} not an impl", item),
|
2017-04-20 17:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 15:39:39 +00:00
|
|
|
/// Returns the early-bound lifetimes declared in this generics
|
2019-02-08 13:53:55 +00:00
|
|
|
/// listing. For anything other than fns/methods, this is just all
|
2015-02-18 15:39:39 +00:00
|
|
|
/// the lifetimes that are declared. For fns or methods, we have to
|
|
|
|
/// screen out those that do not appear in any where-clauses etc using
|
|
|
|
/// `resolve_lifetime::early_bound_lifetimes`.
|
2019-06-11 19:03:44 +00:00
|
|
|
fn early_bound_lifetimes_from_generics<'a, 'tcx: 'a>(
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-12-01 15:08:58 +00:00
|
|
|
generics: &'a hir::Generics<'a>,
|
|
|
|
) -> impl Iterator<Item = &'a hir::GenericParam<'a>> + Captures<'tcx> {
|
2018-08-19 16:34:21 +00:00
|
|
|
generics.params.iter().filter(move |param| match param.kind {
|
2022-05-26 06:59:15 +00:00
|
|
|
GenericParamKind::Lifetime { .. } => !tcx.is_late_bound(param.hir_id),
|
2018-08-19 16:34:21 +00:00
|
|
|
_ => false,
|
|
|
|
})
|
2015-02-18 15:39:39 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Returns a list of type predicates for the definition with ID `def_id`, including inferred
|
|
|
|
/// lifetime constraints. This includes all predicates returned by `explicit_predicates_of`, plus
|
|
|
|
/// inferred constraints concerning which regions outlive other regions.
|
2022-06-26 04:10:07 +00:00
|
|
|
#[instrument(level = "debug", skip(tcx))]
|
2019-10-18 00:14:57 +00:00
|
|
|
fn predicates_defined_on(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> {
|
2018-11-08 06:14:13 +00:00
|
|
|
let mut result = tcx.explicit_predicates_of(def_id);
|
2018-11-16 13:25:46 +00:00
|
|
|
debug!("predicates_defined_on: explicit_predicates_of({:?}) = {:?}", def_id, result,);
|
2018-11-08 06:14:13 +00:00
|
|
|
let inferred_outlives = tcx.inferred_outlives_of(def_id);
|
|
|
|
if !inferred_outlives.is_empty() {
|
2018-11-16 13:25:46 +00:00
|
|
|
debug!(
|
|
|
|
"predicates_defined_on: inferred_outlives_of({:?}) = {:?}",
|
|
|
|
def_id, inferred_outlives,
|
|
|
|
);
|
2022-11-25 19:35:27 +00:00
|
|
|
let inferred_outlives_iter =
|
|
|
|
inferred_outlives.iter().map(|(clause, span)| ((*clause).to_predicate(tcx), *span));
|
2019-10-18 01:24:22 +00:00
|
|
|
if result.predicates.is_empty() {
|
2022-11-25 19:35:27 +00:00
|
|
|
result.predicates = tcx.arena.alloc_from_iter(inferred_outlives_iter);
|
2019-10-18 01:24:22 +00:00
|
|
|
} else {
|
2022-11-25 19:35:27 +00:00
|
|
|
result.predicates = tcx.arena.alloc_from_iter(
|
|
|
|
result.predicates.into_iter().copied().chain(inferred_outlives_iter),
|
|
|
|
);
|
2019-10-18 01:24:22 +00:00
|
|
|
}
|
2017-09-26 04:36:38 +00:00
|
|
|
}
|
2020-09-10 06:52:02 +00:00
|
|
|
|
2019-01-04 13:27:55 +00:00
|
|
|
debug!("predicates_defined_on({:?}) = {:?}", def_id, result);
|
2018-11-08 06:14:13 +00:00
|
|
|
result
|
2017-09-23 18:55:40 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 20:35:39 +00:00
|
|
|
fn compute_sig_of_foreign_fn_decl<'tcx>(
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-03-13 19:06:41 +00:00
|
|
|
def_id: LocalDefId,
|
2019-12-01 15:08:58 +00:00
|
|
|
decl: &'tcx hir::FnDecl<'tcx>,
|
2018-08-19 16:34:21 +00:00
|
|
|
abi: abi::Abi,
|
|
|
|
) -> ty::PolyFnSig<'tcx> {
|
2018-08-22 13:56:37 +00:00
|
|
|
let unsafety = if abi == abi::Abi::RustIntrinsic {
|
2023-03-13 19:06:41 +00:00
|
|
|
intrinsic_operation_unsafety(tcx, def_id.to_def_id())
|
2018-08-22 13:56:37 +00:00
|
|
|
} else {
|
|
|
|
hir::Unsafety::Unsafe
|
|
|
|
};
|
2023-11-24 16:28:19 +00:00
|
|
|
let hir_id = tcx.local_def_id_to_hir_id(def_id);
|
2023-01-11 18:58:44 +00:00
|
|
|
let fty =
|
|
|
|
ItemCtxt::new(tcx, def_id).astconv().ty_of_fn(hir_id, unsafety, abi, decl, None, None);
|
2012-05-16 02:36:04 +00:00
|
|
|
|
2019-03-16 00:04:02 +00:00
|
|
|
// Feature gate SIMD types in FFI, since I am not sure that the
|
|
|
|
// ABIs are handled at all correctly. -huonw
|
2018-08-19 16:34:21 +00:00
|
|
|
if abi != abi::Abi::RustIntrinsic
|
|
|
|
&& abi != abi::Abi::PlatformIntrinsic
|
|
|
|
&& !tcx.features().simd_ffi
|
|
|
|
{
|
2019-12-01 15:08:58 +00:00
|
|
|
let check = |ast_ty: &hir::Ty<'_>, ty: Ty<'_>| {
|
2016-02-23 19:06:32 +00:00
|
|
|
if ty.is_simd() {
|
2020-03-24 01:44:41 +00:00
|
|
|
let snip = tcx
|
|
|
|
.sess
|
|
|
|
.source_map()
|
|
|
|
.span_to_snippet(ast_ty.span)
|
2023-07-25 21:17:39 +00:00
|
|
|
.map_or_else(|_| String::new(), |s| format!(" `{s}`"));
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(errors::SIMDFFIHighlyExperimental { span: ast_ty.span, snip });
|
2016-02-23 19:06:32 +00:00
|
|
|
}
|
|
|
|
};
|
2021-03-08 23:32:41 +00:00
|
|
|
for (input, ty) in iter::zip(decl.inputs, fty.inputs().skip_binder()) {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 03:13:38 +00:00
|
|
|
check(input, *ty)
|
2016-02-23 19:06:32 +00:00
|
|
|
}
|
2023-01-09 16:30:40 +00:00
|
|
|
if let hir::FnRetTy::Return(ty) = decl.output {
|
2021-09-30 17:38:50 +00:00
|
|
|
check(ty, fty.output().skip_binder())
|
2016-02-23 19:06:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-13 14:11:52 +00:00
|
|
|
fty
|
2012-05-16 02:36:04 +00:00
|
|
|
}
|
2017-04-14 14:51:22 +00:00
|
|
|
|
2023-10-19 21:46:28 +00:00
|
|
|
fn coroutine_kind(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<hir::CoroutineKind> {
|
2023-12-01 13:28:34 +00:00
|
|
|
match tcx.hir_node_by_def_id(def_id) {
|
2023-12-22 21:29:12 +00:00
|
|
|
Node::Expr(&hir::Expr {
|
|
|
|
kind:
|
|
|
|
hir::ExprKind::Closure(&rustc_hir::Closure {
|
2023-12-25 16:56:12 +00:00
|
|
|
kind: hir::ClosureKind::Coroutine(kind),
|
2023-12-22 21:29:12 +00:00
|
|
|
..
|
|
|
|
}),
|
2020-02-08 22:33:50 +00:00
|
|
|
..
|
2023-12-22 21:29:12 +00:00
|
|
|
}) => Some(kind),
|
2023-03-13 18:54:05 +00:00
|
|
|
_ => None,
|
2020-01-26 01:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-18 18:03:06 +00:00
|
|
|
|
2024-01-24 22:27:25 +00:00
|
|
|
fn coroutine_for_closure(tcx: TyCtxt<'_>, def_id: LocalDefId) -> DefId {
|
2024-01-25 19:17:21 +00:00
|
|
|
let &rustc_hir::Closure { kind: hir::ClosureKind::CoroutineClosure(_), body, .. } =
|
|
|
|
tcx.hir_node_by_def_id(def_id).expect_closure()
|
2024-01-24 22:27:25 +00:00
|
|
|
else {
|
|
|
|
bug!()
|
|
|
|
};
|
|
|
|
|
|
|
|
let &hir::Expr {
|
|
|
|
kind:
|
|
|
|
hir::ExprKind::Closure(&rustc_hir::Closure {
|
|
|
|
def_id,
|
|
|
|
kind: hir::ClosureKind::Coroutine(_),
|
|
|
|
..
|
|
|
|
}),
|
|
|
|
..
|
|
|
|
} = tcx.hir().body(body).value
|
|
|
|
else {
|
|
|
|
bug!()
|
|
|
|
};
|
|
|
|
|
|
|
|
def_id.to_def_id()
|
|
|
|
}
|
|
|
|
|
2023-03-13 18:54:05 +00:00
|
|
|
fn is_type_alias_impl_trait<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> bool {
|
2023-12-01 13:28:34 +00:00
|
|
|
match tcx.hir_node_by_def_id(def_id) {
|
2023-03-13 18:54:05 +00:00
|
|
|
Node::Item(hir::Item { kind: hir::ItemKind::OpaqueTy(opaque), .. }) => {
|
2023-04-17 10:19:41 +00:00
|
|
|
matches!(opaque.origin, hir::OpaqueTyOrigin::TyAlias { .. })
|
2023-01-18 18:03:06 +00:00
|
|
|
}
|
2023-03-13 18:54:05 +00:00
|
|
|
_ => bug!("tried getting opaque_ty_origin for non-opaque: {:?}", def_id),
|
2023-01-18 18:03:06 +00:00
|
|
|
}
|
|
|
|
}
|