2024-10-28 03:38:33 +00:00
|
|
|
use rustc_abi::{HasDataLayout, TargetDataLayout};
|
2024-10-16 23:14:01 +00:00
|
|
|
use rustc_hir::Attribute;
|
2022-05-04 18:02:30 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2020-04-09 08:43:00 +00:00
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2024-04-29 03:56:41 +00:00
|
|
|
use rustc_middle::span_bug;
|
2024-11-15 12:53:31 +00:00
|
|
|
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutError, LayoutOfHelpers};
|
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
2022-10-03 23:34:59 +00:00
|
|
|
use rustc_span::source_map::Spanned;
|
2024-12-12 23:29:23 +00:00
|
|
|
use rustc_span::{Span, sym};
|
2024-07-21 19:20:41 +00:00
|
|
|
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
|
2023-09-09 14:52:20 +00:00
|
|
|
use rustc_trait_selection::infer::TyCtxtInferExt;
|
|
|
|
use rustc_trait_selection::traits;
|
2019-01-09 20:16:32 +00:00
|
|
|
|
2023-09-02 12:03:25 +00:00
|
|
|
use crate::errors::{
|
|
|
|
LayoutAbi, LayoutAlign, LayoutHomogeneousAggregate, LayoutInvalidAttribute, LayoutOf,
|
|
|
|
LayoutSize, UnrecognizedField,
|
|
|
|
};
|
2022-09-23 18:23:18 +00:00
|
|
|
|
2019-06-21 18:27:44 +00:00
|
|
|
pub fn test_layout(tcx: TyCtxt<'_>) {
|
2024-10-09 07:01:57 +00:00
|
|
|
if !tcx.features().rustc_attrs() {
|
2019-01-09 20:16:32 +00:00
|
|
|
// if the `rustc_attrs` feature is not enabled, don't bother testing layout
|
2023-08-25 21:01:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-09-09 15:39:53 +00:00
|
|
|
for id in tcx.hir_crate_items(()).definitions() {
|
|
|
|
for attr in tcx.get_attrs(id, sym::rustc_layout) {
|
|
|
|
match tcx.def_kind(id) {
|
2023-09-26 02:15:32 +00:00
|
|
|
DefKind::TyAlias | DefKind::Enum | DefKind::Struct | DefKind::Union => {
|
2023-09-09 15:39:53 +00:00
|
|
|
dump_layout_of(tcx, id, attr);
|
2023-09-02 12:03:25 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(LayoutInvalidAttribute { span: tcx.def_span(id) });
|
2023-09-02 12:03:25 +00:00
|
|
|
}
|
2019-01-09 20:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-09 14:52:20 +00:00
|
|
|
pub fn ensure_wf<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2024-11-15 12:53:31 +00:00
|
|
|
typing_env: ty::TypingEnv<'tcx>,
|
2023-09-09 14:52:20 +00:00
|
|
|
ty: Ty<'tcx>,
|
2023-09-09 15:43:20 +00:00
|
|
|
def_id: LocalDefId,
|
2023-09-09 14:52:20 +00:00
|
|
|
span: Span,
|
|
|
|
) -> bool {
|
2024-11-15 12:53:31 +00:00
|
|
|
let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
|
|
|
|
let ocx = traits::ObligationCtxt::new_with_diagnostics(&infcx);
|
2023-09-09 14:52:20 +00:00
|
|
|
let pred = ty::ClauseKind::WellFormed(ty.into());
|
|
|
|
let obligation = traits::Obligation::new(
|
|
|
|
tcx,
|
2023-09-09 15:43:20 +00:00
|
|
|
traits::ObligationCause::new(
|
|
|
|
span,
|
|
|
|
def_id,
|
|
|
|
traits::ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Ty(def_id))),
|
|
|
|
),
|
2023-09-09 14:52:20 +00:00
|
|
|
param_env,
|
|
|
|
pred,
|
|
|
|
);
|
|
|
|
ocx.register_obligation(obligation);
|
|
|
|
let errors = ocx.select_all_or_error();
|
|
|
|
if !errors.is_empty() {
|
2023-08-14 13:09:53 +00:00
|
|
|
infcx.err_ctxt().report_fulfillment_errors(errors);
|
2023-09-09 14:52:20 +00:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
// looks WF!
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 21:10:40 +00:00
|
|
|
fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
|
2024-11-15 12:53:31 +00:00
|
|
|
let typing_env = ty::TypingEnv::post_analysis(tcx, item_def_id);
|
2023-07-11 21:35:29 +00:00
|
|
|
let ty = tcx.type_of(item_def_id).instantiate_identity();
|
2023-09-09 14:52:20 +00:00
|
|
|
let span = tcx.def_span(item_def_id.to_def_id());
|
2024-11-15 12:53:31 +00:00
|
|
|
if !ensure_wf(tcx, typing_env, ty, item_def_id, span) {
|
2023-09-09 14:52:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-11-15 12:53:31 +00:00
|
|
|
match tcx.layout_of(typing_env.as_query_input(ty)) {
|
2022-05-04 18:02:30 +00:00
|
|
|
Ok(ty_layout) => {
|
|
|
|
// Check out the `#[rustc_layout(..)]` attribute to tell what to dump.
|
|
|
|
// The `..` are the names of fields to dump.
|
|
|
|
let meta_items = attr.meta_item_list().unwrap_or_default();
|
|
|
|
for meta_item in meta_items {
|
|
|
|
match meta_item.name_or_empty() {
|
2024-10-29 20:37:26 +00:00
|
|
|
// FIXME: this never was about ABI and now this dump arg is confusing
|
2022-05-04 18:02:30 +00:00
|
|
|
sym::abi => {
|
2024-10-29 20:37:26 +00:00
|
|
|
tcx.dcx().emit_err(LayoutAbi {
|
|
|
|
span,
|
|
|
|
abi: format!("{:?}", ty_layout.backend_repr),
|
|
|
|
});
|
2022-05-04 18:02:30 +00:00
|
|
|
}
|
2019-01-09 20:16:32 +00:00
|
|
|
|
2022-05-04 18:02:30 +00:00
|
|
|
sym::align => {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(LayoutAlign {
|
2023-09-09 14:52:20 +00:00
|
|
|
span,
|
2022-09-23 18:23:18 +00:00
|
|
|
align: format!("{:?}", ty_layout.align),
|
|
|
|
});
|
2022-05-04 18:02:30 +00:00
|
|
|
}
|
2019-01-09 20:16:32 +00:00
|
|
|
|
2022-05-04 18:02:30 +00:00
|
|
|
sym::size => {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx()
|
2023-09-09 14:52:20 +00:00
|
|
|
.emit_err(LayoutSize { span, size: format!("{:?}", ty_layout.size) });
|
2022-05-04 18:02:30 +00:00
|
|
|
}
|
2019-01-09 20:16:32 +00:00
|
|
|
|
2022-05-04 18:02:30 +00:00
|
|
|
sym::homogeneous_aggregate => {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(LayoutHomogeneousAggregate {
|
2023-09-09 14:52:20 +00:00
|
|
|
span,
|
2022-09-23 18:23:18 +00:00
|
|
|
homogeneous_aggregate: format!(
|
|
|
|
"{:?}",
|
2024-11-15 12:53:31 +00:00
|
|
|
ty_layout
|
|
|
|
.homogeneous_aggregate(&UnwrapLayoutCx { tcx, typing_env })
|
2022-05-04 18:02:30 +00:00
|
|
|
),
|
2022-09-23 18:23:18 +00:00
|
|
|
});
|
2022-05-04 18:02:30 +00:00
|
|
|
}
|
2019-01-09 20:16:32 +00:00
|
|
|
|
2022-05-04 18:02:30 +00:00
|
|
|
sym::debug => {
|
2024-11-15 12:53:31 +00:00
|
|
|
let normalized_ty =
|
|
|
|
format!("{}", tcx.normalize_erasing_regions(typing_env, ty));
|
2023-09-07 05:14:40 +00:00
|
|
|
// FIXME: using the `Debug` impl here isn't ideal.
|
2022-09-23 18:23:18 +00:00
|
|
|
let ty_layout = format!("{:#?}", *ty_layout);
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(LayoutOf { span, normalized_ty, ty_layout });
|
2022-05-04 18:02:30 +00:00
|
|
|
}
|
2020-03-10 21:25:53 +00:00
|
|
|
|
2022-05-04 18:02:30 +00:00
|
|
|
name => {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(UnrecognizedField { span: meta_item.span(), name });
|
2019-01-09 20:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-04 18:02:30 +00:00
|
|
|
}
|
2019-01-09 20:16:32 +00:00
|
|
|
|
2022-05-04 18:02:30 +00:00
|
|
|
Err(layout_error) => {
|
2024-09-15 20:16:21 +00:00
|
|
|
tcx.dcx().emit_err(Spanned { node: layout_error.into_diagnostic(), span });
|
2019-01-09 20:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
struct UnwrapLayoutCx<'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2024-11-15 12:53:31 +00:00
|
|
|
typing_env: ty::TypingEnv<'tcx>,
|
2019-01-09 20:16:32 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 05:16:36 +00:00
|
|
|
impl<'tcx> LayoutOfHelpers<'tcx> for UnwrapLayoutCx<'tcx> {
|
2021-08-30 15:01:58 +00:00
|
|
|
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
|
2023-05-17 10:30:14 +00:00
|
|
|
span_bug!(span, "`#[rustc_layout(..)]` test resulted in `layout_of({ty}) = Err({err})`",);
|
2019-01-09 20:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 05:16:36 +00:00
|
|
|
impl<'tcx> HasTyCtxt<'tcx> for UnwrapLayoutCx<'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
2019-01-09 20:16:32 +00:00
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-15 12:53:31 +00:00
|
|
|
impl<'tcx> HasTypingEnv<'tcx> for UnwrapLayoutCx<'tcx> {
|
|
|
|
fn typing_env(&self) -> ty::TypingEnv<'tcx> {
|
|
|
|
self.typing_env
|
2019-05-04 09:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 05:16:36 +00:00
|
|
|
impl<'tcx> HasDataLayout for UnwrapLayoutCx<'tcx> {
|
2019-01-09 20:16:32 +00:00
|
|
|
fn data_layout(&self) -> &TargetDataLayout {
|
|
|
|
self.tcx.data_layout()
|
|
|
|
}
|
|
|
|
}
|