From a5b031136702068e4a82d384fec1e66503974064 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 9 Sep 2023 16:52:20 +0200 Subject: [PATCH 1/3] rustc_layout, rustc_abi: make sure the types are well-formed --- compiler/rustc_passes/src/abi_test.rs | 13 +++-- compiler/rustc_passes/src/layout_test.rs | 61 +++++++++++++------- tests/ui/abi/compatibility.rs | 9 +-- tests/ui/abi/debug.rs | 3 + tests/ui/abi/debug.stderr | 12 +++- tests/ui/associated-types/issue-85103.rs | 2 +- tests/ui/associated-types/issue-85103.stderr | 8 ++- tests/ui/layout/debug.rs | 3 + tests/ui/layout/debug.stderr | 12 +++- 9 files changed, 87 insertions(+), 36 deletions(-) diff --git a/compiler/rustc_passes/src/abi_test.rs b/compiler/rustc_passes/src/abi_test.rs index 7e781bdd6f4a8..689f083217bbc 100644 --- a/compiler/rustc_passes/src/abi_test.rs +++ b/compiler/rustc_passes/src/abi_test.rs @@ -7,6 +7,7 @@ use rustc_span::source_map::Spanned; use rustc_span::symbol::sym; use rustc_target::abi::call::FnAbi; +use super::layout_test::ensure_wf; use crate::errors::{AbiInvalidAttribute, AbiNe, AbiOf, UnrecognizedField}; pub fn test_abi(tcx: TyCtxt<'_>) { @@ -131,6 +132,10 @@ fn test_abi_eq<'tcx>(abi1: &'tcx FnAbi<'tcx, Ty<'tcx>>, abi2: &'tcx FnAbi<'tcx, fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) { let param_env = tcx.param_env(item_def_id); let ty = tcx.type_of(item_def_id).instantiate_identity(); + let span = tcx.def_span(item_def_id); + if !ensure_wf(tcx, param_env, ty, span) { + return; + } let meta_items = attr.meta_item_list().unwrap_or_default(); for meta_item in meta_items { match meta_item.name_or_empty() { @@ -148,11 +153,7 @@ fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) { ); let fn_name = tcx.item_name(item_def_id); - tcx.sess.emit_err(AbiOf { - span: tcx.def_span(item_def_id), - fn_name, - fn_abi: format!("{:#?}", abi), - }); + tcx.sess.emit_err(AbiOf { span, fn_name, fn_abi: format!("{:#?}", abi) }); } sym::assert_eq => { let ty::Tuple(fields) = ty.kind() else { @@ -196,7 +197,7 @@ fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) { if !test_abi_eq(abi1, abi2) { tcx.sess.emit_err(AbiNe { - span: tcx.def_span(item_def_id), + span, left: format!("{:#?}", abi1), right: format!("{:#?}", abi2), }); diff --git a/compiler/rustc_passes/src/layout_test.rs b/compiler/rustc_passes/src/layout_test.rs index f3c12e0746d97..6dacac150d34f 100644 --- a/compiler/rustc_passes/src/layout_test.rs +++ b/compiler/rustc_passes/src/layout_test.rs @@ -2,11 +2,13 @@ use rustc_ast::Attribute; use rustc_hir::def::DefKind; use rustc_hir::def_id::LocalDefId; use rustc_middle::ty::layout::{HasParamEnv, HasTyCtxt, LayoutError, LayoutOfHelpers, TyAndLayout}; -use rustc_middle::ty::{ParamEnv, Ty, TyCtxt}; +use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt}; use rustc_span::source_map::Spanned; use rustc_span::symbol::sym; use rustc_span::Span; use rustc_target::abi::{HasDataLayout, TargetDataLayout}; +use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt; +use rustc_trait_selection::{infer::TyCtxtInferExt, traits}; use crate::errors::{ LayoutAbi, LayoutAlign, LayoutHomogeneousAggregate, LayoutInvalidAttribute, LayoutOf, @@ -40,9 +42,39 @@ pub fn test_layout(tcx: TyCtxt<'_>) { } } +pub fn ensure_wf<'tcx>( + tcx: TyCtxt<'tcx>, + param_env: ParamEnv<'tcx>, + ty: Ty<'tcx>, + span: Span, +) -> bool { + let pred = ty::ClauseKind::WellFormed(ty.into()); + let obligation = traits::Obligation::new( + tcx, + traits::ObligationCause::dummy_with_span(span), + param_env, + pred, + ); + let infcx = tcx.infer_ctxt().build(); + let ocx = traits::ObligationCtxt::new(&infcx); + ocx.register_obligation(obligation); + let errors = ocx.select_all_or_error(); + if !errors.is_empty() { + infcx.err_ctxt().report_fulfillment_errors(&errors); + false + } else { + // looks WF! + true + } +} + fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) { let param_env = tcx.param_env(item_def_id); let ty = tcx.type_of(item_def_id).instantiate_identity(); + let span = tcx.def_span(item_def_id.to_def_id()); + if !ensure_wf(tcx, param_env, ty, span) { + return; + } match tcx.layout_of(param_env.and(ty)) { Ok(ty_layout) => { // Check out the `#[rustc_layout(..)]` attribute to tell what to dump. @@ -51,29 +83,24 @@ fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) { for meta_item in meta_items { match meta_item.name_or_empty() { sym::abi => { - tcx.sess.emit_err(LayoutAbi { - span: tcx.def_span(item_def_id.to_def_id()), - abi: format!("{:?}", ty_layout.abi), - }); + tcx.sess.emit_err(LayoutAbi { span, abi: format!("{:?}", ty_layout.abi) }); } sym::align => { tcx.sess.emit_err(LayoutAlign { - span: tcx.def_span(item_def_id.to_def_id()), + span, align: format!("{:?}", ty_layout.align), }); } sym::size => { - tcx.sess.emit_err(LayoutSize { - span: tcx.def_span(item_def_id.to_def_id()), - size: format!("{:?}", ty_layout.size), - }); + tcx.sess + .emit_err(LayoutSize { span, size: format!("{:?}", ty_layout.size) }); } sym::homogeneous_aggregate => { tcx.sess.emit_err(LayoutHomogeneousAggregate { - span: tcx.def_span(item_def_id.to_def_id()), + span, homogeneous_aggregate: format!( "{:?}", ty_layout.homogeneous_aggregate(&UnwrapLayoutCx { tcx, param_env }) @@ -90,11 +117,7 @@ fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) { ) ); let ty_layout = format!("{:#?}", *ty_layout); - tcx.sess.emit_err(LayoutOf { - span: tcx.def_span(item_def_id.to_def_id()), - normalized_ty, - ty_layout, - }); + tcx.sess.emit_err(LayoutOf { span, normalized_ty, ty_layout }); } name => { @@ -105,11 +128,7 @@ fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) { } Err(layout_error) => { - tcx.sess.emit_fatal(Spanned { - node: layout_error.into_diagnostic(), - - span: tcx.def_span(item_def_id.to_def_id()), - }); + tcx.sess.emit_fatal(Spanned { node: layout_error.into_diagnostic(), span }); } } } diff --git a/tests/ui/abi/compatibility.rs b/tests/ui/abi/compatibility.rs index 0bbcba200c7e3..86fb365507a88 100644 --- a/tests/ui/abi/compatibility.rs +++ b/tests/ui/abi/compatibility.rs @@ -2,6 +2,7 @@ #![feature(rustc_attrs, transparent_unions)] #![allow(unused, improper_ctypes_definitions)] use std::marker::PhantomData; +use std::mem::ManuallyDrop; use std::num::NonZeroI32; use std::ptr::NonNull; @@ -37,9 +38,9 @@ enum ReprCEnum { Variant2(T), } #[repr(C)] -union ReprCUnion { +union ReprCUnion { nothing: (), - something: T, + something: ManuallyDrop, } macro_rules! test_abi_compatible { @@ -82,9 +83,9 @@ struct Wrapper2((), Zst, T); #[repr(transparent)] struct Wrapper3(T, [u8; 0], PhantomData); #[repr(transparent)] -union WrapperUnion { +union WrapperUnion { nothing: (), - something: T, + something: ManuallyDrop, } macro_rules! test_transparent { diff --git a/tests/ui/abi/debug.rs b/tests/ui/abi/debug.rs index 9decb41d56517..77715ee4023b8 100644 --- a/tests/ui/abi/debug.rs +++ b/tests/ui/abi/debug.rs @@ -48,3 +48,6 @@ type TestAbiNeFloat = (fn(f32), fn(u32)); //~ ERROR: ABIs are not compatible // Sign matters on some targets (such as s390x), so let's make sure we never accept this. #[rustc_abi(assert_eq)] type TestAbiNeSign = (fn(i32), fn(u32)); //~ ERROR: ABIs are not compatible + +#[rustc_abi(assert_eq)] +type TestAbiEqNonsense = (fn((str, str)), fn((str, str))); //~ ERROR: cannot be known at compilation time diff --git a/tests/ui/abi/debug.stderr b/tests/ui/abi/debug.stderr index 0feaf0971d8d2..0ca74fdda355d 100644 --- a/tests/ui/abi/debug.stderr +++ b/tests/ui/abi/debug.stderr @@ -945,5 +945,15 @@ error: ABIs are not compatible LL | type TestAbiNeSign = (fn(i32), fn(u32)); | ^^^^^^^^^^^^^^^^^^ -error: aborting due to 10 previous errors +error[E0277]: the size for values of type `str` cannot be known at compilation time + --> $DIR/debug.rs:53:1 + | +LL | type TestAbiEqNonsense = (fn((str, str)), fn((str, str))); + | ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `str` + = note: only the last element of a tuple may have a dynamically sized type + +error: aborting due to 11 previous errors +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/associated-types/issue-85103.rs b/tests/ui/associated-types/issue-85103.rs index 9c6a419e9f72d..77c9876ffa57f 100644 --- a/tests/ui/associated-types/issue-85103.rs +++ b/tests/ui/associated-types/issue-85103.rs @@ -4,6 +4,6 @@ use std::borrow::Cow; #[rustc_layout(debug)] type Edges<'a, E> = Cow<'a, [E]>; -//~^ 6:1: 6:18: unable to determine layout for `<[E] as ToOwned>::Owned` because `<[E] as ToOwned>::Owned` cannot be normalized +//~^ the trait bound `[E]: ToOwned` is not satisfied fn main() {} diff --git a/tests/ui/associated-types/issue-85103.stderr b/tests/ui/associated-types/issue-85103.stderr index 17f7148074cf3..302dec8dad1c0 100644 --- a/tests/ui/associated-types/issue-85103.stderr +++ b/tests/ui/associated-types/issue-85103.stderr @@ -1,8 +1,12 @@ -error: unable to determine layout for `<[E] as ToOwned>::Owned` because `<[E] as ToOwned>::Owned` cannot be normalized +error[E0277]: the trait bound `[E]: ToOwned` is not satisfied --> $DIR/issue-85103.rs:6:1 | LL | type Edges<'a, E> = Cow<'a, [E]>; - | ^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^ the trait `ToOwned` is not implemented for `[E]` + | +note: required by a bound in `Cow` + --> $SRC_DIR/alloc/src/borrow.rs:LL:COL error: aborting due to previous error +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/layout/debug.rs b/tests/ui/layout/debug.rs index 97dc73d3aa70c..65f2f3b89af1f 100644 --- a/tests/ui/layout/debug.rs +++ b/tests/ui/layout/debug.rs @@ -73,3 +73,6 @@ impl S { #[rustc_layout(debug)] const C: () = (); //~ ERROR: can only be applied to } + +#[rustc_layout(debug)] +type Impossible = (str, str); //~ ERROR: cannot be known at compilation time diff --git a/tests/ui/layout/debug.stderr b/tests/ui/layout/debug.stderr index 0973043c67814..0910db6912946 100644 --- a/tests/ui/layout/debug.stderr +++ b/tests/ui/layout/debug.stderr @@ -563,5 +563,15 @@ error: `#[rustc_layout]` can only be applied to `struct`/`enum`/`union` declarat LL | const C: () = (); | ^^^^^^^^^^^ -error: aborting due to 16 previous errors +error[E0277]: the size for values of type `str` cannot be known at compilation time + --> $DIR/debug.rs:78:1 + | +LL | type Impossible = (str, str); + | ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `str` + = note: only the last element of a tuple may have a dynamically sized type + +error: aborting due to 17 previous errors +For more information about this error, try `rustc --explain E0277`. From c2a7e684cd43f449bc40a696dbf9b69ebccc33c3 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 9 Sep 2023 17:39:53 +0200 Subject: [PATCH 2/3] use hir_crate_items(()).definitions() instead of hir().items() --- compiler/rustc_passes/src/abi_test.rs | 43 ++---- compiler/rustc_passes/src/layout_test.rs | 16 +- tests/ui/abi/debug.stderr | 188 +++++++++++------------ tests/ui/layout/debug.stderr | 12 +- 4 files changed, 118 insertions(+), 141 deletions(-) diff --git a/compiler/rustc_passes/src/abi_test.rs b/compiler/rustc_passes/src/abi_test.rs index 689f083217bbc..240999b91b747 100644 --- a/compiler/rustc_passes/src/abi_test.rs +++ b/compiler/rustc_passes/src/abi_test.rs @@ -1,6 +1,6 @@ use rustc_ast::Attribute; use rustc_hir::def::DefKind; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::LocalDefId; use rustc_middle::ty::layout::{FnAbiError, LayoutError}; use rustc_middle::ty::{self, GenericArgs, Instance, Ty, TyCtxt}; use rustc_span::source_map::Spanned; @@ -15,32 +15,17 @@ pub fn test_abi(tcx: TyCtxt<'_>) { // if the `rustc_attrs` feature is not enabled, don't bother testing ABI return; } - for id in tcx.hir().items() { - for attr in tcx.get_attrs(id.owner_id, sym::rustc_abi) { - match tcx.def_kind(id.owner_id) { - DefKind::Fn => { - dump_abi_of_fn_item(tcx, id.owner_id.def_id.into(), attr); + for id in tcx.hir_crate_items(()).definitions() { + for attr in tcx.get_attrs(id, sym::rustc_abi) { + match tcx.def_kind(id) { + DefKind::Fn | DefKind::AssocFn => { + dump_abi_of_fn_item(tcx, id, attr); } DefKind::TyAlias { .. } => { - dump_abi_of_fn_type(tcx, id.owner_id.def_id.into(), attr); + dump_abi_of_fn_type(tcx, id, attr); } _ => { - tcx.sess.emit_err(AbiInvalidAttribute { span: tcx.def_span(id.owner_id) }); - } - } - } - if matches!(tcx.def_kind(id.owner_id), DefKind::Impl { .. }) { - // To find associated functions we need to go into the child items here. - for &id in tcx.associated_item_def_ids(id.owner_id) { - for attr in tcx.get_attrs(id, sym::rustc_abi) { - match tcx.def_kind(id) { - DefKind::AssocFn => { - dump_abi_of_fn_item(tcx, id, attr); - } - _ => { - tcx.sess.emit_err(AbiInvalidAttribute { span: tcx.def_span(id) }); - } - } + tcx.sess.emit_err(AbiInvalidAttribute { span: tcx.def_span(id) }); } } } @@ -50,7 +35,7 @@ pub fn test_abi(tcx: TyCtxt<'_>) { fn unwrap_fn_abi<'tcx>( abi: Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>>, tcx: TyCtxt<'tcx>, - item_def_id: DefId, + item_def_id: LocalDefId, ) -> &'tcx FnAbi<'tcx, Ty<'tcx>> { match abi { Ok(abi) => abi, @@ -72,10 +57,10 @@ fn unwrap_fn_abi<'tcx>( } } -fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) { +fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) { let param_env = tcx.param_env(item_def_id); let args = GenericArgs::identity_for_item(tcx, item_def_id); - let instance = match Instance::resolve(tcx, param_env, item_def_id, args) { + let instance = match Instance::resolve(tcx, param_env, item_def_id.into(), args) { Ok(Some(instance)) => instance, Ok(None) => { // Not sure what to do here, but `LayoutError::Unknown` seems reasonable? @@ -100,7 +85,7 @@ fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) { for meta_item in meta_items { match meta_item.name_or_empty() { sym::debug => { - let fn_name = tcx.item_name(item_def_id); + let fn_name = tcx.item_name(item_def_id.into()); tcx.sess.emit_err(AbiOf { span: tcx.def_span(item_def_id), fn_name, @@ -129,7 +114,7 @@ fn test_abi_eq<'tcx>(abi1: &'tcx FnAbi<'tcx, Ty<'tcx>>, abi2: &'tcx FnAbi<'tcx, && abi1.args.iter().zip(abi2.args.iter()).all(|(arg1, arg2)| arg1.eq_abi(arg2)) } -fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) { +fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) { let param_env = tcx.param_env(item_def_id); let ty = tcx.type_of(item_def_id).instantiate_identity(); let span = tcx.def_span(item_def_id); @@ -152,7 +137,7 @@ fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) { item_def_id, ); - let fn_name = tcx.item_name(item_def_id); + let fn_name = tcx.item_name(item_def_id.into()); tcx.sess.emit_err(AbiOf { span, fn_name, fn_abi: format!("{:#?}", abi) }); } sym::assert_eq => { diff --git a/compiler/rustc_passes/src/layout_test.rs b/compiler/rustc_passes/src/layout_test.rs index 6dacac150d34f..6403effe0276c 100644 --- a/compiler/rustc_passes/src/layout_test.rs +++ b/compiler/rustc_passes/src/layout_test.rs @@ -20,21 +20,13 @@ pub fn test_layout(tcx: TyCtxt<'_>) { // if the `rustc_attrs` feature is not enabled, don't bother testing layout return; } - for id in tcx.hir().items() { - for attr in tcx.get_attrs(id.owner_id, sym::rustc_layout) { - match tcx.def_kind(id.owner_id) { + for id in tcx.hir_crate_items(()).definitions() { + for attr in tcx.get_attrs(id, sym::rustc_layout) { + match tcx.def_kind(id) { DefKind::TyAlias { .. } | DefKind::Enum | DefKind::Struct | DefKind::Union => { - dump_layout_of(tcx, id.owner_id.def_id, attr); + dump_layout_of(tcx, id, attr); } _ => { - tcx.sess.emit_err(LayoutInvalidAttribute { span: tcx.def_span(id.owner_id) }); - } - } - } - if matches!(tcx.def_kind(id.owner_id), DefKind::Impl { .. }) { - // To find associated functions we need to go into the child items here. - for &id in tcx.associated_item_def_ids(id.owner_id) { - for _attr in tcx.get_attrs(id, sym::rustc_layout) { tcx.sess.emit_err(LayoutInvalidAttribute { span: tcx.def_span(id) }); } } diff --git a/tests/ui/abi/debug.stderr b/tests/ui/abi/debug.stderr index 0ca74fdda355d..c0c808c1edeae 100644 --- a/tests/ui/abi/debug.stderr +++ b/tests/ui/abi/debug.stderr @@ -268,100 +268,6 @@ error: `#[rustc_abi]` can only be applied to function items, type aliases, and a LL | const C: () = (); | ^^^^^^^^^^^ -error: `#[rustc_abi]` can only be applied to function items, type aliases, and associated functions - --> $DIR/debug.rs:28:5 - | -LL | const C: () = (); - | ^^^^^^^^^^^ - -error: fn_abi_of(assoc_test) = FnAbi { - args: [ - ArgAbi { - layout: TyAndLayout { - ty: &S, - layout: Layout { - size: $SOME_SIZE, - align: AbiAndPrefAlign { - abi: $SOME_ALIGN, - pref: $SOME_ALIGN, - }, - abi: Scalar( - Initialized { - value: Pointer( - AddressSpace( - 0, - ), - ), - valid_range: $NON_NULL, - }, - ), - fields: Primitive, - largest_niche: Some( - Niche { - offset: Size(0 bytes), - value: Pointer( - AddressSpace( - 0, - ), - ), - valid_range: $NON_NULL, - }, - ), - variants: Single { - index: 0, - }, - max_repr_align: None, - unadjusted_abi_align: $SOME_ALIGN, - }, - }, - mode: Direct( - ArgAttributes { - regular: NoAlias | NonNull | ReadOnly | NoUndef, - arg_ext: None, - pointee_size: Size(2 bytes), - pointee_align: Some( - Align(2 bytes), - ), - }, - ), - }, - ], - ret: ArgAbi { - layout: TyAndLayout { - ty: (), - layout: Layout { - size: Size(0 bytes), - align: AbiAndPrefAlign { - abi: $SOME_ALIGN, - pref: $SOME_ALIGN, - }, - abi: Aggregate { - sized: true, - }, - fields: Arbitrary { - offsets: [], - memory_index: [], - }, - largest_niche: None, - variants: Single { - index: 0, - }, - max_repr_align: None, - unadjusted_abi_align: $SOME_ALIGN, - }, - }, - mode: Ignore, - }, - c_variadic: false, - fixed_count: 1, - conv: Rust, - can_unwind: $SOME_BOOL, - } - --> $DIR/debug.rs:33:5 - | -LL | fn assoc_test(&self) { } - | ^^^^^^^^^^^^^^^^^^^^ - error: ABIs are not compatible left ABI = FnAbi { args: [ @@ -954,6 +860,100 @@ LL | type TestAbiEqNonsense = (fn((str, str)), fn((str, str))); = help: the trait `Sized` is not implemented for `str` = note: only the last element of a tuple may have a dynamically sized type +error: `#[rustc_abi]` can only be applied to function items, type aliases, and associated functions + --> $DIR/debug.rs:28:5 + | +LL | const C: () = (); + | ^^^^^^^^^^^ + +error: fn_abi_of(assoc_test) = FnAbi { + args: [ + ArgAbi { + layout: TyAndLayout { + ty: &S, + layout: Layout { + size: $SOME_SIZE, + align: AbiAndPrefAlign { + abi: $SOME_ALIGN, + pref: $SOME_ALIGN, + }, + abi: Scalar( + Initialized { + value: Pointer( + AddressSpace( + 0, + ), + ), + valid_range: $NON_NULL, + }, + ), + fields: Primitive, + largest_niche: Some( + Niche { + offset: Size(0 bytes), + value: Pointer( + AddressSpace( + 0, + ), + ), + valid_range: $NON_NULL, + }, + ), + variants: Single { + index: 0, + }, + max_repr_align: None, + unadjusted_abi_align: $SOME_ALIGN, + }, + }, + mode: Direct( + ArgAttributes { + regular: NoAlias | NonNull | ReadOnly | NoUndef, + arg_ext: None, + pointee_size: Size(2 bytes), + pointee_align: Some( + Align(2 bytes), + ), + }, + ), + }, + ], + ret: ArgAbi { + layout: TyAndLayout { + ty: (), + layout: Layout { + size: Size(0 bytes), + align: AbiAndPrefAlign { + abi: $SOME_ALIGN, + pref: $SOME_ALIGN, + }, + abi: Aggregate { + sized: true, + }, + fields: Arbitrary { + offsets: [], + memory_index: [], + }, + largest_niche: None, + variants: Single { + index: 0, + }, + max_repr_align: None, + unadjusted_abi_align: $SOME_ALIGN, + }, + }, + mode: Ignore, + }, + c_variadic: false, + fixed_count: 1, + conv: Rust, + can_unwind: $SOME_BOOL, + } + --> $DIR/debug.rs:33:5 + | +LL | fn assoc_test(&self) { } + | ^^^^^^^^^^^^^^^^^^^^ + error: aborting due to 11 previous errors For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/layout/debug.stderr b/tests/ui/layout/debug.stderr index 0910db6912946..59df10b737b7d 100644 --- a/tests/ui/layout/debug.stderr +++ b/tests/ui/layout/debug.stderr @@ -557,12 +557,6 @@ error: `#[rustc_layout]` can only be applied to `struct`/`enum`/`union` declarat LL | const C: () = (); | ^^^^^^^^^^^ -error: `#[rustc_layout]` can only be applied to `struct`/`enum`/`union` declarations and type aliases - --> $DIR/debug.rs:74:5 - | -LL | const C: () = (); - | ^^^^^^^^^^^ - error[E0277]: the size for values of type `str` cannot be known at compilation time --> $DIR/debug.rs:78:1 | @@ -572,6 +566,12 @@ LL | type Impossible = (str, str); = help: the trait `Sized` is not implemented for `str` = note: only the last element of a tuple may have a dynamically sized type +error: `#[rustc_layout]` can only be applied to `struct`/`enum`/`union` declarations and type aliases + --> $DIR/debug.rs:74:5 + | +LL | const C: () = (); + | ^^^^^^^^^^^ + error: aborting due to 17 previous errors For more information about this error, try `rustc --explain E0277`. From 3bd8bcb8bb431462858d71b94786fe90cfa7faed Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 9 Sep 2023 17:43:20 +0200 Subject: [PATCH 3/3] better spans for WF errors --- compiler/rustc_passes/src/abi_test.rs | 2 +- compiler/rustc_passes/src/layout_test.rs | 9 +++++++-- tests/ui/abi/debug.stderr | 4 ++-- ...ssue-85103.rs => issue-85103-layout-debug.rs} | 0 .../issue-85103-layout-debug.stderr | 16 ++++++++++++++++ tests/ui/associated-types/issue-85103.stderr | 12 ------------ tests/ui/layout/debug.stderr | 4 ++-- 7 files changed, 28 insertions(+), 19 deletions(-) rename tests/ui/associated-types/{issue-85103.rs => issue-85103-layout-debug.rs} (100%) create mode 100644 tests/ui/associated-types/issue-85103-layout-debug.stderr delete mode 100644 tests/ui/associated-types/issue-85103.stderr diff --git a/compiler/rustc_passes/src/abi_test.rs b/compiler/rustc_passes/src/abi_test.rs index 240999b91b747..6a946b552130b 100644 --- a/compiler/rustc_passes/src/abi_test.rs +++ b/compiler/rustc_passes/src/abi_test.rs @@ -118,7 +118,7 @@ fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribut let param_env = tcx.param_env(item_def_id); let ty = tcx.type_of(item_def_id).instantiate_identity(); let span = tcx.def_span(item_def_id); - if !ensure_wf(tcx, param_env, ty, span) { + if !ensure_wf(tcx, param_env, ty, item_def_id, span) { return; } let meta_items = attr.meta_item_list().unwrap_or_default(); diff --git a/compiler/rustc_passes/src/layout_test.rs b/compiler/rustc_passes/src/layout_test.rs index 6403effe0276c..3e0dc1f6063ff 100644 --- a/compiler/rustc_passes/src/layout_test.rs +++ b/compiler/rustc_passes/src/layout_test.rs @@ -38,12 +38,17 @@ pub fn ensure_wf<'tcx>( tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: Ty<'tcx>, + def_id: LocalDefId, span: Span, ) -> bool { let pred = ty::ClauseKind::WellFormed(ty.into()); let obligation = traits::Obligation::new( tcx, - traits::ObligationCause::dummy_with_span(span), + traits::ObligationCause::new( + span, + def_id, + traits::ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Ty(def_id))), + ), param_env, pred, ); @@ -64,7 +69,7 @@ fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) { let param_env = tcx.param_env(item_def_id); let ty = tcx.type_of(item_def_id).instantiate_identity(); let span = tcx.def_span(item_def_id.to_def_id()); - if !ensure_wf(tcx, param_env, ty, span) { + if !ensure_wf(tcx, param_env, ty, item_def_id, span) { return; } match tcx.layout_of(param_env.and(ty)) { diff --git a/tests/ui/abi/debug.stderr b/tests/ui/abi/debug.stderr index c0c808c1edeae..ceaf5136a6f55 100644 --- a/tests/ui/abi/debug.stderr +++ b/tests/ui/abi/debug.stderr @@ -852,10 +852,10 @@ LL | type TestAbiNeSign = (fn(i32), fn(u32)); | ^^^^^^^^^^^^^^^^^^ error[E0277]: the size for values of type `str` cannot be known at compilation time - --> $DIR/debug.rs:53:1 + --> $DIR/debug.rs:53:46 | LL | type TestAbiEqNonsense = (fn((str, str)), fn((str, str))); - | ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | ^^^^^^^^^^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `str` = note: only the last element of a tuple may have a dynamically sized type diff --git a/tests/ui/associated-types/issue-85103.rs b/tests/ui/associated-types/issue-85103-layout-debug.rs similarity index 100% rename from tests/ui/associated-types/issue-85103.rs rename to tests/ui/associated-types/issue-85103-layout-debug.rs diff --git a/tests/ui/associated-types/issue-85103-layout-debug.stderr b/tests/ui/associated-types/issue-85103-layout-debug.stderr new file mode 100644 index 0000000000000..0bdea10ba474a --- /dev/null +++ b/tests/ui/associated-types/issue-85103-layout-debug.stderr @@ -0,0 +1,16 @@ +error[E0277]: the trait bound `[E]: ToOwned` is not satisfied + --> $DIR/issue-85103-layout-debug.rs:6:21 + | +LL | type Edges<'a, E> = Cow<'a, [E]>; + | ^^^^^^^^^^^^ the trait `ToOwned` is not implemented for `[E]` + | +note: required by a bound in `Cow` + --> $SRC_DIR/alloc/src/borrow.rs:LL:COL +help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement + | +LL | type Edges<'a, E> where [E]: ToOwned = Cow<'a, [E]>; + | ++++++++++++++++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/associated-types/issue-85103.stderr b/tests/ui/associated-types/issue-85103.stderr deleted file mode 100644 index 302dec8dad1c0..0000000000000 --- a/tests/ui/associated-types/issue-85103.stderr +++ /dev/null @@ -1,12 +0,0 @@ -error[E0277]: the trait bound `[E]: ToOwned` is not satisfied - --> $DIR/issue-85103.rs:6:1 - | -LL | type Edges<'a, E> = Cow<'a, [E]>; - | ^^^^^^^^^^^^^^^^^ the trait `ToOwned` is not implemented for `[E]` - | -note: required by a bound in `Cow` - --> $SRC_DIR/alloc/src/borrow.rs:LL:COL - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/layout/debug.stderr b/tests/ui/layout/debug.stderr index 59df10b737b7d..1ba70ae39e71b 100644 --- a/tests/ui/layout/debug.stderr +++ b/tests/ui/layout/debug.stderr @@ -558,10 +558,10 @@ LL | const C: () = (); | ^^^^^^^^^^^ error[E0277]: the size for values of type `str` cannot be known at compilation time - --> $DIR/debug.rs:78:1 + --> $DIR/debug.rs:78:19 | LL | type Impossible = (str, str); - | ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | ^^^^^^^^^^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `str` = note: only the last element of a tuple may have a dynamically sized type