2022-07-07 16:14:27 +02:00
|
|
|
use crate::check::intrinsicck::InlineAsmCtxt;
|
|
|
|
|
2020-09-19 13:51:59 -07:00
|
|
|
use super::coercion::CoerceMany;
|
2020-06-27 21:36:35 +01:00
|
|
|
use super::compare_method::check_type_bounds;
|
2020-09-19 13:51:59 -07:00
|
|
|
use super::compare_method::{compare_const_impl, compare_impl_method, compare_ty_impl};
|
|
|
|
use super::*;
|
|
|
|
use rustc_attr as attr;
|
2022-03-24 02:03:04 +00:00
|
|
|
use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan};
|
2020-09-19 13:51:59 -07:00
|
|
|
use rustc_hir as hir;
|
2022-05-12 22:29:04 +02:00
|
|
|
use rustc_hir::def::{DefKind, Res};
|
2021-04-26 01:09:35 +08:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId};
|
2021-02-12 16:50:45 +08:00
|
|
|
use rustc_hir::intravisit::Visitor;
|
2020-09-19 13:51:59 -07:00
|
|
|
use rustc_hir::lang_items::LangItem;
|
2022-05-12 22:29:04 +02:00
|
|
|
use rustc_hir::{ItemKind, Node, PathSegment};
|
2022-06-27 18:46:16 +02:00
|
|
|
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
|
2020-09-19 13:51:59 -07:00
|
|
|
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
|
2022-07-02 16:37:49 +03:00
|
|
|
use rustc_infer::infer::{DefiningAnchor, RegionVariableOrigin, TyCtxtInferExt};
|
2022-03-31 13:52:40 +00:00
|
|
|
use rustc_infer::traits::Obligation;
|
2022-07-07 16:55:41 +00:00
|
|
|
use rustc_lint::builtin::REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS;
|
2021-11-03 18:03:12 -05:00
|
|
|
use rustc_middle::hir::nested_filter;
|
2022-04-27 18:14:19 +04:00
|
|
|
use rustc_middle::middle::stability::EvalResult;
|
2022-02-24 14:10:41 -05:00
|
|
|
use rustc_middle::ty::layout::{LayoutError, MAX_SIMD_LANES};
|
2020-09-19 13:51:59 -07:00
|
|
|
use rustc_middle::ty::subst::GenericArgKind;
|
2021-04-27 15:01:37 +02:00
|
|
|
use rustc_middle::ty::util::{Discr, IntTypeExt};
|
2022-06-17 13:15:00 +01:00
|
|
|
use rustc_middle::ty::{
|
|
|
|
self, ParamEnv, ToPredicate, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
|
|
|
|
};
|
2021-06-11 14:22:13 +03:00
|
|
|
use rustc_session::lint::builtin::{UNINHABITED_STATIC, UNSUPPORTED_CALLING_CONVENTIONS};
|
2020-09-19 13:51:59 -07:00
|
|
|
use rustc_span::symbol::sym;
|
2022-03-24 02:03:04 +00:00
|
|
|
use rustc_span::{self, Span};
|
2020-09-19 13:51:59 -07:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2020-09-07 10:01:45 +01:00
|
|
|
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
|
2022-07-07 15:12:32 +02:00
|
|
|
use rustc_trait_selection::traits::{self, ObligationCtxt};
|
2021-04-27 15:01:37 +02:00
|
|
|
use rustc_ty_utils::representability::{self, Representability};
|
2020-09-19 13:51:59 -07:00
|
|
|
|
2020-10-21 14:26:34 +02:00
|
|
|
use std::ops::ControlFlow;
|
|
|
|
|
2021-06-11 14:22:13 +03:00
|
|
|
pub(super) fn check_abi(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: Abi) {
|
|
|
|
match tcx.sess.target.is_abi_supported(abi) {
|
|
|
|
Some(true) => (),
|
2022-01-27 09:44:25 +00:00
|
|
|
Some(false) => {
|
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
span,
|
|
|
|
E0570,
|
2022-04-15 15:56:32 +09:00
|
|
|
"`{abi}` is not a supported ABI for the current target",
|
2022-01-27 09:44:25 +00:00
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
2021-06-11 14:22:13 +03:00
|
|
|
None => {
|
|
|
|
tcx.struct_span_lint_hir(UNSUPPORTED_CALLING_CONVENTIONS, hir_id, span, |lint| {
|
2022-01-22 18:49:12 -06:00
|
|
|
lint.build("use of calling convention not supported on this target").emit();
|
2021-06-11 14:22:13 +03:00
|
|
|
});
|
|
|
|
}
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
2021-01-24 17:15:05 +00:00
|
|
|
|
|
|
|
// This ABI is only allowed on function pointers
|
|
|
|
if abi == Abi::CCmseNonSecureCall {
|
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
span,
|
|
|
|
E0781,
|
2021-10-03 14:28:39 +09:00
|
|
|
"the `\"C-cmse-nonsecure-call\"` ABI is only allowed on function pointers"
|
2021-01-24 17:15:05 +00:00
|
|
|
)
|
2022-01-27 09:44:25 +00:00
|
|
|
.emit();
|
2021-01-24 17:15:05 +00:00
|
|
|
}
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper used for fns and closures. Does the grungy work of checking a function
|
|
|
|
/// body and returns the function context used for that purpose, since in the case of a fn item
|
|
|
|
/// there is still a bit more to do.
|
|
|
|
///
|
|
|
|
/// * ...
|
|
|
|
/// * inherited: other fields inherited from the enclosing fn (if any)
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(inherited, body), level = "debug")]
|
2020-09-19 13:51:59 -07:00
|
|
|
pub(super) fn check_fn<'a, 'tcx>(
|
|
|
|
inherited: &'a Inherited<'a, 'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
fn_sig: ty::FnSig<'tcx>,
|
|
|
|
decl: &'tcx hir::FnDecl<'tcx>,
|
|
|
|
fn_id: hir::HirId,
|
|
|
|
body: &'tcx hir::Body<'tcx>,
|
|
|
|
can_be_generator: Option<hir::Movability>,
|
2021-07-31 15:46:03 +02:00
|
|
|
return_type_pre_known: bool,
|
2020-09-19 13:51:59 -07:00
|
|
|
) -> (FnCtxt<'a, 'tcx>, Option<GeneratorTypes<'tcx>>) {
|
|
|
|
// Create the function context. This is either derived from scratch or,
|
|
|
|
// in the case of closures, based on the outer context.
|
|
|
|
let mut fcx = FnCtxt::new(inherited, param_env, body.value.hir_id);
|
2021-01-23 11:47:38 +01:00
|
|
|
fcx.ps.set(UnsafetyState::function(fn_sig.unsafety, fn_id));
|
2021-07-31 15:46:03 +02:00
|
|
|
fcx.return_type_pre_known = return_type_pre_known;
|
2020-09-19 13:51:59 -07:00
|
|
|
|
|
|
|
let tcx = fcx.tcx;
|
|
|
|
let hir = tcx.hir();
|
|
|
|
|
|
|
|
let declared_ret_ty = fn_sig.output();
|
|
|
|
|
2022-02-17 14:49:45 +00:00
|
|
|
let ret_ty =
|
|
|
|
fcx.register_infer_ok_obligations(fcx.infcx.replace_opaque_types_with_inference_vars(
|
|
|
|
declared_ret_ty,
|
|
|
|
body.value.hir_id,
|
2022-05-27 19:31:10 +00:00
|
|
|
decl.output.span(),
|
2022-02-17 14:49:45 +00:00
|
|
|
param_env,
|
|
|
|
));
|
2022-08-18 10:13:37 +08:00
|
|
|
// If we replaced declared_ret_ty with infer vars, then we must be inferring
|
2022-06-06 23:20:13 -07:00
|
|
|
// an opaque type, so set a flag so we can improve diagnostics.
|
|
|
|
fcx.return_type_has_opaque = ret_ty != declared_ret_ty;
|
|
|
|
|
2022-02-16 15:48:46 +00:00
|
|
|
fcx.ret_coercion = Some(RefCell::new(CoerceMany::new(ret_ty)));
|
2020-09-19 13:51:59 -07:00
|
|
|
|
|
|
|
let span = body.value.span;
|
|
|
|
|
|
|
|
fn_maybe_err(tcx, span, fn_sig.abi);
|
|
|
|
|
2020-11-17 15:49:40 -05:00
|
|
|
if fn_sig.abi == Abi::RustCall {
|
2020-11-17 14:42:29 -05:00
|
|
|
let expected_args = if let ImplicitSelfKind::None = decl.implicit_self { 1 } else { 2 };
|
|
|
|
|
|
|
|
let err = || {
|
|
|
|
let item = match tcx.hir().get(fn_id) {
|
|
|
|
Node::Item(hir::Item { kind: ItemKind::Fn(header, ..), .. }) => Some(header),
|
|
|
|
Node::ImplItem(hir::ImplItem {
|
|
|
|
kind: hir::ImplItemKind::Fn(header, ..), ..
|
|
|
|
}) => Some(header),
|
2020-12-03 15:49:20 -05:00
|
|
|
Node::TraitItem(hir::TraitItem {
|
2020-12-03 15:53:52 -05:00
|
|
|
kind: hir::TraitItemKind::Fn(header, ..),
|
|
|
|
..
|
2020-12-03 15:49:20 -05:00
|
|
|
}) => Some(header),
|
2020-11-17 14:42:29 -05:00
|
|
|
// Closures are RustCall, but they tuple their arguments, so shouldn't be checked
|
2022-06-11 21:25:25 +02:00
|
|
|
Node::Expr(hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => None,
|
2020-11-17 14:42:29 -05:00
|
|
|
node => bug!("Item being checked wasn't a function/closure: {:?}", node),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(header) = item {
|
2022-01-22 18:49:12 -06:00
|
|
|
tcx.sess.span_err(header.span, "functions with the \"rust-call\" ABI must take a single non-self argument that is a tuple");
|
2020-11-17 14:42:29 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if fn_sig.inputs().len() != expected_args {
|
|
|
|
err()
|
|
|
|
} else {
|
|
|
|
// FIXME(CraftSpider) Add a check on parameter expansion, so we don't just make the ICE happen later on
|
|
|
|
// This will probably require wide-scale changes to support a TupleKind obligation
|
|
|
|
// We can't resolve this without knowing the type of the param
|
|
|
|
if !matches!(fn_sig.inputs()[expected_args - 1].kind(), ty::Tuple(_) | ty::Param(_)) {
|
|
|
|
err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 13:51:59 -07:00
|
|
|
if body.generator_kind.is_some() && can_be_generator.is_some() {
|
|
|
|
let yield_ty = fcx
|
|
|
|
.next_ty_var(TypeVariableOrigin { kind: TypeVariableOriginKind::TypeInference, span });
|
|
|
|
fcx.require_type_is_sized(yield_ty, span, traits::SizedYieldType);
|
|
|
|
|
|
|
|
// Resume type defaults to `()` if the generator has no argument.
|
|
|
|
let resume_ty = fn_sig.inputs().get(0).copied().unwrap_or_else(|| tcx.mk_unit());
|
|
|
|
|
|
|
|
fcx.resume_yield_tys = Some((resume_ty, yield_ty));
|
|
|
|
}
|
|
|
|
|
2021-07-16 10:06:26 -03:00
|
|
|
GatherLocalsVisitor::new(&fcx).visit_body(body);
|
2020-09-19 13:51:59 -07:00
|
|
|
|
|
|
|
// C-variadic fns also have a `VaList` input that's not listed in `fn_sig`
|
|
|
|
// (as it's created inside the body itself, not passed in from outside).
|
|
|
|
let maybe_va_list = if fn_sig.c_variadic {
|
|
|
|
let span = body.params.last().unwrap().span;
|
|
|
|
let va_list_did = tcx.require_lang_item(LangItem::VaList, Some(span));
|
|
|
|
let region = fcx.next_region_var(RegionVariableOrigin::MiscVariable(span));
|
|
|
|
|
2022-05-08 15:12:56 -04:00
|
|
|
Some(tcx.bound_type_of(va_list_did).subst(tcx, &[region.into()]))
|
2020-09-19 13:51:59 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add formal parameters.
|
|
|
|
let inputs_hir = hir.fn_decl_by_hir_id(fn_id).map(|decl| &decl.inputs);
|
|
|
|
let inputs_fn = fn_sig.inputs().iter().copied();
|
|
|
|
for (idx, (param_ty, param)) in inputs_fn.chain(maybe_va_list).zip(body.params).enumerate() {
|
|
|
|
// Check the pattern.
|
|
|
|
let ty_span = try { inputs_hir?.get(idx)?.span };
|
|
|
|
fcx.check_pat_top(¶m.pat, param_ty, ty_span, false);
|
|
|
|
|
|
|
|
// Check that argument is Sized.
|
|
|
|
// The check for a non-trivial pattern is a hack to avoid duplicate warnings
|
|
|
|
// for simple cases like `fn foo(x: Trait)`,
|
|
|
|
// where we would error once on the parameter as a whole, and once on the binding `x`.
|
2020-10-16 17:46:59 -03:00
|
|
|
if param.pat.simple_ident().is_none() && !tcx.features().unsized_fn_params {
|
2020-09-19 13:51:59 -07:00
|
|
|
fcx.require_type_is_sized(param_ty, param.pat.span, traits::SizedArgumentType(ty_span));
|
|
|
|
}
|
|
|
|
|
|
|
|
fcx.write_ty(param.hir_id, param_ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
inherited.typeck_results.borrow_mut().liberated_fn_sigs_mut().insert(fn_id, fn_sig);
|
|
|
|
|
|
|
|
fcx.in_tail_expr = true;
|
|
|
|
if let ty::Dynamic(..) = declared_ret_ty.kind() {
|
|
|
|
// FIXME: We need to verify that the return type is `Sized` after the return expression has
|
|
|
|
// been evaluated so that we have types available for all the nodes being returned, but that
|
|
|
|
// requires the coerced evaluated type to be stored. Moving `check_return_expr` before this
|
|
|
|
// causes unsized errors caused by the `declared_ret_ty` to point at the return expression,
|
|
|
|
// while keeping the current ordering we will ignore the tail expression's type because we
|
|
|
|
// don't know it yet. We can't do `check_expr_kind` while keeping `check_return_expr`
|
|
|
|
// because we will trigger "unreachable expression" lints unconditionally.
|
|
|
|
// Because of all of this, we perform a crude check to know whether the simplest `!Sized`
|
|
|
|
// case that a newcomer might make, returning a bare trait, and in that case we populate
|
|
|
|
// the tail expression's type so that the suggestion will be correct, but ignore all other
|
|
|
|
// possible cases.
|
|
|
|
fcx.check_expr(&body.value);
|
|
|
|
fcx.require_type_is_sized(declared_ret_ty, decl.output.span(), traits::SizedReturnType);
|
|
|
|
} else {
|
|
|
|
fcx.require_type_is_sized(declared_ret_ty, decl.output.span(), traits::SizedReturnType);
|
2021-09-16 16:53:40 -05:00
|
|
|
fcx.check_return_expr(&body.value, false);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
fcx.in_tail_expr = false;
|
|
|
|
|
|
|
|
// We insert the deferred_generator_interiors entry after visiting the body.
|
|
|
|
// This ensures that all nested generators appear before the entry of this generator.
|
|
|
|
// resolve_generator_interiors relies on this property.
|
|
|
|
let gen_ty = if let (Some(_), Some(gen_kind)) = (can_be_generator, body.generator_kind) {
|
|
|
|
let interior = fcx
|
|
|
|
.next_ty_var(TypeVariableOrigin { kind: TypeVariableOriginKind::MiscVariable, span });
|
|
|
|
fcx.deferred_generator_interiors.borrow_mut().push((body.id(), interior, gen_kind));
|
|
|
|
|
|
|
|
let (resume_ty, yield_ty) = fcx.resume_yield_tys.unwrap();
|
|
|
|
Some(GeneratorTypes {
|
|
|
|
resume_ty,
|
|
|
|
yield_ty,
|
|
|
|
interior,
|
|
|
|
movability: can_be_generator.unwrap(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
// Finalize the return check by taking the LUB of the return types
|
|
|
|
// we saw and assigning it to the expected return type. This isn't
|
|
|
|
// really expected to fail, since the coercions would have failed
|
|
|
|
// earlier when trying to find a LUB.
|
|
|
|
let coercion = fcx.ret_coercion.take().unwrap().into_inner();
|
|
|
|
let mut actual_return_ty = coercion.complete(&fcx);
|
2020-11-23 07:42:06 -05:00
|
|
|
debug!("actual_return_ty = {:?}", actual_return_ty);
|
|
|
|
if let ty::Dynamic(..) = declared_ret_ty.kind() {
|
|
|
|
// We have special-cased the case where the function is declared
|
|
|
|
// `-> dyn Foo` and we don't actually relate it to the
|
|
|
|
// `fcx.ret_coercion`, so just substitute a type variable.
|
|
|
|
actual_return_ty =
|
|
|
|
fcx.next_ty_var(TypeVariableOrigin { kind: TypeVariableOriginKind::DynReturnFn, span });
|
|
|
|
debug!("actual_return_ty replaced with {:?}", actual_return_ty);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
2022-05-26 10:21:20 -07:00
|
|
|
|
|
|
|
// HACK(oli-obk, compiler-errors): We should be comparing this against
|
|
|
|
// `declared_ret_ty`, but then anything uninferred would be inferred to
|
|
|
|
// the opaque type itself. That again would cause writeback to assume
|
|
|
|
// we have a recursive call site and do the sadly stabilized fallback to `()`.
|
|
|
|
fcx.demand_suptype(span, ret_ty, actual_return_ty);
|
2020-09-19 13:51:59 -07:00
|
|
|
|
|
|
|
// Check that a function marked as `#[panic_handler]` has signature `fn(&PanicInfo) -> !`
|
2022-04-15 15:56:32 +09:00
|
|
|
if let Some(panic_impl_did) = tcx.lang_items().panic_impl()
|
|
|
|
&& panic_impl_did == hir.local_def_id(fn_id).to_def_id()
|
|
|
|
{
|
2022-05-12 22:29:04 +02:00
|
|
|
check_panic_info_fn(tcx, panic_impl_did.expect_local(), fn_sig, decl, declared_ret_ty);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that a function marked as `#[alloc_error_handler]` has signature `fn(Layout) -> !`
|
2022-04-15 15:56:32 +09:00
|
|
|
if let Some(alloc_error_handler_did) = tcx.lang_items().oom()
|
|
|
|
&& alloc_error_handler_did == hir.local_def_id(fn_id).to_def_id()
|
|
|
|
{
|
2022-05-12 22:29:04 +02:00
|
|
|
check_alloc_error_fn(tcx, alloc_error_handler_did.expect_local(), fn_sig, decl, declared_ret_ty);
|
|
|
|
}
|
2020-09-19 13:51:59 -07:00
|
|
|
|
2022-05-12 22:29:04 +02:00
|
|
|
(fcx, gen_ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_panic_info_fn(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
fn_id: LocalDefId,
|
|
|
|
fn_sig: ty::FnSig<'_>,
|
|
|
|
decl: &hir::FnDecl<'_>,
|
|
|
|
declared_ret_ty: Ty<'_>,
|
|
|
|
) {
|
|
|
|
let Some(panic_info_did) = tcx.lang_items().panic_info() else {
|
|
|
|
tcx.sess.err("language item required, but not found: `panic_info`");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
if *declared_ret_ty.kind() != ty::Never {
|
|
|
|
tcx.sess.span_err(decl.output.span(), "return type should be `!`");
|
|
|
|
}
|
|
|
|
|
|
|
|
let inputs = fn_sig.inputs();
|
|
|
|
if inputs.len() != 1 {
|
2022-07-04 17:23:24 +09:00
|
|
|
tcx.sess.span_err(tcx.def_span(fn_id), "function should have one argument");
|
2022-05-12 22:29:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let arg_is_panic_info = match *inputs[0].kind() {
|
|
|
|
ty::Ref(region, ty, mutbl) => match *ty.kind() {
|
|
|
|
ty::Adt(ref adt, _) => {
|
|
|
|
adt.did() == panic_info_did && mutbl == hir::Mutability::Not && !region.is_static()
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
2022-05-12 22:29:04 +02:00
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if !arg_is_panic_info {
|
|
|
|
tcx.sess.span_err(decl.inputs[0].span, "argument should be `&PanicInfo`");
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
|
2022-05-12 22:29:04 +02:00
|
|
|
let DefKind::Fn = tcx.def_kind(fn_id) else {
|
|
|
|
let span = tcx.def_span(fn_id);
|
|
|
|
tcx.sess.span_err(span, "should be a function");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let generic_counts = tcx.generics_of(fn_id).own_counts();
|
|
|
|
if generic_counts.types != 0 {
|
|
|
|
let span = tcx.def_span(fn_id);
|
|
|
|
tcx.sess.span_err(span, "should have no type parameters");
|
|
|
|
}
|
|
|
|
if generic_counts.consts != 0 {
|
|
|
|
let span = tcx.def_span(fn_id);
|
|
|
|
tcx.sess.span_err(span, "should have no const parameters");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_alloc_error_fn(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
fn_id: LocalDefId,
|
|
|
|
fn_sig: ty::FnSig<'_>,
|
|
|
|
decl: &hir::FnDecl<'_>,
|
|
|
|
declared_ret_ty: Ty<'_>,
|
|
|
|
) {
|
|
|
|
let Some(alloc_layout_did) = tcx.lang_items().alloc_layout() else {
|
|
|
|
tcx.sess.err("language item required, but not found: `alloc_layout`");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
if *declared_ret_ty.kind() != ty::Never {
|
|
|
|
tcx.sess.span_err(decl.output.span(), "return type should be `!`");
|
|
|
|
}
|
|
|
|
|
|
|
|
let inputs = fn_sig.inputs();
|
|
|
|
if inputs.len() != 1 {
|
2022-07-04 17:23:24 +09:00
|
|
|
tcx.sess.span_err(tcx.def_span(fn_id), "function should have one argument");
|
2022-05-12 22:29:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let arg_is_alloc_layout = match inputs[0].kind() {
|
|
|
|
ty::Adt(ref adt, _) => adt.did() == alloc_layout_did,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if !arg_is_alloc_layout {
|
|
|
|
tcx.sess.span_err(decl.inputs[0].span, "argument should be `Layout`");
|
|
|
|
}
|
|
|
|
|
|
|
|
let DefKind::Fn = tcx.def_kind(fn_id) else {
|
|
|
|
let span = tcx.def_span(fn_id);
|
|
|
|
tcx.sess.span_err(span, "`#[alloc_error_handler]` should be a function");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let generic_counts = tcx.generics_of(fn_id).own_counts();
|
|
|
|
if generic_counts.types != 0 {
|
|
|
|
let span = tcx.def_span(fn_id);
|
|
|
|
tcx.sess.span_err(span, "`#[alloc_error_handler]` function should have no type parameters");
|
|
|
|
}
|
|
|
|
if generic_counts.consts != 0 {
|
|
|
|
let span = tcx.def_span(fn_id);
|
|
|
|
tcx.sess
|
|
|
|
.span_err(span, "`#[alloc_error_handler]` function should have no const parameters");
|
|
|
|
}
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
|
2022-07-08 02:32:51 +00:00
|
|
|
fn check_struct(tcx: TyCtxt<'_>, def_id: LocalDefId) {
|
2020-09-19 13:51:59 -07:00
|
|
|
let def = tcx.adt_def(def_id);
|
2022-07-08 02:32:51 +00:00
|
|
|
let span = tcx.def_span(def_id);
|
2020-09-19 13:51:59 -07:00
|
|
|
def.destructor(tcx); // force the destructor to be evaluated
|
|
|
|
check_representable(tcx, span, def_id);
|
|
|
|
|
2022-03-05 07:28:41 +11:00
|
|
|
if def.repr().simd() {
|
2020-09-19 13:51:59 -07:00
|
|
|
check_simd(tcx, span, def_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
check_transparent(tcx, span, def);
|
|
|
|
check_packed(tcx, span, def);
|
|
|
|
}
|
|
|
|
|
2022-07-08 02:32:51 +00:00
|
|
|
fn check_union(tcx: TyCtxt<'_>, def_id: LocalDefId) {
|
2020-09-19 13:51:59 -07:00
|
|
|
let def = tcx.adt_def(def_id);
|
2022-07-08 02:32:51 +00:00
|
|
|
let span = tcx.def_span(def_id);
|
2020-09-19 13:51:59 -07:00
|
|
|
def.destructor(tcx); // force the destructor to be evaluated
|
|
|
|
check_representable(tcx, span, def_id);
|
|
|
|
check_transparent(tcx, span, def);
|
|
|
|
check_union_fields(tcx, span, def_id);
|
|
|
|
check_packed(tcx, span, def);
|
|
|
|
}
|
|
|
|
|
2020-10-04 22:24:14 +02:00
|
|
|
/// Check that the fields of the `union` do not need dropping.
|
2020-10-24 16:13:39 +02:00
|
|
|
fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> bool {
|
2020-09-19 13:51:59 -07:00
|
|
|
let item_type = tcx.type_of(item_def_id);
|
|
|
|
if let ty::Adt(def, substs) = item_type.kind() {
|
|
|
|
assert!(def.is_union());
|
2022-06-29 22:33:18 -04:00
|
|
|
|
|
|
|
fn allowed_union_field<'tcx>(
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
) -> bool {
|
|
|
|
// We don't just accept all !needs_drop fields, due to semver concerns.
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Ref(..) => true, // references never drop (even mutable refs, which are non-Copy and hence fail the later check)
|
|
|
|
ty::Tuple(tys) => {
|
|
|
|
// allow tuples of allowed types
|
|
|
|
tys.iter().all(|ty| allowed_union_field(ty, tcx, param_env, span))
|
|
|
|
}
|
|
|
|
ty::Array(elem, _len) => {
|
|
|
|
// Like `Copy`, we do *not* special-case length 0.
|
|
|
|
allowed_union_field(*elem, tcx, param_env, span)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Fallback case: allow `ManuallyDrop` and things that are `Copy`.
|
2022-06-30 10:17:49 -04:00
|
|
|
ty.ty_adt_def().is_some_and(|adt_def| adt_def.is_manually_drop())
|
2022-06-29 22:33:18 -04:00
|
|
|
|| ty.is_copy_modulo_regions(tcx.at(span), param_env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 13:51:59 -07:00
|
|
|
let param_env = tcx.param_env(item_def_id);
|
2022-06-29 22:33:18 -04:00
|
|
|
for field in &def.non_enum_variant().fields {
|
2020-09-19 13:51:59 -07:00
|
|
|
let field_ty = field.ty(tcx, substs);
|
2022-06-29 22:33:18 -04:00
|
|
|
|
|
|
|
if !allowed_union_field(field_ty, tcx, param_env, span) {
|
2021-11-15 14:47:36 +11:00
|
|
|
let (field_span, ty_span) = match tcx.hir().get_if_local(field.did) {
|
|
|
|
// We are currently checking the type this field came from, so it must be local.
|
|
|
|
Some(Node::Field(field)) => (field.span, field.ty.span),
|
|
|
|
_ => unreachable!("mir field has to correspond to hir field"),
|
|
|
|
};
|
2020-09-19 13:51:59 -07:00
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
field_span,
|
|
|
|
E0740,
|
2022-02-15 20:16:19 +01:00
|
|
|
"unions cannot contain fields that may need dropping"
|
|
|
|
)
|
|
|
|
.note(
|
|
|
|
"a type is guaranteed not to need dropping \
|
|
|
|
when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type",
|
2020-09-19 13:51:59 -07:00
|
|
|
)
|
2021-11-14 22:04:25 +11:00
|
|
|
.multipart_suggestion_verbose(
|
2022-02-15 20:16:19 +01:00
|
|
|
"when the type does not implement `Copy`, \
|
|
|
|
wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped",
|
2021-11-15 14:47:36 +11:00
|
|
|
vec![
|
2022-02-03 23:12:25 +01:00
|
|
|
(ty_span.shrink_to_lo(), "std::mem::ManuallyDrop<".into()),
|
2021-11-15 14:47:36 +11:00
|
|
|
(ty_span.shrink_to_hi(), ">".into()),
|
|
|
|
],
|
2021-11-14 22:04:25 +11:00
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
2020-09-19 13:51:59 -07:00
|
|
|
.emit();
|
|
|
|
return false;
|
2022-06-29 22:33:18 -04:00
|
|
|
} else if field_ty.needs_drop(tcx, param_env) {
|
|
|
|
// This should never happen. But we can get here e.g. in case of name resolution errors.
|
|
|
|
tcx.sess.delay_span_bug(span, "we should never accept maybe-dropping union fields");
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
span_bug!(span, "unions must be ty::Adt, but got {:?}", item_type.kind());
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2020-10-24 16:13:39 +02:00
|
|
|
/// Check that a `static` is inhabited.
|
2022-07-08 02:32:51 +00:00
|
|
|
fn check_static_inhabited<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {
|
2020-10-24 16:13:39 +02:00
|
|
|
// Make sure statics are inhabited.
|
|
|
|
// Other parts of the compiler assume that there are no uninhabited places. In principle it
|
2020-10-24 20:39:04 +02:00
|
|
|
// would be enough to check this for `extern` statics, as statics with an initializer will
|
2020-10-24 16:13:39 +02:00
|
|
|
// have UB during initialization if they are uninhabited, but there also seems to be no good
|
|
|
|
// reason to allow any statics to be uninhabited.
|
|
|
|
let ty = tcx.type_of(def_id);
|
2022-07-08 02:32:51 +00:00
|
|
|
let span = tcx.def_span(def_id);
|
2022-02-24 14:10:41 -05:00
|
|
|
let layout = match tcx.layout_of(ParamEnv::reveal_all().and(ty)) {
|
|
|
|
Ok(l) => l,
|
|
|
|
// Foreign statics that overflow their allowed size should emit an error
|
|
|
|
Err(LayoutError::SizeOverflow(_))
|
|
|
|
if {
|
|
|
|
let node = tcx.hir().get_by_def_id(def_id);
|
|
|
|
matches!(
|
|
|
|
node,
|
|
|
|
hir::Node::ForeignItem(hir::ForeignItem {
|
|
|
|
kind: hir::ForeignItemKind::Static(..),
|
|
|
|
..
|
|
|
|
})
|
|
|
|
)
|
|
|
|
} =>
|
|
|
|
{
|
|
|
|
tcx.sess
|
|
|
|
.struct_span_err(span, "extern static is too large for the current architecture")
|
|
|
|
.emit();
|
|
|
|
return;
|
|
|
|
}
|
2022-02-19 00:44:45 +01:00
|
|
|
// Generic statics are rejected, but we still reach this case.
|
2022-02-24 14:10:41 -05:00
|
|
|
Err(e) => {
|
|
|
|
tcx.sess.delay_span_bug(span, &e.to_string());
|
|
|
|
return;
|
|
|
|
}
|
2020-10-24 16:13:39 +02:00
|
|
|
};
|
|
|
|
if layout.abi.is_uninhabited() {
|
|
|
|
tcx.struct_span_lint_hir(
|
|
|
|
UNINHABITED_STATIC,
|
|
|
|
tcx.hir().local_def_id_to_hir_id(def_id),
|
|
|
|
span,
|
|
|
|
|lint| {
|
|
|
|
lint.build("static of uninhabited type")
|
|
|
|
.note("uninhabited statics cannot be initialized, and any access would be an immediate error")
|
|
|
|
.emit();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 13:51:59 -07:00
|
|
|
/// Checks that an opaque type does not contain cycles and does not use `Self` or `T::Foo`
|
|
|
|
/// projections that would result in "inheriting lifetimes".
|
|
|
|
pub(super) fn check_opaque<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
def_id: LocalDefId,
|
|
|
|
substs: SubstsRef<'tcx>,
|
|
|
|
origin: &hir::OpaqueTyOrigin,
|
|
|
|
) {
|
2022-07-08 02:32:51 +00:00
|
|
|
let span = tcx.def_span(def_id);
|
2020-09-19 13:51:59 -07:00
|
|
|
check_opaque_for_inheriting_lifetimes(tcx, def_id, span);
|
2020-09-07 10:01:45 +01:00
|
|
|
if tcx.type_of(def_id).references_error() {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-28 16:46:02 +01:00
|
|
|
if check_opaque_for_cycles(tcx, def_id, substs, span, origin).is_err() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
check_opaque_meets_bounds(tcx, def_id, substs, span, origin);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that an opaque type does not use `Self` or `T::Foo` projections that would result
|
|
|
|
/// in "inheriting lifetimes".
|
2021-01-24 00:00:00 +00:00
|
|
|
#[instrument(level = "debug", skip(tcx, span))]
|
2021-12-13 21:45:08 -04:00
|
|
|
pub(super) fn check_opaque_for_inheriting_lifetimes<'tcx>(
|
2020-09-19 13:51:59 -07:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
def_id: LocalDefId,
|
|
|
|
span: Span,
|
|
|
|
) {
|
2021-10-20 22:38:10 +02:00
|
|
|
let item = tcx.hir().expect_item(def_id);
|
2020-12-06 21:31:42 +01:00
|
|
|
debug!(?item, ?span);
|
2020-09-19 13:51:59 -07:00
|
|
|
|
2020-12-06 21:31:42 +01:00
|
|
|
struct FoundParentLifetime;
|
2022-01-12 03:19:52 +00:00
|
|
|
struct FindParentLifetimeVisitor<'tcx>(&'tcx ty::Generics);
|
2022-06-17 13:15:00 +01:00
|
|
|
impl<'tcx> ty::visit::TypeVisitor<'tcx> for FindParentLifetimeVisitor<'tcx> {
|
2020-12-06 21:31:42 +01:00
|
|
|
type BreakTy = FoundParentLifetime;
|
2020-09-19 13:51:59 -07:00
|
|
|
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2020-12-06 21:31:42 +01:00
|
|
|
debug!("FindParentLifetimeVisitor: r={:?}", r);
|
2022-01-28 11:25:15 +11:00
|
|
|
if let ty::ReEarlyBound(ty::EarlyBoundRegion { index, .. }) = *r {
|
|
|
|
if index < self.0.parent_count as u32 {
|
2020-12-06 21:31:42 +01:00
|
|
|
return ControlFlow::Break(FoundParentLifetime);
|
2020-10-21 14:26:34 +02:00
|
|
|
} else {
|
|
|
|
return ControlFlow::CONTINUE;
|
|
|
|
}
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
r.super_visit_with(self)
|
|
|
|
}
|
|
|
|
|
2022-02-02 14:24:45 +11:00
|
|
|
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2022-06-10 11:18:06 +10:00
|
|
|
if let ty::ConstKind::Unevaluated(..) = c.kind() {
|
2020-12-06 21:31:42 +01:00
|
|
|
// FIXME(#72219) We currently don't detect lifetimes within substs
|
2020-09-19 13:51:59 -07:00
|
|
|
// which would violate this check. Even though the particular substitution is not used
|
|
|
|
// within the const, this should still be fixed.
|
2020-10-21 14:26:34 +02:00
|
|
|
return ControlFlow::CONTINUE;
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
c.super_visit_with(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-06 21:31:42 +01:00
|
|
|
struct ProhibitOpaqueVisitor<'tcx> {
|
2021-03-13 16:05:15 +01:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-12-06 21:31:42 +01:00
|
|
|
opaque_identity_ty: Ty<'tcx>,
|
|
|
|
generics: &'tcx ty::Generics,
|
2021-02-13 14:45:53 +08:00
|
|
|
selftys: Vec<(Span, Option<String>)>,
|
2020-12-06 21:31:42 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 13:15:00 +01:00
|
|
|
impl<'tcx> ty::visit::TypeVisitor<'tcx> for ProhibitOpaqueVisitor<'tcx> {
|
2020-12-06 21:31:42 +01:00
|
|
|
type BreakTy = Ty<'tcx>;
|
|
|
|
|
|
|
|
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
|
|
|
debug!("check_opaque_for_inheriting_lifetimes: (visit_ty) t={:?}", t);
|
|
|
|
if t == self.opaque_identity_ty {
|
|
|
|
ControlFlow::CONTINUE
|
|
|
|
} else {
|
2022-01-12 03:19:52 +00:00
|
|
|
t.super_visit_with(&mut FindParentLifetimeVisitor(self.generics))
|
2020-12-06 21:31:42 +01:00
|
|
|
.map_break(|FoundParentLifetime| t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 21:45:08 -04:00
|
|
|
impl<'tcx> Visitor<'tcx> for ProhibitOpaqueVisitor<'tcx> {
|
2021-11-03 18:03:12 -05:00
|
|
|
type NestedFilter = nested_filter::OnlyBodies;
|
2021-02-13 14:45:53 +08:00
|
|
|
|
2021-11-03 18:03:12 -05:00
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
|
|
|
self.tcx.hir()
|
2021-02-13 14:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
|
|
|
|
match arg.kind {
|
|
|
|
hir::TyKind::Path(hir::QPath::Resolved(None, path)) => match &path.segments {
|
2022-08-30 15:10:28 +10:00
|
|
|
[PathSegment { res: Res::SelfTy { trait_: _, alias_to: impl_ref }, .. }] => {
|
2021-02-13 14:45:53 +08:00
|
|
|
let impl_ty_name =
|
|
|
|
impl_ref.map(|(def_id, _)| self.tcx.def_path_str(def_id));
|
|
|
|
self.selftys.push((path.span, impl_ty_name));
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
hir::intravisit::walk_ty(self, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:00:04 +00:00
|
|
|
if let ItemKind::OpaqueTy(hir::OpaqueTy {
|
|
|
|
origin: hir::OpaqueTyOrigin::AsyncFn(..) | hir::OpaqueTyOrigin::FnReturn(..),
|
|
|
|
..
|
|
|
|
}) = item.kind
|
2020-09-19 13:51:59 -07:00
|
|
|
{
|
|
|
|
let mut visitor = ProhibitOpaqueVisitor {
|
|
|
|
opaque_identity_ty: tcx.mk_opaque(
|
|
|
|
def_id.to_def_id(),
|
|
|
|
InternalSubsts::identity_for_item(tcx, def_id.to_def_id()),
|
|
|
|
),
|
|
|
|
generics: tcx.generics_of(def_id),
|
2021-02-13 14:45:53 +08:00
|
|
|
tcx,
|
|
|
|
selftys: vec![],
|
2020-09-19 13:51:59 -07:00
|
|
|
};
|
|
|
|
let prohibit_opaque = tcx
|
2020-06-27 21:36:35 +01:00
|
|
|
.explicit_item_bounds(def_id)
|
2020-09-19 13:51:59 -07:00
|
|
|
.iter()
|
2020-11-05 19:17:30 +01:00
|
|
|
.try_for_each(|(predicate, _)| predicate.visit_with(&mut visitor));
|
2020-09-19 13:51:59 -07:00
|
|
|
debug!(
|
2021-02-13 14:45:53 +08:00
|
|
|
"check_opaque_for_inheriting_lifetimes: prohibit_opaque={:?}, visitor.opaque_identity_ty={:?}, visitor.generics={:?}",
|
|
|
|
prohibit_opaque, visitor.opaque_identity_ty, visitor.generics
|
2020-09-19 13:51:59 -07:00
|
|
|
);
|
|
|
|
|
2020-11-05 19:17:30 +01:00
|
|
|
if let Some(ty) = prohibit_opaque.break_value() {
|
2021-02-12 16:50:45 +08:00
|
|
|
visitor.visit_item(&item);
|
2022-02-17 16:00:04 +00:00
|
|
|
let is_async = match item.kind {
|
|
|
|
ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) => {
|
|
|
|
matches!(origin, hir::OpaqueTyOrigin::AsyncFn(..))
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2020-09-19 13:51:59 -07:00
|
|
|
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
span,
|
|
|
|
E0760,
|
2022-02-17 16:00:04 +00:00
|
|
|
"`{}` return type cannot contain a projection or `Self` that references lifetimes from \
|
2020-06-27 21:36:35 +01:00
|
|
|
a parent scope",
|
2022-02-17 16:00:04 +00:00
|
|
|
if is_async { "async fn" } else { "impl Trait" },
|
2020-09-19 13:51:59 -07:00
|
|
|
);
|
|
|
|
|
2021-02-13 14:45:53 +08:00
|
|
|
for (span, name) in visitor.selftys {
|
2021-02-12 16:50:45 +08:00
|
|
|
err.span_suggestion(
|
|
|
|
span,
|
|
|
|
"consider spelling out the type instead",
|
2021-02-13 14:45:53 +08:00
|
|
|
name.unwrap_or_else(|| format!("{:?}", ty)),
|
2021-02-12 16:50:45 +08:00
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that an opaque type does not contain cycles.
|
|
|
|
pub(super) fn check_opaque_for_cycles<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
def_id: LocalDefId,
|
|
|
|
substs: SubstsRef<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
origin: &hir::OpaqueTyOrigin,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
2021-07-14 12:31:58 -03:00
|
|
|
if tcx.try_expand_impl_trait_type(def_id.to_def_id(), substs).is_err() {
|
2022-01-22 18:49:12 -06:00
|
|
|
let reported = match origin {
|
2021-11-30 19:11:35 +01:00
|
|
|
hir::OpaqueTyOrigin::AsyncFn(..) => async_opaque_type_cycle_error(tcx, span),
|
2020-09-19 13:51:59 -07:00
|
|
|
_ => opaque_type_cycle_error(tcx, def_id, span),
|
2022-01-22 18:49:12 -06:00
|
|
|
};
|
|
|
|
Err(reported)
|
2020-06-28 16:46:02 +01:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check that the concrete type behind `impl Trait` actually implements `Trait`.
|
2020-07-02 21:45:28 +01:00
|
|
|
///
|
|
|
|
/// This is mostly checked at the places that specify the opaque type, but we
|
|
|
|
/// check those cases in the `param_env` of that function, which may have
|
|
|
|
/// bounds not on this opaque type:
|
|
|
|
///
|
|
|
|
/// type X<T> = impl Clone
|
|
|
|
/// fn f<T: Clone>(t: T) -> X<T> {
|
|
|
|
/// t
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// Without this check the above code is incorrectly accepted: we would ICE if
|
|
|
|
/// some tried, for example, to clone an `Option<X<&mut ()>>`.
|
2021-12-01 16:34:40 +00:00
|
|
|
#[instrument(level = "debug", skip(tcx))]
|
2020-06-28 16:46:02 +01:00
|
|
|
fn check_opaque_meets_bounds<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
def_id: LocalDefId,
|
|
|
|
substs: SubstsRef<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
origin: &hir::OpaqueTyOrigin,
|
|
|
|
) {
|
2022-05-08 15:12:56 -04:00
|
|
|
let hidden_type = tcx.bound_type_of(def_id.to_def_id()).subst(tcx, substs);
|
2022-02-14 16:10:22 +00:00
|
|
|
|
2020-06-28 16:46:02 +01:00
|
|
|
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
|
2021-12-01 16:34:40 +00:00
|
|
|
let defining_use_anchor = match *origin {
|
|
|
|
hir::OpaqueTyOrigin::FnReturn(did) | hir::OpaqueTyOrigin::AsyncFn(did) => did,
|
|
|
|
hir::OpaqueTyOrigin::TyAlias => def_id,
|
|
|
|
};
|
|
|
|
let param_env = tcx.param_env(defining_use_anchor);
|
2020-06-28 16:46:02 +01:00
|
|
|
|
2022-07-02 16:37:49 +03:00
|
|
|
tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bind(defining_use_anchor)).enter(
|
|
|
|
move |infcx| {
|
|
|
|
let ocx = ObligationCtxt::new(&infcx);
|
|
|
|
let opaque_ty = tcx.mk_opaque(def_id.to_def_id(), substs);
|
2020-06-28 16:46:02 +01:00
|
|
|
|
2022-07-02 16:37:49 +03:00
|
|
|
let misc_cause = traits::ObligationCause::misc(span, hir_id);
|
2020-06-28 16:46:02 +01:00
|
|
|
|
2022-07-02 16:37:49 +03:00
|
|
|
match infcx.at(&misc_cause, param_env).eq(opaque_ty, hidden_type) {
|
|
|
|
Ok(infer_ok) => ocx.register_infer_ok_obligations(infer_ok),
|
|
|
|
Err(ty_err) => {
|
|
|
|
tcx.sess.delay_span_bug(
|
|
|
|
span,
|
|
|
|
&format!("could not unify `{hidden_type}` with revealed type:\n{ty_err}"),
|
|
|
|
);
|
|
|
|
}
|
2022-02-11 07:18:06 +00:00
|
|
|
}
|
2020-06-28 16:46:02 +01:00
|
|
|
|
2022-07-02 16:37:49 +03:00
|
|
|
// Additionally require the hidden type to be well-formed with only the generics of the opaque type.
|
|
|
|
// Defining use functions may have more bounds than the opaque type, which is ok, as long as the
|
|
|
|
// hidden type is well formed even without those bounds.
|
|
|
|
let predicate = ty::Binder::dummy(ty::PredicateKind::WellFormed(hidden_type.into()))
|
|
|
|
.to_predicate(tcx);
|
|
|
|
ocx.register_obligation(Obligation::new(misc_cause, param_env, predicate));
|
|
|
|
|
|
|
|
// Check that all obligations are satisfied by the implementation's
|
|
|
|
// version.
|
|
|
|
let errors = ocx.select_all_or_error();
|
|
|
|
if !errors.is_empty() {
|
|
|
|
infcx.report_fulfillment_errors(&errors, None, false);
|
2021-12-01 16:34:40 +00:00
|
|
|
}
|
2022-07-02 16:37:49 +03:00
|
|
|
match origin {
|
|
|
|
// Checked when type checking the function containing them.
|
|
|
|
hir::OpaqueTyOrigin::FnReturn(..) | hir::OpaqueTyOrigin::AsyncFn(..) => {}
|
|
|
|
// Can have different predicates to their defining use
|
|
|
|
hir::OpaqueTyOrigin::TyAlias => {
|
|
|
|
let outlives_environment = OutlivesEnvironment::new(param_env);
|
|
|
|
infcx.check_region_obligations_and_report_errors(
|
|
|
|
defining_use_anchor,
|
|
|
|
&outlives_environment,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Clean up after ourselves
|
|
|
|
let _ = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
|
|
|
|
},
|
|
|
|
);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
|
2022-06-12 00:47:21 +02:00
|
|
|
fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, id: hir::ItemId) {
|
2020-09-19 13:51:59 -07:00
|
|
|
debug!(
|
2021-01-30 17:47:51 +01:00
|
|
|
"check_item_type(it.def_id={:?}, it.name={})",
|
2022-04-29 13:09:03 -04:00
|
|
|
id.def_id,
|
|
|
|
tcx.def_path_str(id.def_id.to_def_id())
|
2020-09-19 13:51:59 -07:00
|
|
|
);
|
|
|
|
let _indenter = indenter();
|
2022-04-29 13:11:22 -04:00
|
|
|
match tcx.def_kind(id.def_id) {
|
2022-04-29 13:09:03 -04:00
|
|
|
DefKind::Static(..) => {
|
2022-09-20 14:11:23 +09:00
|
|
|
tcx.ensure().typeck(id.def_id.def_id);
|
|
|
|
maybe_check_static_with_link_section(tcx, id.def_id.def_id);
|
|
|
|
check_static_inhabited(tcx, id.def_id.def_id);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
2022-04-29 13:09:03 -04:00
|
|
|
DefKind::Const => {
|
2022-09-20 14:11:23 +09:00
|
|
|
tcx.ensure().typeck(id.def_id.def_id);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
2022-04-29 13:09:03 -04:00
|
|
|
DefKind::Enum => {
|
|
|
|
let item = tcx.hir().item(id);
|
|
|
|
let hir::ItemKind::Enum(ref enum_definition, _) = item.kind else {
|
|
|
|
return;
|
|
|
|
};
|
2022-09-20 14:11:23 +09:00
|
|
|
check_enum(tcx, &enum_definition.variants, item.def_id.def_id);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
2022-04-29 13:09:03 -04:00
|
|
|
DefKind::Fn => {} // entirely within check_item_body
|
|
|
|
DefKind::Impl => {
|
|
|
|
let it = tcx.hir().item(id);
|
|
|
|
let hir::ItemKind::Impl(ref impl_) = it.kind else {
|
|
|
|
return;
|
|
|
|
};
|
2021-01-30 17:47:51 +01:00
|
|
|
debug!("ItemKind::Impl {} with id {:?}", it.ident, it.def_id);
|
|
|
|
if let Some(impl_trait_ref) = tcx.impl_trait_ref(it.def_id) {
|
2020-11-22 17:46:21 -05:00
|
|
|
check_impl_items_against_trait(
|
|
|
|
tcx,
|
|
|
|
it.span,
|
2022-09-20 14:11:23 +09:00
|
|
|
it.def_id.def_id,
|
2020-11-22 17:46:21 -05:00
|
|
|
impl_trait_ref,
|
|
|
|
&impl_.items,
|
|
|
|
);
|
2022-03-06 19:51:30 -08:00
|
|
|
check_on_unimplemented(tcx, it);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
}
|
2022-04-29 13:09:03 -04:00
|
|
|
DefKind::Trait => {
|
|
|
|
let it = tcx.hir().item(id);
|
|
|
|
let hir::ItemKind::Trait(_, _, _, _, ref items) = it.kind else {
|
|
|
|
return;
|
|
|
|
};
|
2022-03-06 19:51:30 -08:00
|
|
|
check_on_unimplemented(tcx, it);
|
2020-09-19 13:51:59 -07:00
|
|
|
|
|
|
|
for item in items.iter() {
|
|
|
|
let item = tcx.hir().trait_item(item.id);
|
2020-06-27 21:36:35 +01:00
|
|
|
match item.kind {
|
|
|
|
hir::TraitItemKind::Fn(ref sig, _) => {
|
|
|
|
let abi = sig.header.abi;
|
|
|
|
fn_maybe_err(tcx, item.ident.span, abi);
|
|
|
|
}
|
2021-12-11 02:20:41 +00:00
|
|
|
hir::TraitItemKind::Type(.., Some(default)) => {
|
2021-01-30 20:46:50 +01:00
|
|
|
let assoc_item = tcx.associated_item(item.def_id);
|
2020-06-27 21:36:35 +01:00
|
|
|
let trait_substs =
|
2021-01-30 17:47:51 +01:00
|
|
|
InternalSubsts::identity_for_item(tcx, it.def_id.to_def_id());
|
2022-01-23 12:34:26 -06:00
|
|
|
let _: Result<_, rustc_errors::ErrorGuaranteed> = check_type_bounds(
|
2020-06-27 21:36:35 +01:00
|
|
|
tcx,
|
|
|
|
assoc_item,
|
|
|
|
assoc_item,
|
2021-12-11 02:20:41 +00:00
|
|
|
default.span,
|
2021-01-30 17:47:51 +01:00
|
|
|
ty::TraitRef { def_id: it.def_id.to_def_id(), substs: trait_substs },
|
2020-06-27 21:36:35 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {}
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-29 13:09:03 -04:00
|
|
|
DefKind::Struct => {
|
2022-09-20 14:11:23 +09:00
|
|
|
check_struct(tcx, id.def_id.def_id);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
2022-04-29 13:09:03 -04:00
|
|
|
DefKind::Union => {
|
2022-09-20 14:11:23 +09:00
|
|
|
check_union(tcx, id.def_id.def_id);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
2022-04-29 13:09:03 -04:00
|
|
|
DefKind::OpaqueTy => {
|
|
|
|
let item = tcx.hir().item(id);
|
|
|
|
let hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) = item.kind else {
|
|
|
|
return;
|
|
|
|
};
|
2020-09-19 13:51:59 -07:00
|
|
|
// HACK(jynelson): trying to infer the type of `impl trait` breaks documenting
|
|
|
|
// `async-std` (and `pub async fn` in general).
|
|
|
|
// Since rustdoc doesn't care about the concrete type behind `impl Trait`, just don't look at it!
|
|
|
|
// See https://github.com/rust-lang/rust/issues/75100
|
|
|
|
if !tcx.sess.opts.actually_rustdoc {
|
2022-04-29 13:09:03 -04:00
|
|
|
let substs = InternalSubsts::identity_for_item(tcx, item.def_id.to_def_id());
|
2022-09-20 14:11:23 +09:00
|
|
|
check_opaque(tcx, item.def_id.def_id, substs, &origin);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
}
|
2022-04-29 13:09:03 -04:00
|
|
|
DefKind::TyAlias => {
|
|
|
|
let pty_ty = tcx.type_of(id.def_id);
|
|
|
|
let generics = tcx.generics_of(id.def_id);
|
2020-09-19 13:51:59 -07:00
|
|
|
check_type_params_are_used(tcx, &generics, pty_ty);
|
|
|
|
}
|
2022-04-29 13:09:03 -04:00
|
|
|
DefKind::ForeignMod => {
|
|
|
|
let it = tcx.hir().item(id);
|
|
|
|
let hir::ItemKind::ForeignMod { abi, items } = it.kind else {
|
|
|
|
return;
|
|
|
|
};
|
2021-06-11 14:22:13 +03:00
|
|
|
check_abi(tcx, it.hir_id(), it.span, abi);
|
2020-09-19 13:51:59 -07:00
|
|
|
|
2020-11-11 22:40:09 +01:00
|
|
|
if abi == Abi::RustIntrinsic {
|
|
|
|
for item in items {
|
2020-11-11 21:57:54 +01:00
|
|
|
let item = tcx.hir().foreign_item(item.id);
|
2020-09-19 13:51:59 -07:00
|
|
|
intrinsic::check_intrinsic_type(tcx, item);
|
|
|
|
}
|
2020-11-11 22:40:09 +01:00
|
|
|
} else if abi == Abi::PlatformIntrinsic {
|
|
|
|
for item in items {
|
2020-11-11 21:57:54 +01:00
|
|
|
let item = tcx.hir().foreign_item(item.id);
|
2020-09-19 13:51:59 -07:00
|
|
|
intrinsic::check_platform_intrinsic_type(tcx, item);
|
|
|
|
}
|
|
|
|
} else {
|
2020-11-11 22:40:09 +01:00
|
|
|
for item in items {
|
2022-09-20 14:11:23 +09:00
|
|
|
let def_id = item.id.def_id.def_id;
|
2020-10-24 16:13:39 +02:00
|
|
|
let generics = tcx.generics_of(def_id);
|
2020-09-19 13:51:59 -07:00
|
|
|
let own_counts = generics.own_counts();
|
|
|
|
if generics.params.len() - own_counts.lifetimes != 0 {
|
|
|
|
let (kinds, kinds_pl, egs) = match (own_counts.types, own_counts.consts) {
|
|
|
|
(_, 0) => ("type", "types", Some("u32")),
|
|
|
|
// We don't specify an example value, because we can't generate
|
|
|
|
// a valid value for any type.
|
|
|
|
(0, _) => ("const", "consts", None),
|
|
|
|
_ => ("type or const", "types or consts", None),
|
|
|
|
};
|
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
item.span,
|
|
|
|
E0044,
|
2022-04-15 15:56:32 +09:00
|
|
|
"foreign items may not have {kinds} parameters",
|
2020-09-19 13:51:59 -07:00
|
|
|
)
|
2022-04-15 15:56:32 +09:00
|
|
|
.span_label(item.span, &format!("can't have {kinds} parameters"))
|
2020-09-19 13:51:59 -07:00
|
|
|
.help(
|
|
|
|
// FIXME: once we start storing spans for type arguments, turn this
|
|
|
|
// into a suggestion.
|
|
|
|
&format!(
|
|
|
|
"replace the {} parameters with concrete {}{}",
|
|
|
|
kinds,
|
|
|
|
kinds_pl,
|
|
|
|
egs.map(|egs| format!(" like `{}`", egs)).unwrap_or_default(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
2020-11-11 21:57:54 +01:00
|
|
|
let item = tcx.hir().foreign_item(item.id);
|
2020-10-24 16:13:39 +02:00
|
|
|
match item.kind {
|
|
|
|
hir::ForeignItemKind::Fn(ref fn_decl, _, _) => {
|
2020-11-11 22:40:09 +01:00
|
|
|
require_c_abi_if_c_variadic(tcx, fn_decl, abi, item.span);
|
2020-10-24 16:13:39 +02:00
|
|
|
}
|
|
|
|
hir::ForeignItemKind::Static(..) => {
|
2022-07-08 02:32:51 +00:00
|
|
|
check_static_inhabited(tcx, def_id);
|
2020-10-24 16:13:39 +02:00
|
|
|
}
|
|
|
|
_ => {}
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-14 12:07:36 +00:00
|
|
|
DefKind::GlobalAsm => {
|
|
|
|
let it = tcx.hir().item(id);
|
|
|
|
let hir::ItemKind::GlobalAsm(asm) = it.kind else { span_bug!(it.span, "DefKind::GlobalAsm but got {:#?}", it) };
|
2022-07-07 16:14:27 +02:00
|
|
|
InlineAsmCtxt::new_global_asm(tcx).check_asm(asm, id.hir_id());
|
2022-04-14 12:07:36 +00:00
|
|
|
}
|
2022-04-29 13:09:03 -04:00
|
|
|
_ => {}
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 19:51:30 -08:00
|
|
|
pub(super) fn check_on_unimplemented(tcx: TyCtxt<'_>, item: &hir::Item<'_>) {
|
2020-09-19 13:51:59 -07:00
|
|
|
// an error would be reported if this fails.
|
2022-03-06 19:51:30 -08:00
|
|
|
let _ = traits::OnUnimplementedDirective::of_item(tcx, item.def_id.to_def_id());
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn check_specialization_validity<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
trait_def: &ty::TraitDef,
|
|
|
|
trait_item: &ty::AssocItem,
|
|
|
|
impl_id: DefId,
|
2021-11-18 21:35:42 +00:00
|
|
|
impl_item: &hir::ImplItemRef,
|
2020-09-19 13:51:59 -07:00
|
|
|
) {
|
2022-02-19 00:44:45 +01:00
|
|
|
let Ok(ancestors) = trait_def.ancestors(tcx, impl_id) else { return };
|
2021-01-22 18:15:55 +01:00
|
|
|
let mut ancestor_impls = ancestors.skip(1).filter_map(|parent| {
|
|
|
|
if parent.is_from_trait() {
|
|
|
|
None
|
|
|
|
} else {
|
2021-11-18 21:35:42 +00:00
|
|
|
Some((parent, parent.item(tcx, trait_item.def_id)))
|
2021-01-22 18:15:55 +01:00
|
|
|
}
|
|
|
|
});
|
2020-09-19 13:51:59 -07:00
|
|
|
|
|
|
|
let opt_result = ancestor_impls.find_map(|(parent_impl, parent_item)| {
|
|
|
|
match parent_item {
|
|
|
|
// Parent impl exists, and contains the parent item we're trying to specialize, but
|
|
|
|
// doesn't mark it `default`.
|
|
|
|
Some(parent_item) if traits::impl_item_is_final(tcx, &parent_item) => {
|
|
|
|
Some(Err(parent_impl.def_id()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent impl contains item and makes it specializable.
|
|
|
|
Some(_) => Some(Ok(())),
|
|
|
|
|
|
|
|
// Parent impl doesn't mention the item. This means it's inherited from the
|
|
|
|
// grandparent. In that case, if parent is a `default impl`, inherited items use the
|
|
|
|
// "defaultness" from the grandparent, else they are final.
|
|
|
|
None => {
|
|
|
|
if tcx.impl_defaultness(parent_impl.def_id()).is_default() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Err(parent_impl.def_id()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// If `opt_result` is `None`, we have only encountered `default impl`s that don't contain the
|
|
|
|
// item. This is allowed, the item isn't actually getting specialized here.
|
|
|
|
let result = opt_result.unwrap_or(Ok(()));
|
|
|
|
|
|
|
|
if let Err(parent_impl) = result {
|
|
|
|
report_forbidden_specialization(tcx, impl_item, parent_impl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 21:35:42 +00:00
|
|
|
fn check_impl_items_against_trait<'tcx>(
|
2020-09-19 13:51:59 -07:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
full_impl_span: Span,
|
|
|
|
impl_id: LocalDefId,
|
|
|
|
impl_trait_ref: ty::TraitRef<'tcx>,
|
2021-07-15 22:19:39 +02:00
|
|
|
impl_item_refs: &[hir::ImplItemRef],
|
2020-09-19 13:51:59 -07:00
|
|
|
) {
|
|
|
|
// If the trait reference itself is erroneous (so the compilation is going
|
|
|
|
// to fail), skip checking the items here -- the `impl_item` table in `tcx`
|
|
|
|
// isn't populated for such impls.
|
|
|
|
if impl_trait_ref.references_error() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Negative impls are not expected to have any items
|
|
|
|
match tcx.impl_polarity(impl_id) {
|
|
|
|
ty::ImplPolarity::Reservation | ty::ImplPolarity::Positive => {}
|
|
|
|
ty::ImplPolarity::Negative => {
|
|
|
|
if let [first_item_ref, ..] = impl_item_refs {
|
|
|
|
let first_item_span = tcx.hir().impl_item(first_item_ref.id).span;
|
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
first_item_span,
|
|
|
|
E0749,
|
|
|
|
"negative impls cannot have any items"
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let trait_def = tcx.trait_def(impl_trait_ref.def_id);
|
2021-01-22 18:15:55 +01:00
|
|
|
|
2021-11-18 21:35:42 +00:00
|
|
|
for impl_item in impl_item_refs {
|
|
|
|
let ty_impl_item = tcx.associated_item(impl_item.id.def_id);
|
|
|
|
let ty_trait_item = if let Some(trait_item_id) = ty_impl_item.trait_item_def_id {
|
|
|
|
tcx.associated_item(trait_item_id)
|
2021-01-22 18:15:55 +01:00
|
|
|
} else {
|
2021-11-18 21:35:42 +00:00
|
|
|
// Checked in `associated_item`.
|
|
|
|
tcx.sess.delay_span_bug(impl_item.span, "missing associated item in trait");
|
2021-01-22 18:15:55 +01:00
|
|
|
continue;
|
|
|
|
};
|
2022-01-07 14:16:56 -08:00
|
|
|
let impl_item_full = tcx.hir().impl_item(impl_item.id);
|
|
|
|
match impl_item_full.kind {
|
|
|
|
hir::ImplItemKind::Const(..) => {
|
2021-11-18 21:35:42 +00:00
|
|
|
// Find associated const definition.
|
|
|
|
compare_const_impl(
|
|
|
|
tcx,
|
|
|
|
&ty_impl_item,
|
|
|
|
impl_item.span,
|
|
|
|
&ty_trait_item,
|
|
|
|
impl_trait_ref,
|
|
|
|
);
|
|
|
|
}
|
2022-01-07 14:16:56 -08:00
|
|
|
hir::ImplItemKind::Fn(..) => {
|
2021-11-18 21:35:42 +00:00
|
|
|
let opt_trait_span = tcx.hir().span_if_local(ty_trait_item.def_id);
|
|
|
|
compare_impl_method(
|
|
|
|
tcx,
|
|
|
|
&ty_impl_item,
|
|
|
|
&ty_trait_item,
|
|
|
|
impl_trait_ref,
|
|
|
|
opt_trait_span,
|
|
|
|
);
|
|
|
|
}
|
2022-01-07 14:16:56 -08:00
|
|
|
hir::ImplItemKind::TyAlias(impl_ty) => {
|
2021-11-18 21:35:42 +00:00
|
|
|
let opt_trait_span = tcx.hir().span_if_local(ty_trait_item.def_id);
|
|
|
|
compare_ty_impl(
|
|
|
|
tcx,
|
|
|
|
&ty_impl_item,
|
2022-01-07 14:16:56 -08:00
|
|
|
impl_ty.span,
|
2021-11-18 21:35:42 +00:00
|
|
|
&ty_trait_item,
|
|
|
|
impl_trait_ref,
|
|
|
|
opt_trait_span,
|
|
|
|
);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
}
|
2021-11-18 21:35:42 +00:00
|
|
|
|
|
|
|
check_specialization_validity(
|
|
|
|
tcx,
|
|
|
|
trait_def,
|
|
|
|
&ty_trait_item,
|
|
|
|
impl_id.to_def_id(),
|
|
|
|
impl_item,
|
|
|
|
);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Ok(ancestors) = trait_def.ancestors(tcx, impl_id.to_def_id()) {
|
2021-01-22 08:22:15 +01:00
|
|
|
// Check for missing items from trait
|
|
|
|
let mut missing_items = Vec::new();
|
2021-12-21 18:40:50 +03:00
|
|
|
|
2022-01-14 16:52:30 +03:00
|
|
|
let mut must_implement_one_of: Option<&[Ident]> =
|
|
|
|
trait_def.must_implement_one_of.as_deref();
|
2021-12-21 18:40:50 +03:00
|
|
|
|
2021-11-18 21:35:42 +00:00
|
|
|
for &trait_item_id in tcx.associated_item_def_ids(impl_trait_ref.def_id) {
|
2020-09-19 13:51:59 -07:00
|
|
|
let is_implemented = ancestors
|
2021-11-18 21:35:42 +00:00
|
|
|
.leaf_def(tcx, trait_item_id)
|
2022-03-12 19:36:11 +01:00
|
|
|
.map_or(false, |node_item| node_item.item.defaultness(tcx).has_value());
|
2020-09-19 13:51:59 -07:00
|
|
|
|
|
|
|
if !is_implemented && tcx.impl_defaultness(impl_id).is_final() {
|
2021-11-18 21:35:42 +00:00
|
|
|
missing_items.push(tcx.associated_item(trait_item_id));
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
2021-12-21 18:40:50 +03:00
|
|
|
|
2022-04-27 18:14:19 +04:00
|
|
|
// true if this item is specifically implemented in this impl
|
|
|
|
let is_implemented_here = ancestors
|
|
|
|
.leaf_def(tcx, trait_item_id)
|
|
|
|
.map_or(false, |node_item| !node_item.defining_node.is_from_trait());
|
|
|
|
|
|
|
|
if !is_implemented_here {
|
|
|
|
match tcx.eval_default_body_stability(trait_item_id, full_impl_span) {
|
2022-06-20 16:07:14 +04:00
|
|
|
EvalResult::Deny { feature, reason, issue, .. } => default_body_is_unstable(
|
|
|
|
tcx,
|
|
|
|
full_impl_span,
|
|
|
|
trait_item_id,
|
|
|
|
feature,
|
|
|
|
reason,
|
|
|
|
issue,
|
|
|
|
),
|
2022-01-10 12:54:04 +03:00
|
|
|
|
2022-04-27 18:14:19 +04:00
|
|
|
// Unmarked default bodies are considered stable (at least for now).
|
|
|
|
EvalResult::Allow | EvalResult::Unmarked => {}
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 12:54:04 +03:00
|
|
|
|
2022-04-27 18:14:19 +04:00
|
|
|
if let Some(required_items) = &must_implement_one_of {
|
2022-01-10 12:54:04 +03:00
|
|
|
if is_implemented_here {
|
|
|
|
let trait_item = tcx.associated_item(trait_item_id);
|
2022-01-12 21:15:51 -05:00
|
|
|
if required_items.contains(&trait_item.ident(tcx)) {
|
2022-01-10 12:54:04 +03:00
|
|
|
must_implement_one_of = None;
|
|
|
|
}
|
2021-12-21 18:40:50 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
|
2021-01-22 08:22:15 +01:00
|
|
|
if !missing_items.is_empty() {
|
2022-07-04 17:23:24 +09:00
|
|
|
missing_items_err(tcx, tcx.def_span(impl_id), &missing_items, full_impl_span);
|
2021-01-22 08:22:15 +01:00
|
|
|
}
|
2021-12-21 18:40:50 +03:00
|
|
|
|
|
|
|
if let Some(missing_items) = must_implement_one_of {
|
|
|
|
let attr_span = tcx
|
2022-05-02 09:31:56 +02:00
|
|
|
.get_attr(impl_trait_ref.def_id, sym::rustc_must_implement_one_of)
|
2021-12-21 18:40:50 +03:00
|
|
|
.map(|attr| attr.span);
|
|
|
|
|
2022-07-04 17:23:24 +09:00
|
|
|
missing_items_must_implement_one_of_err(
|
|
|
|
tcx,
|
|
|
|
tcx.def_span(impl_id),
|
|
|
|
missing_items,
|
|
|
|
attr_span,
|
|
|
|
);
|
2021-12-21 18:40:50 +03:00
|
|
|
}
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks whether a type can be represented in memory. In particular, it
|
|
|
|
/// identifies types that contain themselves without indirection through a
|
|
|
|
/// pointer, which would mean their size is unbounded.
|
|
|
|
pub(super) fn check_representable(tcx: TyCtxt<'_>, sp: Span, item_def_id: LocalDefId) -> bool {
|
|
|
|
let rty = tcx.type_of(item_def_id);
|
|
|
|
|
|
|
|
// Check that it is possible to represent this type. This call identifies
|
|
|
|
// (1) types that contain themselves and (2) types that contain a different
|
|
|
|
// recursive type. It is only necessary to throw an error on those that
|
|
|
|
// contain themselves. For case 2, there must be an inner type that will be
|
|
|
|
// caught by case 1.
|
2021-11-30 21:30:05 -08:00
|
|
|
match representability::ty_is_representable(tcx, rty, sp, None) {
|
2020-09-19 13:51:59 -07:00
|
|
|
Representability::SelfRecursive(spans) => {
|
|
|
|
recursive_type_with_infinite_size_error(tcx, item_def_id.to_def_id(), spans);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Representability::Representable | Representability::ContainsRecursive => (),
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
|
|
|
|
let t = tcx.type_of(def_id);
|
2022-04-15 15:56:32 +09:00
|
|
|
if let ty::Adt(def, substs) = t.kind()
|
|
|
|
&& def.is_struct()
|
|
|
|
{
|
|
|
|
let fields = &def.non_enum_variant().fields;
|
|
|
|
if fields.is_empty() {
|
|
|
|
struct_span_err!(tcx.sess, sp, E0075, "SIMD vector cannot be empty").emit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let e = fields[0].ty(tcx, substs);
|
|
|
|
if !fields.iter().all(|f| f.ty(tcx, substs) == e) {
|
|
|
|
struct_span_err!(tcx.sess, sp, E0076, "SIMD vector should be homogeneous")
|
|
|
|
.span_label(sp, "SIMD elements must have the same type")
|
|
|
|
.emit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let len = if let ty::Array(_ty, c) = e.kind() {
|
|
|
|
c.try_eval_usize(tcx, tcx.param_env(def.did()))
|
|
|
|
} else {
|
|
|
|
Some(fields.len() as u64)
|
|
|
|
};
|
|
|
|
if let Some(len) = len {
|
|
|
|
if len == 0 {
|
2020-09-19 13:51:59 -07:00
|
|
|
struct_span_err!(tcx.sess, sp, E0075, "SIMD vector cannot be empty").emit();
|
|
|
|
return;
|
2022-04-15 15:56:32 +09:00
|
|
|
} else if len > MAX_SIMD_LANES {
|
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
sp,
|
|
|
|
E0075,
|
|
|
|
"SIMD vector cannot have more than {MAX_SIMD_LANES} elements",
|
|
|
|
)
|
|
|
|
.emit();
|
2020-09-19 13:51:59 -07:00
|
|
|
return;
|
|
|
|
}
|
2022-04-15 15:56:32 +09:00
|
|
|
}
|
2021-01-02 23:49:28 -05:00
|
|
|
|
2022-04-15 15:56:32 +09:00
|
|
|
// Check that we use types valid for use in the lanes of a SIMD "vector register"
|
|
|
|
// These are scalar types which directly match a "machine" type
|
|
|
|
// Yes: Integers, floats, "thin" pointers
|
|
|
|
// No: char, "fat" pointers, compound types
|
|
|
|
match e.kind() {
|
|
|
|
ty::Param(_) => (), // pass struct<T>(T, T, T, T) through, let monomorphization catch errors
|
|
|
|
ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::RawPtr(_) => (), // struct(u8, u8, u8, u8) is ok
|
|
|
|
ty::Array(t, _) if matches!(t.kind(), ty::Param(_)) => (), // pass struct<T>([T; N]) through, let monomorphization catch errors
|
|
|
|
ty::Array(t, _clen)
|
|
|
|
if matches!(
|
|
|
|
t.kind(),
|
|
|
|
ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::RawPtr(_)
|
|
|
|
) =>
|
|
|
|
{ /* struct([f32; 4]) is ok */ }
|
|
|
|
_ => {
|
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
sp,
|
|
|
|
E0077,
|
|
|
|
"SIMD vector element type should be a \
|
|
|
|
primitive scalar (integer/float/pointer) type"
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
return;
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 07:28:41 +11:00
|
|
|
pub(super) fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: ty::AdtDef<'_>) {
|
|
|
|
let repr = def.repr();
|
2020-09-19 13:51:59 -07:00
|
|
|
if repr.packed() {
|
2022-05-02 09:31:56 +02:00
|
|
|
for attr in tcx.get_attrs(def.did(), sym::repr) {
|
|
|
|
for r in attr::parse_repr_attr(&tcx.sess, attr) {
|
2022-02-26 07:43:47 -03:00
|
|
|
if let attr::ReprPacked(pack) = r
|
2022-05-02 09:31:56 +02:00
|
|
|
&& let Some(repr_pack) = repr.pack
|
|
|
|
&& pack as u64 != repr_pack.bytes()
|
|
|
|
{
|
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
sp,
|
|
|
|
E0634,
|
|
|
|
"type has conflicting packed representation hints"
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if repr.align.is_some() {
|
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
sp,
|
|
|
|
E0587,
|
|
|
|
"type has conflicting packed and align representation hints"
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
} else {
|
2022-03-05 07:28:41 +11:00
|
|
|
if let Some(def_spans) = check_packed_inner(tcx, def.did(), &mut vec![]) {
|
2020-09-19 13:51:59 -07:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
sp,
|
|
|
|
E0588,
|
|
|
|
"packed type cannot transitively contain a `#[repr(align)]` type"
|
|
|
|
);
|
|
|
|
|
|
|
|
err.span_note(
|
|
|
|
tcx.def_span(def_spans[0].0),
|
|
|
|
&format!(
|
|
|
|
"`{}` has a `#[repr(align)]` attribute",
|
|
|
|
tcx.item_name(def_spans[0].0)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
if def_spans.len() > 2 {
|
|
|
|
let mut first = true;
|
|
|
|
for (adt_def, span) in def_spans.iter().skip(1).rev() {
|
|
|
|
let ident = tcx.item_name(*adt_def);
|
|
|
|
err.span_note(
|
|
|
|
*span,
|
|
|
|
&if first {
|
|
|
|
format!(
|
|
|
|
"`{}` contains a field of type `{}`",
|
2022-03-05 07:28:41 +11:00
|
|
|
tcx.type_of(def.did()),
|
2020-09-19 13:51:59 -07:00
|
|
|
ident
|
|
|
|
)
|
|
|
|
} else {
|
2022-04-15 15:56:32 +09:00
|
|
|
format!("...which contains a field of type `{ident}`")
|
2020-09-19 13:51:59 -07:00
|
|
|
},
|
|
|
|
);
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn check_packed_inner(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
def_id: DefId,
|
|
|
|
stack: &mut Vec<DefId>,
|
|
|
|
) -> Option<Vec<(DefId, Span)>> {
|
|
|
|
if let ty::Adt(def, substs) = tcx.type_of(def_id).kind() {
|
|
|
|
if def.is_struct() || def.is_union() {
|
2022-03-05 07:28:41 +11:00
|
|
|
if def.repr().align.is_some() {
|
|
|
|
return Some(vec![(def.did(), DUMMY_SP)]);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
stack.push(def_id);
|
|
|
|
for field in &def.non_enum_variant().fields {
|
2022-04-15 15:56:32 +09:00
|
|
|
if let ty::Adt(def, _) = field.ty(tcx, substs).kind()
|
|
|
|
&& !stack.contains(&def.did())
|
|
|
|
&& let Some(mut defs) = check_packed_inner(tcx, def.did(), stack)
|
|
|
|
{
|
|
|
|
defs.push((def.did(), field.ident(tcx).span));
|
|
|
|
return Some(defs);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
stack.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2022-03-05 07:28:41 +11:00
|
|
|
pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, adt: ty::AdtDef<'tcx>) {
|
|
|
|
if !adt.repr().transparent() {
|
2020-09-19 13:51:59 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if adt.is_union() && !tcx.features().transparent_unions {
|
|
|
|
feature_err(
|
|
|
|
&tcx.sess.parse_sess,
|
|
|
|
sym::transparent_unions,
|
|
|
|
sp,
|
|
|
|
"transparent unions are unstable",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
2022-03-05 07:28:41 +11:00
|
|
|
if adt.variants().len() != 1 {
|
|
|
|
bad_variant_count(tcx, adt, sp, adt.did());
|
|
|
|
if adt.variants().is_empty() {
|
2020-09-19 13:51:59 -07:00
|
|
|
// Don't bother checking the fields. No variants (and thus no fields) exist.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-07 16:55:41 +00:00
|
|
|
// For each field, figure out if it's known to be a ZST and align(1), with "known"
|
|
|
|
// respecting #[non_exhaustive] attributes.
|
2020-09-19 13:51:59 -07:00
|
|
|
let field_infos = adt.all_fields().map(|field| {
|
|
|
|
let ty = field.ty(tcx, InternalSubsts::identity_for_item(tcx, field.did));
|
|
|
|
let param_env = tcx.param_env(field.did);
|
|
|
|
let layout = tcx.layout_of(param_env.and(ty));
|
|
|
|
// We are currently checking the type this field came from, so it must be local
|
|
|
|
let span = tcx.hir().span_if_local(field.did).unwrap();
|
2021-01-11 20:45:33 +01:00
|
|
|
let zst = layout.map_or(false, |layout| layout.is_zst());
|
|
|
|
let align1 = layout.map_or(false, |layout| layout.align.abi.bytes() == 1);
|
2022-07-07 16:55:41 +00:00
|
|
|
if !zst {
|
|
|
|
return (span, zst, align1, None);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_non_exhaustive<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
t: Ty<'tcx>,
|
|
|
|
) -> ControlFlow<(&'static str, DefId, SubstsRef<'tcx>, bool)> {
|
|
|
|
match t.kind() {
|
|
|
|
ty::Tuple(list) => list.iter().try_for_each(|t| check_non_exhaustive(tcx, t)),
|
|
|
|
ty::Array(ty, _) => check_non_exhaustive(tcx, *ty),
|
|
|
|
ty::Adt(def, subst) => {
|
|
|
|
if !def.did().is_local() {
|
|
|
|
let non_exhaustive = def.is_variant_list_non_exhaustive()
|
|
|
|
|| def
|
|
|
|
.variants()
|
|
|
|
.iter()
|
|
|
|
.any(ty::VariantDef::is_field_list_non_exhaustive);
|
|
|
|
let has_priv = def.all_fields().any(|f| !f.vis.is_public());
|
|
|
|
if non_exhaustive || has_priv {
|
|
|
|
return ControlFlow::Break((
|
|
|
|
def.descr(),
|
|
|
|
def.did(),
|
|
|
|
subst,
|
|
|
|
non_exhaustive,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
def.all_fields()
|
|
|
|
.map(|field| field.ty(tcx, subst))
|
|
|
|
.try_for_each(|t| check_non_exhaustive(tcx, t))
|
|
|
|
}
|
|
|
|
_ => ControlFlow::Continue(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(span, zst, align1, check_non_exhaustive(tcx, ty).break_value())
|
2020-09-19 13:51:59 -07:00
|
|
|
});
|
|
|
|
|
2022-07-07 16:55:41 +00:00
|
|
|
let non_zst_fields = field_infos
|
|
|
|
.clone()
|
|
|
|
.filter_map(|(span, zst, _align1, _non_exhaustive)| if !zst { Some(span) } else { None });
|
2020-09-19 13:51:59 -07:00
|
|
|
let non_zst_count = non_zst_fields.clone().count();
|
2021-06-14 07:04:56 +09:00
|
|
|
if non_zst_count >= 2 {
|
2020-09-19 13:51:59 -07:00
|
|
|
bad_non_zero_sized_fields(tcx, adt, non_zst_count, non_zst_fields, sp);
|
|
|
|
}
|
2022-07-07 16:55:41 +00:00
|
|
|
let incompatible_zst_fields =
|
|
|
|
field_infos.clone().filter(|(_, _, _, opt)| opt.is_some()).count();
|
|
|
|
let incompat = incompatible_zst_fields + non_zst_count >= 2 && non_zst_count < 2;
|
|
|
|
for (span, zst, align1, non_exhaustive) in field_infos {
|
2020-09-19 13:51:59 -07:00
|
|
|
if zst && !align1 {
|
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
span,
|
|
|
|
E0691,
|
|
|
|
"zero-sized field in transparent {} has alignment larger than 1",
|
|
|
|
adt.descr(),
|
|
|
|
)
|
|
|
|
.span_label(span, "has alignment larger than 1")
|
|
|
|
.emit();
|
|
|
|
}
|
2022-07-07 16:55:41 +00:00
|
|
|
if incompat && let Some((descr, def_id, substs, non_exhaustive)) = non_exhaustive {
|
|
|
|
tcx.struct_span_lint_hir(
|
|
|
|
REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS,
|
|
|
|
tcx.hir().local_def_id_to_hir_id(adt.did().expect_local()),
|
|
|
|
span,
|
|
|
|
|lint| {
|
|
|
|
let note = if non_exhaustive {
|
|
|
|
"is marked with `#[non_exhaustive]`"
|
|
|
|
} else {
|
|
|
|
"contains private fields"
|
|
|
|
};
|
|
|
|
let field_ty = tcx.def_path_str_with_substs(def_id, substs);
|
|
|
|
lint.build("zero-sized fields in repr(transparent) cannot contain external non-exhaustive types")
|
|
|
|
.note(format!("this {descr} contains `{field_ty}`, which {note}, \
|
|
|
|
and makes it not a breaking change to become non-zero-sized in the future."))
|
|
|
|
.emit();
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(trivial_numeric_casts)]
|
2022-07-08 02:32:51 +00:00
|
|
|
fn check_enum<'tcx>(tcx: TyCtxt<'tcx>, vs: &'tcx [hir::Variant<'tcx>], def_id: LocalDefId) {
|
2020-09-19 13:51:59 -07:00
|
|
|
let def = tcx.adt_def(def_id);
|
2022-07-08 02:32:51 +00:00
|
|
|
let sp = tcx.def_span(def_id);
|
2020-09-19 13:51:59 -07:00
|
|
|
def.destructor(tcx); // force the destructor to be evaluated
|
|
|
|
|
|
|
|
if vs.is_empty() {
|
2022-09-06 14:16:54 +08:00
|
|
|
if let Some(attr) = tcx.get_attrs(def_id.to_def_id(), sym::repr).next() {
|
2020-09-19 13:51:59 -07:00
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
attr.span,
|
|
|
|
E0084,
|
|
|
|
"unsupported representation for zero-variant enum"
|
|
|
|
)
|
|
|
|
.span_label(sp, "zero-variant enum")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 07:28:41 +11:00
|
|
|
let repr_type_ty = def.repr().discr_type().to_ty(tcx);
|
2020-09-19 13:51:59 -07:00
|
|
|
if repr_type_ty == tcx.types.i128 || repr_type_ty == tcx.types.u128 {
|
|
|
|
if !tcx.features().repr128 {
|
|
|
|
feature_err(
|
|
|
|
&tcx.sess.parse_sess,
|
|
|
|
sym::repr128,
|
|
|
|
sp,
|
|
|
|
"repr with 128-bit type is unstable",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for v in vs {
|
|
|
|
if let Some(ref e) = v.disr_expr {
|
|
|
|
tcx.ensure().typeck(tcx.hir().local_def_id(e.hir_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 07:28:41 +11:00
|
|
|
if tcx.adt_def(def_id).repr().int.is_none() && tcx.features().arbitrary_enum_discriminant {
|
2020-12-24 02:55:21 +01:00
|
|
|
let is_unit = |var: &hir::Variant<'_>| matches!(var.data, hir::VariantData::Unit(..));
|
2020-09-19 13:51:59 -07:00
|
|
|
|
|
|
|
let has_disr = |var: &hir::Variant<'_>| var.disr_expr.is_some();
|
|
|
|
let has_non_units = vs.iter().any(|var| !is_unit(var));
|
|
|
|
let disr_units = vs.iter().any(|var| is_unit(&var) && has_disr(&var));
|
|
|
|
let disr_non_unit = vs.iter().any(|var| !is_unit(&var) && has_disr(&var));
|
|
|
|
|
|
|
|
if disr_non_unit || (disr_units && has_non_units) {
|
|
|
|
let mut err =
|
|
|
|
struct_span_err!(tcx.sess, sp, E0732, "`#[repr(inttype)]` must be specified");
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 17:24:25 +02:00
|
|
|
detect_discriminant_duplicate(tcx, def.discriminants(tcx).collect(), vs, sp);
|
2020-09-19 13:51:59 -07:00
|
|
|
|
|
|
|
check_representable(tcx, sp, def_id);
|
|
|
|
check_transparent(tcx, sp, def);
|
|
|
|
}
|
|
|
|
|
2022-08-08 21:34:55 +02:00
|
|
|
/// Part of enum check. Given the discriminants of an enum, errors if two or more discriminants are equal
|
2022-08-07 17:24:25 +02:00
|
|
|
fn detect_discriminant_duplicate<'tcx>(
|
2021-07-15 21:25:11 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2022-08-07 17:24:25 +02:00
|
|
|
mut discrs: Vec<(VariantIdx, Discr<'tcx>)>,
|
|
|
|
vs: &'tcx [hir::Variant<'tcx>],
|
|
|
|
self_span: Span,
|
|
|
|
) {
|
2022-08-08 21:34:55 +02:00
|
|
|
// Helper closure to reduce duplicate code. This gets called everytime we detect a duplicate.
|
|
|
|
// Here `idx` refers to the order of which the discriminant appears, and its index in `vs`
|
2022-08-10 03:39:41 +00:00
|
|
|
let report = |dis: Discr<'tcx>, idx: usize, err: &mut Diagnostic| {
|
2022-08-08 21:34:55 +02:00
|
|
|
let var = &vs[idx]; // HIR for the duplicate discriminant
|
2022-08-07 17:24:25 +02:00
|
|
|
let (span, display_discr) = match var.disr_expr {
|
|
|
|
Some(ref expr) => {
|
|
|
|
// In the case the discriminant is both a duplicate and overflowed, let the user know
|
|
|
|
if let hir::ExprKind::Lit(lit) = &tcx.hir().body(expr.body).value.kind
|
|
|
|
&& let rustc_ast::LitKind::Int(lit_value, _int_kind) = &lit.node
|
|
|
|
&& *lit_value != dis.val
|
|
|
|
{
|
|
|
|
(tcx.hir().span(expr.hir_id), format!("`{dis}` (overflowed from `{lit_value}`)"))
|
2022-08-07 22:28:16 +02:00
|
|
|
// Otherwise, format the value as-is
|
2022-08-07 17:24:25 +02:00
|
|
|
} else {
|
|
|
|
(tcx.hir().span(expr.hir_id), format!("`{dis}`"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
2022-08-07 22:28:16 +02:00
|
|
|
// At this point we know this discriminant is a duplicate, and was not explicitly
|
2022-08-07 23:50:12 +02:00
|
|
|
// assigned by the user. Here we iterate backwards to fetch the HIR for the last
|
2022-08-18 10:13:37 +08:00
|
|
|
// explicitly assigned discriminant, and letting the user know that this was the
|
2022-08-07 22:28:16 +02:00
|
|
|
// increment startpoint, and how many steps from there leading to the duplicate
|
2022-08-07 17:24:25 +02:00
|
|
|
if let Some((n, hir::Variant { span, ident, .. })) =
|
|
|
|
vs[..idx].iter().rev().enumerate().find(|v| v.1.disr_expr.is_some())
|
|
|
|
{
|
|
|
|
let ve_ident = var.ident;
|
|
|
|
let n = n + 1;
|
2022-08-08 21:34:55 +02:00
|
|
|
let sp = if n > 1 { "variants" } else { "variant" };
|
2022-08-07 17:24:25 +02:00
|
|
|
|
|
|
|
err.span_label(
|
|
|
|
*span,
|
|
|
|
format!("discriminant for `{ve_ident}` incremented from this startpoint (`{ident}` + {n} {sp} later => `{ve_ident}` = {dis})"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
(vs[idx].span, format!("`{dis}`"))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
err.span_label(span, format!("{display_discr} assigned here"));
|
|
|
|
};
|
|
|
|
|
2022-08-08 21:34:55 +02:00
|
|
|
// Here we loop through the discriminants, comparing each discriminant to another.
|
2022-08-18 10:13:37 +08:00
|
|
|
// When a duplicate is detected, we instantiate an error and point to both
|
2022-08-07 23:50:12 +02:00
|
|
|
// initial and duplicate value. The duplicate discriminant is then discarded by swapping
|
|
|
|
// it with the last element and decrementing the `vec.len` (which is why we have to evaluate
|
|
|
|
// `discrs.len()` anew every iteration, and why this could be tricky to do in a functional
|
|
|
|
// style as we are mutating `discrs` on the fly).
|
2022-08-07 17:24:25 +02:00
|
|
|
let mut i = 0;
|
|
|
|
while i < discrs.len() {
|
|
|
|
let hir_var_i_idx = discrs[i].0.index();
|
|
|
|
let mut error: Option<DiagnosticBuilder<'_, _>> = None;
|
|
|
|
|
|
|
|
let mut o = i + 1;
|
|
|
|
while o < discrs.len() {
|
|
|
|
let hir_var_o_idx = discrs[o].0.index();
|
|
|
|
|
|
|
|
if discrs[i].1.val == discrs[o].1.val {
|
|
|
|
let err = error.get_or_insert_with(|| {
|
|
|
|
let mut ret = struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
self_span,
|
|
|
|
E0081,
|
|
|
|
"discriminant value `{}` assigned more than once",
|
|
|
|
discrs[i].1,
|
|
|
|
);
|
|
|
|
|
2022-08-07 22:28:16 +02:00
|
|
|
report(discrs[i].1, hir_var_i_idx, &mut ret);
|
2022-08-07 17:24:25 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
});
|
|
|
|
|
2022-08-07 22:28:16 +02:00
|
|
|
report(discrs[o].1, hir_var_o_idx, err);
|
2022-08-07 17:24:25 +02:00
|
|
|
|
2022-08-07 22:28:16 +02:00
|
|
|
// Safe to unwrap here, as we wouldn't reach this point if `discrs` was empty
|
2022-08-07 17:24:25 +02:00
|
|
|
discrs[o] = *discrs.last().unwrap();
|
|
|
|
discrs.pop();
|
|
|
|
} else {
|
|
|
|
o += 1;
|
|
|
|
}
|
2021-07-15 21:25:11 +02:00
|
|
|
}
|
2022-05-27 15:42:15 +02:00
|
|
|
|
2022-08-07 17:24:25 +02:00
|
|
|
if let Some(mut e) = error {
|
|
|
|
e.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
i += 1;
|
|
|
|
}
|
2021-07-15 21:25:11 +02:00
|
|
|
}
|
|
|
|
|
2020-09-19 13:51:59 -07:00
|
|
|
pub(super) fn check_type_params_are_used<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
generics: &ty::Generics,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) {
|
|
|
|
debug!("check_type_params_are_used(generics={:?}, ty={:?})", generics, ty);
|
|
|
|
|
|
|
|
assert_eq!(generics.parent, None);
|
|
|
|
|
|
|
|
if generics.own_counts().types == 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut params_used = BitSet::new_empty(generics.params.len());
|
|
|
|
|
|
|
|
if ty.references_error() {
|
|
|
|
// If there is already another error, do not emit
|
|
|
|
// an error for not using a type parameter.
|
2022-01-22 18:49:12 -06:00
|
|
|
assert!(tcx.sess.has_errors().is_some());
|
2020-09-19 13:51:59 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-12 03:19:52 +00:00
|
|
|
for leaf in ty.walk() {
|
2022-04-15 15:56:32 +09:00
|
|
|
if let GenericArgKind::Type(leaf_ty) = leaf.unpack()
|
|
|
|
&& let ty::Param(param) = leaf_ty.kind()
|
|
|
|
{
|
|
|
|
debug!("found use of ty param {:?}", param);
|
|
|
|
params_used.insert(param.index);
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for param in &generics.params {
|
2022-04-15 15:56:32 +09:00
|
|
|
if !params_used.contains(param.index)
|
|
|
|
&& let ty::GenericParamDefKind::Type { .. } = param.kind
|
|
|
|
{
|
|
|
|
let span = tcx.def_span(param.def_id);
|
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
span,
|
|
|
|
E0091,
|
|
|
|
"type parameter `{}` is unused",
|
|
|
|
param.name,
|
|
|
|
)
|
|
|
|
.span_label(span, "unused type parameter")
|
|
|
|
.emit();
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
2022-04-29 12:22:40 -04:00
|
|
|
let module = tcx.hir_module_items(module_def_id);
|
|
|
|
for id in module.items() {
|
2022-04-29 13:09:03 -04:00
|
|
|
check_item_type(tcx, id);
|
2022-04-29 12:22:40 -04:00
|
|
|
}
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
|
2022-01-22 18:49:12 -06:00
|
|
|
fn async_opaque_type_cycle_error(tcx: TyCtxt<'_>, span: Span) -> ErrorGuaranteed {
|
2020-09-19 13:51:59 -07:00
|
|
|
struct_span_err!(tcx.sess, span, E0733, "recursion in an `async fn` requires boxing")
|
|
|
|
.span_label(span, "recursive `async fn`")
|
|
|
|
.note("a recursive `async fn` must be rewritten to return a boxed `dyn Future`")
|
2021-02-09 18:43:39 +05:30
|
|
|
.note(
|
|
|
|
"consider using the `async_recursion` crate: https://crates.io/crates/async_recursion",
|
|
|
|
)
|
2022-01-22 18:49:12 -06:00
|
|
|
.emit()
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit an error for recursive opaque types.
|
|
|
|
///
|
|
|
|
/// If this is a return `impl Trait`, find the item's return expressions and point at them. For
|
|
|
|
/// direct recursion this is enough, but for indirect recursion also point at the last intermediary
|
|
|
|
/// `impl Trait`.
|
|
|
|
///
|
|
|
|
/// If all the return expressions evaluate to `!`, then we explain that the error will go away
|
|
|
|
/// after changing it. This can happen when a user uses `panic!()` or similar as a placeholder.
|
2022-01-22 18:49:12 -06:00
|
|
|
fn opaque_type_cycle_error(tcx: TyCtxt<'_>, def_id: LocalDefId, span: Span) -> ErrorGuaranteed {
|
2020-09-19 13:51:59 -07:00
|
|
|
let mut err = struct_span_err!(tcx.sess, span, E0720, "cannot resolve opaque type");
|
|
|
|
|
|
|
|
let mut label = false;
|
2021-10-21 19:41:47 +02:00
|
|
|
if let Some((def_id, visitor)) = get_owner_return_paths(tcx, def_id) {
|
|
|
|
let typeck_results = tcx.typeck(def_id);
|
2020-09-19 13:51:59 -07:00
|
|
|
if visitor
|
|
|
|
.returns
|
|
|
|
.iter()
|
|
|
|
.filter_map(|expr| typeck_results.node_type_opt(expr.hir_id))
|
|
|
|
.all(|ty| matches!(ty.kind(), ty::Never))
|
|
|
|
{
|
|
|
|
let spans = visitor
|
|
|
|
.returns
|
|
|
|
.iter()
|
|
|
|
.filter(|expr| typeck_results.node_type_opt(expr.hir_id).is_some())
|
|
|
|
.map(|expr| expr.span)
|
|
|
|
.collect::<Vec<Span>>();
|
|
|
|
let span_len = spans.len();
|
|
|
|
if span_len == 1 {
|
|
|
|
err.span_label(spans[0], "this returned value is of `!` type");
|
|
|
|
} else {
|
|
|
|
let mut multispan: MultiSpan = spans.clone().into();
|
|
|
|
for span in spans {
|
2022-06-29 21:16:43 +09:00
|
|
|
multispan.push_span_label(span, "this returned value is of `!` type");
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
err.span_note(multispan, "these returned values have a concrete \"never\" type");
|
|
|
|
}
|
|
|
|
err.help("this error will resolve once the item's body returns a concrete type");
|
|
|
|
} else {
|
|
|
|
let mut seen = FxHashSet::default();
|
|
|
|
seen.insert(span);
|
|
|
|
err.span_label(span, "recursive opaque type");
|
|
|
|
label = true;
|
|
|
|
for (sp, ty) in visitor
|
|
|
|
.returns
|
|
|
|
.iter()
|
|
|
|
.filter_map(|e| typeck_results.node_type_opt(e.hir_id).map(|t| (e.span, t)))
|
|
|
|
.filter(|(_, ty)| !matches!(ty.kind(), ty::Never))
|
|
|
|
{
|
2021-03-16 00:05:45 +01:00
|
|
|
struct OpaqueTypeCollector(Vec<DefId>);
|
2022-06-17 13:15:00 +01:00
|
|
|
impl<'tcx> ty::visit::TypeVisitor<'tcx> for OpaqueTypeCollector {
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2020-09-19 13:51:59 -07:00
|
|
|
match *t.kind() {
|
|
|
|
ty::Opaque(def, _) => {
|
|
|
|
self.0.push(def);
|
2020-10-21 14:26:34 +02:00
|
|
|
ControlFlow::CONTINUE
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
_ => t.super_visit_with(self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 00:05:45 +01:00
|
|
|
let mut visitor = OpaqueTypeCollector(vec![]);
|
2020-09-19 13:51:59 -07:00
|
|
|
ty.visit_with(&mut visitor);
|
|
|
|
for def_id in visitor.0 {
|
|
|
|
let ty_span = tcx.def_span(def_id);
|
|
|
|
if !seen.contains(&ty_span) {
|
2022-04-15 15:56:32 +09:00
|
|
|
err.span_label(ty_span, &format!("returning this opaque type `{ty}`"));
|
2020-09-19 13:51:59 -07:00
|
|
|
seen.insert(ty_span);
|
|
|
|
}
|
2022-04-15 15:56:32 +09:00
|
|
|
err.span_label(sp, &format!("returning here with type `{ty}`"));
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !label {
|
|
|
|
err.span_label(span, "cannot resolve opaque type");
|
|
|
|
}
|
2022-01-22 18:49:12 -06:00
|
|
|
err.emit()
|
2020-09-19 13:51:59 -07:00
|
|
|
}
|