From a4b7ed7a42c716ab9f05e351f003d589124fd55d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:18:58 +0200 Subject: Adding upstream version 1.68.2+dfsg1. Signed-off-by: Daniel Baumann --- tests/codegen-units/item-collection/asm-sym.rs | 20 +++++ .../auxiliary/cgu_export_trait_method.rs | 24 ++++++ .../auxiliary/cgu_extern_closures.rs | 23 ++++++ .../auxiliary/cgu_generic_function.rs | 26 +++++++ .../item-collection/cross-crate-closures.rs | 33 ++++++++ .../cross-crate-generic-functions.rs | 24 ++++++ .../item-collection/cross-crate-trait-method.rs | 50 ++++++++++++ .../item-collection/drop_in_place_intrinsic.rs | 36 +++++++++ .../item-collection/function-as-argument.rs | 40 ++++++++++ .../item-collection/generic-drop-glue.rs | 91 ++++++++++++++++++++++ .../item-collection/generic-functions.rs | 55 +++++++++++++ .../codegen-units/item-collection/generic-impl.rs | 71 +++++++++++++++++ .../impl-in-non-instantiated-generic.rs | 27 +++++++ .../item-collection/implicit-panic-call.rs | 58 ++++++++++++++ .../instantiation-through-vtable.rs | 38 +++++++++ .../item-collection/items-within-generic-items.rs | 35 +++++++++ .../item-collection/non-generic-closures.rs | 60 ++++++++++++++ .../item-collection/non-generic-drop-glue.rs | 49 ++++++++++++ .../item-collection/non-generic-functions.rs | 72 +++++++++++++++++ .../item-collection/overloaded-operators.rs | 59 ++++++++++++++ tests/codegen-units/item-collection/static-init.rs | 16 ++++ .../item-collection/statics-and-consts.rs | 54 +++++++++++++ .../item-collection/trait-implementations.rs | 73 +++++++++++++++++ .../item-collection/trait-method-as-argument.rs | 60 ++++++++++++++ .../item-collection/trait-method-default-impl.rs | 61 +++++++++++++++ .../item-collection/transitive-drop-glue.rs | 46 +++++++++++ .../item-collection/tuple-drop-glue.rs | 27 +++++++ .../item-collection/unreferenced-const-fn.rs | 9 +++ .../unreferenced-inline-function.rs | 11 +++ tests/codegen-units/item-collection/unsizing.rs | 79 +++++++++++++++++++ .../item-collection/unused-traits-and-generics.rs | 77 ++++++++++++++++++ 31 files changed, 1404 insertions(+) create mode 100644 tests/codegen-units/item-collection/asm-sym.rs create mode 100644 tests/codegen-units/item-collection/auxiliary/cgu_export_trait_method.rs create mode 100644 tests/codegen-units/item-collection/auxiliary/cgu_extern_closures.rs create mode 100644 tests/codegen-units/item-collection/auxiliary/cgu_generic_function.rs create mode 100644 tests/codegen-units/item-collection/cross-crate-closures.rs create mode 100644 tests/codegen-units/item-collection/cross-crate-generic-functions.rs create mode 100644 tests/codegen-units/item-collection/cross-crate-trait-method.rs create mode 100644 tests/codegen-units/item-collection/drop_in_place_intrinsic.rs create mode 100644 tests/codegen-units/item-collection/function-as-argument.rs create mode 100644 tests/codegen-units/item-collection/generic-drop-glue.rs create mode 100644 tests/codegen-units/item-collection/generic-functions.rs create mode 100644 tests/codegen-units/item-collection/generic-impl.rs create mode 100644 tests/codegen-units/item-collection/impl-in-non-instantiated-generic.rs create mode 100644 tests/codegen-units/item-collection/implicit-panic-call.rs create mode 100644 tests/codegen-units/item-collection/instantiation-through-vtable.rs create mode 100644 tests/codegen-units/item-collection/items-within-generic-items.rs create mode 100644 tests/codegen-units/item-collection/non-generic-closures.rs create mode 100644 tests/codegen-units/item-collection/non-generic-drop-glue.rs create mode 100644 tests/codegen-units/item-collection/non-generic-functions.rs create mode 100644 tests/codegen-units/item-collection/overloaded-operators.rs create mode 100644 tests/codegen-units/item-collection/static-init.rs create mode 100644 tests/codegen-units/item-collection/statics-and-consts.rs create mode 100644 tests/codegen-units/item-collection/trait-implementations.rs create mode 100644 tests/codegen-units/item-collection/trait-method-as-argument.rs create mode 100644 tests/codegen-units/item-collection/trait-method-default-impl.rs create mode 100644 tests/codegen-units/item-collection/transitive-drop-glue.rs create mode 100644 tests/codegen-units/item-collection/tuple-drop-glue.rs create mode 100644 tests/codegen-units/item-collection/unreferenced-const-fn.rs create mode 100644 tests/codegen-units/item-collection/unreferenced-inline-function.rs create mode 100644 tests/codegen-units/item-collection/unsizing.rs create mode 100644 tests/codegen-units/item-collection/unused-traits-and-generics.rs (limited to 'tests/codegen-units/item-collection') diff --git a/tests/codegen-units/item-collection/asm-sym.rs b/tests/codegen-units/item-collection/asm-sym.rs new file mode 100644 index 000000000..8bafb95bc --- /dev/null +++ b/tests/codegen-units/item-collection/asm-sym.rs @@ -0,0 +1,20 @@ +// needs-asm-support +// compile-flags: -Ccodegen-units=1 -Zprint-mono-items=lazy --crate-type=lib + +#[inline(always)] +pub unsafe fn f() { + //~ MONO_ITEM static f::S @@ asm_sym-cgu.0[External] + static S: usize = 1; + //~ MONO_ITEM fn f::fun @@ asm_sym-cgu.0[External] + fn fun() {} + core::arch::asm!("/* {0} {1} */", sym S, sym fun); +} + +//~ MONO_ITEM fn g @@ asm_sym-cgu.0[External] +pub unsafe fn g() { + //~ MONO_ITEM static g::S @@ asm_sym-cgu.0[Internal] + static S: usize = 2; + //~ MONO_ITEM fn g::fun @@ asm_sym-cgu.0[Internal] + fn fun() {} + core::arch::asm!("/* {0} {1} */", sym S, sym fun); +} diff --git a/tests/codegen-units/item-collection/auxiliary/cgu_export_trait_method.rs b/tests/codegen-units/item-collection/auxiliary/cgu_export_trait_method.rs new file mode 100644 index 000000000..ecea26dc4 --- /dev/null +++ b/tests/codegen-units/item-collection/auxiliary/cgu_export_trait_method.rs @@ -0,0 +1,24 @@ +#![crate_type = "lib"] + +pub trait Trait : Sized { + fn without_self() -> u32; + fn without_self_default() -> u32 { 0 } + + fn with_default_impl(self) -> Self { self } + fn with_default_impl_generic(self, x: T) -> (Self, T) { (self, x) } + + fn without_default_impl(x: u32) -> (Self, u32); + fn without_default_impl_generic(x: T) -> (Self, T); +} + +impl Trait for char { + fn without_self() -> u32 { 2 } + fn without_default_impl(x: u32) -> (Self, u32) { ('c', x) } + fn without_default_impl_generic(x: T) -> (Self, T) { ('c', x) } +} + +impl Trait for u32 { + fn without_self() -> u32 { 1 } + fn without_default_impl(x: u32) -> (Self, u32) { (0, x) } + fn without_default_impl_generic(x: T) -> (Self, T) { (0, x) } +} diff --git a/tests/codegen-units/item-collection/auxiliary/cgu_extern_closures.rs b/tests/codegen-units/item-collection/auxiliary/cgu_extern_closures.rs new file mode 100644 index 000000000..05ea0a89f --- /dev/null +++ b/tests/codegen-units/item-collection/auxiliary/cgu_extern_closures.rs @@ -0,0 +1,23 @@ +#![crate_type = "lib"] + +#[inline] +pub fn inlined_fn(x: i32, y: i32) -> i32 { + + let closure = |a, b| { a + b }; + + closure(x, y) +} + +pub fn inlined_fn_generic(x: i32, y: i32, z: T) -> (i32, T) { + + let closure = |a, b| { a + b }; + + (closure(x, y), z) +} + +pub fn non_inlined_fn(x: i32, y: i32) -> i32 { + + let closure = |a, b| { a + b }; + + closure(x, y) +} diff --git a/tests/codegen-units/item-collection/auxiliary/cgu_generic_function.rs b/tests/codegen-units/item-collection/auxiliary/cgu_generic_function.rs new file mode 100644 index 000000000..3926f2957 --- /dev/null +++ b/tests/codegen-units/item-collection/auxiliary/cgu_generic_function.rs @@ -0,0 +1,26 @@ +#![crate_type = "lib"] + +struct Struct(u32); + +#[inline(never)] +pub fn foo(x: T) -> (T, u32, i8) { + let (x, Struct(y)) = bar(x); + (x, y, 2) +} + +#[inline(never)] +fn bar(x: T) -> (T, Struct) { + let _ = not_exported_and_not_generic(0); + (x, Struct(1)) +} + +// These should not contribute to the codegen items of other crates. +#[inline(never)] +pub fn exported_but_not_generic(x: i32) -> i64 { + x as i64 +} + +#[inline(never)] +fn not_exported_and_not_generic(x: u32) -> u64 { + x as u64 +} diff --git a/tests/codegen-units/item-collection/cross-crate-closures.rs b/tests/codegen-units/item-collection/cross-crate-closures.rs new file mode 100644 index 000000000..6af344fab --- /dev/null +++ b/tests/codegen-units/item-collection/cross-crate-closures.rs @@ -0,0 +1,33 @@ +// In the current version of the collector that still has to support +// legacy-codegen, closures do not generate their own MonoItems, so we are +// ignoring this test until MIR codegen has taken over completely +// ignore-test + +// compile-flags:-Zprint-mono-items=eager + +#![deny(dead_code)] +#![feature(start)] + +// aux-build:cgu_extern_closures.rs +extern crate cgu_extern_closures; + +//~ MONO_ITEM fn cross_crate_closures::start[0] +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + + //~ MONO_ITEM fn cgu_extern_closures::inlined_fn[0] + //~ MONO_ITEM fn cgu_extern_closures::inlined_fn[0]::{{closure}}[0] + let _ = cgu_extern_closures::inlined_fn(1, 2); + + //~ MONO_ITEM fn cgu_extern_closures::inlined_fn_generic[0] + //~ MONO_ITEM fn cgu_extern_closures::inlined_fn_generic[0]::{{closure}}[0] + let _ = cgu_extern_closures::inlined_fn_generic(3, 4, 5i32); + + // Nothing should be generated for this call, we just link to the instance + // in the extern crate. + let _ = cgu_extern_closures::non_inlined_fn(6, 7); + + 0 +} + +//~ MONO_ITEM drop-glue i8 diff --git a/tests/codegen-units/item-collection/cross-crate-generic-functions.rs b/tests/codegen-units/item-collection/cross-crate-generic-functions.rs new file mode 100644 index 000000000..7289ceee9 --- /dev/null +++ b/tests/codegen-units/item-collection/cross-crate-generic-functions.rs @@ -0,0 +1,24 @@ +// compile-flags:-Zprint-mono-items=eager + +#![deny(dead_code)] +#![feature(start)] + +// aux-build:cgu_generic_function.rs +extern crate cgu_generic_function; + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + //~ MONO_ITEM fn cgu_generic_function::bar:: + //~ MONO_ITEM fn cgu_generic_function::foo:: + let _ = cgu_generic_function::foo(1u32); + + //~ MONO_ITEM fn cgu_generic_function::bar:: + //~ MONO_ITEM fn cgu_generic_function::foo:: + let _ = cgu_generic_function::foo(2u64); + + // This should not introduce a codegen item + let _ = cgu_generic_function::exported_but_not_generic(3); + + 0 +} diff --git a/tests/codegen-units/item-collection/cross-crate-trait-method.rs b/tests/codegen-units/item-collection/cross-crate-trait-method.rs new file mode 100644 index 000000000..dc0984c8a --- /dev/null +++ b/tests/codegen-units/item-collection/cross-crate-trait-method.rs @@ -0,0 +1,50 @@ +// compile-flags:-Zprint-mono-items=eager + +#![deny(dead_code)] +#![feature(start)] + +// aux-build:cgu_export_trait_method.rs +extern crate cgu_export_trait_method; + +use cgu_export_trait_method::Trait; + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + // The object code of these methods is contained in the external crate, so + // calling them should *not* introduce codegen items in the current crate. + let _: (u32, u32) = Trait::without_default_impl(0); + let _: (char, u32) = Trait::without_default_impl(0); + + // Currently, no object code is generated for trait methods with default + // implementations, unless they are actually called from somewhere. Therefore + // we cannot import the implementations and have to create our own inline. + //~ MONO_ITEM fn ::with_default_impl + let _ = Trait::with_default_impl(0u32); + //~ MONO_ITEM fn ::with_default_impl + let _ = Trait::with_default_impl('c'); + + + + //~ MONO_ITEM fn ::with_default_impl_generic::<&str> + let _ = Trait::with_default_impl_generic(0u32, "abc"); + //~ MONO_ITEM fn ::with_default_impl_generic:: + let _ = Trait::with_default_impl_generic(0u32, false); + + //~ MONO_ITEM fn ::with_default_impl_generic:: + let _ = Trait::with_default_impl_generic('x', 1i16); + //~ MONO_ITEM fn ::with_default_impl_generic:: + let _ = Trait::with_default_impl_generic('y', 0i32); + + //~ MONO_ITEM fn ::without_default_impl_generic:: + let _: (u32, char) = Trait::without_default_impl_generic('c'); + //~ MONO_ITEM fn ::without_default_impl_generic:: + let _: (u32, bool) = Trait::without_default_impl_generic(false); + + //~ MONO_ITEM fn ::without_default_impl_generic:: + let _: (char, char) = Trait::without_default_impl_generic('c'); + //~ MONO_ITEM fn ::without_default_impl_generic:: + let _: (char, bool) = Trait::without_default_impl_generic(false); + + 0 +} diff --git a/tests/codegen-units/item-collection/drop_in_place_intrinsic.rs b/tests/codegen-units/item-collection/drop_in_place_intrinsic.rs new file mode 100644 index 000000000..a3f1fb5e7 --- /dev/null +++ b/tests/codegen-units/item-collection/drop_in_place_intrinsic.rs @@ -0,0 +1,36 @@ +// +// compile-flags:-Zprint-mono-items=eager +// compile-flags:-Zinline-in-all-cgus + +#![feature(start)] + +//~ MONO_ITEM fn std::ptr::drop_in_place:: - shim(Some(StructWithDtor)) @@ drop_in_place_intrinsic-cgu.0[Internal] +struct StructWithDtor(u32); + +impl Drop for StructWithDtor { + //~ MONO_ITEM fn ::drop + fn drop(&mut self) {} +} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + + //~ MONO_ITEM fn std::ptr::drop_in_place::<[StructWithDtor; 2]> - shim(Some([StructWithDtor; 2])) @@ drop_in_place_intrinsic-cgu.0[Internal] + let x = [StructWithDtor(0), StructWithDtor(1)]; + + drop_slice_in_place(&x); + + 0 +} + +//~ MONO_ITEM fn drop_slice_in_place +fn drop_slice_in_place(x: &[StructWithDtor]) { + unsafe { + // This is the interesting thing in this test case: Normally we would + // not have drop-glue for the unsized [StructWithDtor]. This has to be + // generated though when the drop_in_place() intrinsic is used. + //~ MONO_ITEM fn std::ptr::drop_in_place::<[StructWithDtor]> - shim(Some([StructWithDtor])) @@ drop_in_place_intrinsic-cgu.0[Internal] + ::std::ptr::drop_in_place(x as *const _ as *mut [StructWithDtor]); + } +} diff --git a/tests/codegen-units/item-collection/function-as-argument.rs b/tests/codegen-units/item-collection/function-as-argument.rs new file mode 100644 index 000000000..ea500c311 --- /dev/null +++ b/tests/codegen-units/item-collection/function-as-argument.rs @@ -0,0 +1,40 @@ +// +// compile-flags:-Zprint-mono-items=eager + +#![deny(dead_code)] +#![feature(start)] + +fn take_fn_once(f: F, x: T1, y: T2) { + (f)(x, y) +} + +fn function(_: T1, _: T2) {} + +fn take_fn_pointer(f: fn(T1, T2), x: T1, y: T2) { + (f)(x, y) +} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + + //~ MONO_ITEM fn take_fn_once::}> + //~ MONO_ITEM fn function:: + //~ MONO_ITEM fn } as std::ops::FnOnce<(u32, &str)>>::call_once - shim(fn(u32, &str) {function::}) + take_fn_once(function, 0u32, "abc"); + + //~ MONO_ITEM fn take_fn_once::}> + //~ MONO_ITEM fn function:: + //~ MONO_ITEM fn } as std::ops::FnOnce<(char, f64)>>::call_once - shim(fn(char, f64) {function::}) + take_fn_once(function, 'c', 0f64); + + //~ MONO_ITEM fn take_fn_pointer:: + //~ MONO_ITEM fn function:: + take_fn_pointer(function, 0i32, ()); + + //~ MONO_ITEM fn take_fn_pointer:: + //~ MONO_ITEM fn function:: + take_fn_pointer(function, 0f32, 0i64); + + 0 +} diff --git a/tests/codegen-units/item-collection/generic-drop-glue.rs b/tests/codegen-units/item-collection/generic-drop-glue.rs new file mode 100644 index 000000000..6df4ff7e5 --- /dev/null +++ b/tests/codegen-units/item-collection/generic-drop-glue.rs @@ -0,0 +1,91 @@ +// +// compile-flags:-Zprint-mono-items=eager +// compile-flags:-Zinline-in-all-cgus + +#![deny(dead_code)] +#![feature(start)] + +struct StructWithDrop { + x: T1, + y: T2, +} + +impl Drop for StructWithDrop { + fn drop(&mut self) {} +} + +struct StructNoDrop { + x: T1, + y: T2, +} + +enum EnumWithDrop { + A(T1), + B(T2) +} + +impl Drop for EnumWithDrop { + fn drop(&mut self) {} +} + +enum EnumNoDrop { + A(T1), + B(T2) +} + + +struct NonGenericNoDrop(#[allow(unused_tuple_struct_fields)] i32); + +struct NonGenericWithDrop(#[allow(unused_tuple_struct_fields)] i32); +//~ MONO_ITEM fn std::ptr::drop_in_place:: - shim(Some(NonGenericWithDrop)) @@ generic_drop_glue-cgu.0[Internal] + +impl Drop for NonGenericWithDrop { + //~ MONO_ITEM fn ::drop + fn drop(&mut self) {} +} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + //~ MONO_ITEM fn std::ptr::drop_in_place::> - shim(Some(StructWithDrop)) @@ generic_drop_glue-cgu.0[Internal] + //~ MONO_ITEM fn as std::ops::Drop>::drop + let _ = StructWithDrop { x: 0i8, y: 'a' }.x; + + //~ MONO_ITEM fn std::ptr::drop_in_place::> - shim(Some(StructWithDrop<&str, NonGenericNoDrop>)) @@ generic_drop_glue-cgu.0[Internal] + //~ MONO_ITEM fn as std::ops::Drop>::drop + let _ = StructWithDrop { x: "&str", y: NonGenericNoDrop(0) }.y; + + // Should produce no drop glue + let _ = StructNoDrop { x: 'a', y: 0u32 }.x; + + // This is supposed to generate drop-glue because it contains a field that + // needs to be dropped. + //~ MONO_ITEM fn std::ptr::drop_in_place::> - shim(Some(StructNoDrop)) @@ generic_drop_glue-cgu.0[Internal] + let _ = StructNoDrop { x: NonGenericWithDrop(0), y: 0f64 }.y; + + //~ MONO_ITEM fn std::ptr::drop_in_place::> - shim(Some(EnumWithDrop)) @@ generic_drop_glue-cgu.0[Internal] + //~ MONO_ITEM fn as std::ops::Drop>::drop + let _ = match EnumWithDrop::A::(0) { + EnumWithDrop::A(x) => x, + EnumWithDrop::B(x) => x as i32 + }; + + //~ MONO_ITEM fn std::ptr::drop_in_place::> - shim(Some(EnumWithDrop)) @@ generic_drop_glue-cgu.0[Internal] + //~ MONO_ITEM fn as std::ops::Drop>::drop + let _ = match EnumWithDrop::B::(1.0) { + EnumWithDrop::A(x) => x, + EnumWithDrop::B(x) => x as f64 + }; + + let _ = match EnumNoDrop::A::(0) { + EnumNoDrop::A(x) => x, + EnumNoDrop::B(x) => x as i32 + }; + + let _ = match EnumNoDrop::B::(1.0) { + EnumNoDrop::A(x) => x, + EnumNoDrop::B(x) => x as f64 + }; + + 0 +} diff --git a/tests/codegen-units/item-collection/generic-functions.rs b/tests/codegen-units/item-collection/generic-functions.rs new file mode 100644 index 000000000..04383bb8e --- /dev/null +++ b/tests/codegen-units/item-collection/generic-functions.rs @@ -0,0 +1,55 @@ +// compile-flags:-Zprint-mono-items=eager + +#![deny(dead_code)] +#![feature(start)] + +fn foo1(a: T1) -> (T1, u32) { + (a, 1) +} + +fn foo2(a: T1, b: T2) -> (T1, T2) { + (a, b) +} + +fn foo3(a: T1, b: T2, c: T3) -> (T1, T2, T3) { + (a, b, c) +} + +// This function should be instantiated even if no used +//~ MONO_ITEM fn lifetime_only +pub fn lifetime_only<'a>(a: &'a u32) -> &'a u32 { + a +} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + //~ MONO_ITEM fn foo1:: + let _ = foo1(2i32); + //~ MONO_ITEM fn foo1:: + let _ = foo1(2i64); + //~ MONO_ITEM fn foo1::<&str> + let _ = foo1("abc"); + //~ MONO_ITEM fn foo1:: + let _ = foo1('v'); + + //~ MONO_ITEM fn foo2:: + let _ = foo2(2i32, 2i32); + //~ MONO_ITEM fn foo2:: + let _ = foo2(2i64, "abc"); + //~ MONO_ITEM fn foo2::<&str, usize> + let _ = foo2("a", 2usize); + //~ MONO_ITEM fn foo2:: + let _ = foo2('v', ()); + + //~ MONO_ITEM fn foo3:: + let _ = foo3(2i32, 2i32, 2i32); + //~ MONO_ITEM fn foo3:: + let _ = foo3(2i64, "abc", 'c'); + //~ MONO_ITEM fn foo3:: + let _ = foo3(0i16, "a", 2usize); + //~ MONO_ITEM fn foo3:: + let _ = foo3('v', (), ()); + + 0 +} diff --git a/tests/codegen-units/item-collection/generic-impl.rs b/tests/codegen-units/item-collection/generic-impl.rs new file mode 100644 index 000000000..4260230c2 --- /dev/null +++ b/tests/codegen-units/item-collection/generic-impl.rs @@ -0,0 +1,71 @@ +// compile-flags:-Zprint-mono-items=eager + +#![deny(dead_code)] +#![feature(start)] + +struct Struct { + x: T, + f: fn(x: T) -> T, +} + +fn id(x: T) -> T { x } + +impl Struct { + + fn new(x: T) -> Struct { + Struct { + x: x, + f: id + } + } + + fn get(self, x: T2) -> (T, T2) { + (self.x, x) + } +} + +pub struct LifeTimeOnly<'a> { + _a: &'a u32 +} + +impl<'a> LifeTimeOnly<'a> { + + //~ MONO_ITEM fn LifeTimeOnly::<'_>::foo + pub fn foo(&self) {} + //~ MONO_ITEM fn LifeTimeOnly::<'_>::bar + pub fn bar(&'a self) {} + //~ MONO_ITEM fn LifeTimeOnly::<'_>::baz + pub fn baz<'b>(&'b self) {} + + pub fn non_instantiated(&self) {} +} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + //~ MONO_ITEM fn Struct::::new + //~ MONO_ITEM fn id:: + //~ MONO_ITEM fn Struct::::get:: + let _ = Struct::new(0i32).get(0i16); + + //~ MONO_ITEM fn Struct::::new + //~ MONO_ITEM fn id:: + //~ MONO_ITEM fn Struct::::get:: + let _ = Struct::new(0i64).get(0i16); + + //~ MONO_ITEM fn Struct::::new + //~ MONO_ITEM fn id:: + //~ MONO_ITEM fn Struct::::get:: + let _ = Struct::new('c').get(0i16); + + //~ MONO_ITEM fn Struct::<&str>::new + //~ MONO_ITEM fn id::<&str> + //~ MONO_ITEM fn Struct::>::get:: + let _ = Struct::new(Struct::new("str")).get(0i16); + + //~ MONO_ITEM fn Struct::>::new + //~ MONO_ITEM fn id::> + let _ = (Struct::new(Struct::new("str")).f)(Struct::new("str")); + + 0 +} diff --git a/tests/codegen-units/item-collection/impl-in-non-instantiated-generic.rs b/tests/codegen-units/item-collection/impl-in-non-instantiated-generic.rs new file mode 100644 index 000000000..c01398eb2 --- /dev/null +++ b/tests/codegen-units/item-collection/impl-in-non-instantiated-generic.rs @@ -0,0 +1,27 @@ +// compile-flags:-Zprint-mono-items=eager + +#![deny(dead_code)] +#![feature(start)] + +trait SomeTrait { + fn foo(&self); +} + +// This function is never instantiated but the contained impl must still be +// discovered. +pub fn generic_function(x: T) -> (T, i32) { + impl SomeTrait for i64 { + //~ MONO_ITEM fn generic_function::::foo + fn foo(&self) {} + } + + (x, 0) +} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + 0i64.foo(); + + 0 +} diff --git a/tests/codegen-units/item-collection/implicit-panic-call.rs b/tests/codegen-units/item-collection/implicit-panic-call.rs new file mode 100644 index 000000000..abec7ad50 --- /dev/null +++ b/tests/codegen-units/item-collection/implicit-panic-call.rs @@ -0,0 +1,58 @@ +// compile-flags:-Zprint-mono-items=lazy + +// rust-lang/rust#90405 +// Ensure implicit panic calls are collected + +#![feature(lang_items)] +#![feature(no_core)] +#![crate_type = "lib"] +#![no_core] +#![no_std] + +#[lang = "panic_location"] +struct Location<'a> { + _file: &'a str, + _line: u32, + _col: u32, +} + +#[lang = "panic"] +#[inline] +#[track_caller] +fn panic(_: &'static str) -> ! { + loop {} +} + +#[lang = "sized"] +trait Sized {} + +#[lang = "copy"] +trait Copy {} + +#[lang = "freeze"] +trait Freeze {} + +impl Copy for i32 {} + +#[lang = "div"] +trait Div { + type Output; + fn div(self, rhs: Rhs) -> Self::Output; +} + +impl Div for i32 { + type Output = i32; + fn div(self, rhs: i32) -> i32 { + self / rhs + } +} + +#[allow(unconditional_panic)] +pub fn foo() { + // This implicitly generates a panic call. + let _ = 1 / 0; +} + +//~ MONO_ITEM fn foo +//~ MONO_ITEM fn ::div +//~ MONO_ITEM fn panic diff --git a/tests/codegen-units/item-collection/instantiation-through-vtable.rs b/tests/codegen-units/item-collection/instantiation-through-vtable.rs new file mode 100644 index 000000000..e78226d40 --- /dev/null +++ b/tests/codegen-units/item-collection/instantiation-through-vtable.rs @@ -0,0 +1,38 @@ +// +// compile-flags:-Zprint-mono-items=eager -Zinline-in-all-cgus -Zmir-opt-level=0 + +#![deny(dead_code)] +#![feature(start)] + +trait Trait { + fn foo(&self) -> u32; + fn bar(&self); +} + +struct Struct { + _a: T +} + +impl Trait for Struct { + fn foo(&self) -> u32 { 0 } + fn bar(&self) {} +} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + let s1 = Struct { _a: 0u32 }; + + //~ MONO_ITEM fn std::ptr::drop_in_place::> - shim(None) @@ instantiation_through_vtable-cgu.0[Internal] + //~ MONO_ITEM fn as Trait>::foo + //~ MONO_ITEM fn as Trait>::bar + let _ = &s1 as &Trait; + + let s1 = Struct { _a: 0u64 }; + //~ MONO_ITEM fn std::ptr::drop_in_place::> - shim(None) @@ instantiation_through_vtable-cgu.0[Internal] + //~ MONO_ITEM fn as Trait>::foo + //~ MONO_ITEM fn as Trait>::bar + let _ = &s1 as &Trait; + + 0 +} diff --git a/tests/codegen-units/item-collection/items-within-generic-items.rs b/tests/codegen-units/item-collection/items-within-generic-items.rs new file mode 100644 index 000000000..d37d7f7d9 --- /dev/null +++ b/tests/codegen-units/item-collection/items-within-generic-items.rs @@ -0,0 +1,35 @@ +// compile-flags:-Zprint-mono-items=eager + +#![deny(dead_code)] +#![feature(start)] + +fn generic_fn(a: T) -> (T, i32) { + //~ MONO_ITEM fn generic_fn::nested_fn + fn nested_fn(a: i32) -> i32 { + a + 1 + } + + let x = { + //~ MONO_ITEM fn generic_fn::nested_fn + fn nested_fn(a: i32) -> i32 { + a + 2 + } + + 1 + nested_fn(1) + }; + + return (a, x + nested_fn(0)); +} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + //~ MONO_ITEM fn generic_fn:: + let _ = generic_fn(0i64); + //~ MONO_ITEM fn generic_fn:: + let _ = generic_fn(0u16); + //~ MONO_ITEM fn generic_fn:: + let _ = generic_fn(0i8); + + 0 +} diff --git a/tests/codegen-units/item-collection/non-generic-closures.rs b/tests/codegen-units/item-collection/non-generic-closures.rs new file mode 100644 index 000000000..379fbcf26 --- /dev/null +++ b/tests/codegen-units/item-collection/non-generic-closures.rs @@ -0,0 +1,60 @@ +// In the current version of the collector that still has to support +// legacy-codegen, closures do not generate their own MonoItems, so we are +// ignoring this test until MIR codegen has taken over completely +// ignore-test + +// +// compile-flags:-Zprint-mono-items=eager + +#![deny(dead_code)] +#![feature(start)] + +//~ MONO_ITEM fn non_generic_closures::temporary[0] +fn temporary() { + //~ MONO_ITEM fn non_generic_closures::temporary[0]::{{closure}}[0] + (|a: u32| { + let _ = a; + })(4); +} + +//~ MONO_ITEM fn non_generic_closures::assigned_to_variable_but_not_executed[0] +fn assigned_to_variable_but_not_executed() { + //~ MONO_ITEM fn non_generic_closures::assigned_to_variable_but_not_executed[0]::{{closure}}[0] + let _x = |a: i16| { + let _ = a + 1; + }; +} + +//~ MONO_ITEM fn non_generic_closures::assigned_to_variable_executed_directly[0] +fn assigned_to_variable_executed_indirectly() { + //~ MONO_ITEM fn non_generic_closures::assigned_to_variable_executed_directly[0]::{{closure}}[0] + let f = |a: i32| { + let _ = a + 2; + }; + run_closure(&f); +} + +//~ MONO_ITEM fn non_generic_closures::assigned_to_variable_executed_indirectly[0] +fn assigned_to_variable_executed_directly() { + //~ MONO_ITEM fn non_generic_closures::assigned_to_variable_executed_indirectly[0]::{{closure}}[0] + let f = |a: i64| { + let _ = a + 3; + }; + f(4); +} + +//~ MONO_ITEM fn non_generic_closures::start[0] +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + temporary(); + assigned_to_variable_but_not_executed(); + assigned_to_variable_executed_directly(); + assigned_to_variable_executed_indirectly(); + + 0 +} + +//~ MONO_ITEM fn non_generic_closures::run_closure[0] +fn run_closure(f: &Fn(i32)) { + f(3); +} diff --git a/tests/codegen-units/item-collection/non-generic-drop-glue.rs b/tests/codegen-units/item-collection/non-generic-drop-glue.rs new file mode 100644 index 000000000..06f76f7db --- /dev/null +++ b/tests/codegen-units/item-collection/non-generic-drop-glue.rs @@ -0,0 +1,49 @@ +// +// compile-flags:-Zprint-mono-items=eager +// compile-flags:-Zinline-in-all-cgus + +#![deny(dead_code)] +#![feature(start)] + +//~ MONO_ITEM fn std::ptr::drop_in_place:: - shim(Some(StructWithDrop)) @@ non_generic_drop_glue-cgu.0[Internal] +struct StructWithDrop { + x: i32 +} + +impl Drop for StructWithDrop { + //~ MONO_ITEM fn ::drop + fn drop(&mut self) {} +} + +struct StructNoDrop { + x: i32 +} + +//~ MONO_ITEM fn std::ptr::drop_in_place:: - shim(Some(EnumWithDrop)) @@ non_generic_drop_glue-cgu.0[Internal] +enum EnumWithDrop { + A(i32) +} + +impl Drop for EnumWithDrop { + //~ MONO_ITEM fn ::drop + fn drop(&mut self) {} +} + +enum EnumNoDrop { + A(i32) +} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + let _ = StructWithDrop { x: 0 }.x; + let _ = StructNoDrop { x: 0 }.x; + let _ = match EnumWithDrop::A(0) { + EnumWithDrop::A(x) => x + }; + let _ = match EnumNoDrop::A(0) { + EnumNoDrop::A(x) => x + }; + + 0 +} diff --git a/tests/codegen-units/item-collection/non-generic-functions.rs b/tests/codegen-units/item-collection/non-generic-functions.rs new file mode 100644 index 000000000..092e64562 --- /dev/null +++ b/tests/codegen-units/item-collection/non-generic-functions.rs @@ -0,0 +1,72 @@ +// compile-flags:-Zprint-mono-items=eager + +#![deny(dead_code)] +#![feature(start)] + +//~ MONO_ITEM fn foo +fn foo() { + { + //~ MONO_ITEM fn foo::foo + fn foo() {} + foo(); + } + + { + //~ MONO_ITEM fn foo::foo + fn foo() {} + foo(); + } +} + +//~ MONO_ITEM fn bar +fn bar() { + //~ MONO_ITEM fn bar::baz + fn baz() {} + baz(); +} + +struct Struct { _x: i32 } + +impl Struct { + //~ MONO_ITEM fn Struct::foo + fn foo() { + { + //~ MONO_ITEM fn Struct::foo::foo + fn foo() {} + foo(); + } + + { + //~ MONO_ITEM fn Struct::foo::foo + fn foo() {} + foo(); + } + } + + //~ MONO_ITEM fn Struct::bar + fn bar(&self) { + { + //~ MONO_ITEM fn Struct::bar::foo + fn foo() {} + foo(); + } + + { + //~ MONO_ITEM fn Struct::bar::foo + fn foo() {} + foo(); + } + } +} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + foo(); + bar(); + Struct::foo(); + let x = Struct { _x: 0 }; + x.bar(); + + 0 +} diff --git a/tests/codegen-units/item-collection/overloaded-operators.rs b/tests/codegen-units/item-collection/overloaded-operators.rs new file mode 100644 index 000000000..2be7eba1d --- /dev/null +++ b/tests/codegen-units/item-collection/overloaded-operators.rs @@ -0,0 +1,59 @@ +// compile-flags:-Zprint-mono-items=eager + +#![deny(dead_code)] +#![crate_type="lib"] + +use std::ops::{Index, IndexMut, Add, Deref}; + +pub struct Indexable { + data: [u8; 3] +} + +impl Index for Indexable { + type Output = u8; + + //~ MONO_ITEM fn >::index + fn index(&self, index: usize) -> &Self::Output { + if index >= 3 { + &self.data[0] + } else { + &self.data[index] + } + } +} + +impl IndexMut for Indexable { + //~ MONO_ITEM fn >::index_mut + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + if index >= 3 { + &mut self.data[0] + } else { + &mut self.data[index] + } + } +} + + +//~ MONO_ITEM fn ::eq +//~ MONO_ITEM fn ::ne +#[derive(PartialEq)] +pub struct Equatable(u32); + + +impl Add for Equatable { + type Output = u32; + + //~ MONO_ITEM fn >::add + fn add(self, rhs: u32) -> u32 { + self.0 + rhs + } +} + +impl Deref for Equatable { + type Target = u32; + + //~ MONO_ITEM fn ::deref + fn deref(&self) -> &Self::Target { + &self.0 + } +} diff --git a/tests/codegen-units/item-collection/static-init.rs b/tests/codegen-units/item-collection/static-init.rs new file mode 100644 index 000000000..287ec8f24 --- /dev/null +++ b/tests/codegen-units/item-collection/static-init.rs @@ -0,0 +1,16 @@ +// compile-flags:-Zprint-mono-items=eager -Zpolymorphize=on + +#![feature(start)] + +pub static FN : fn() = foo::; + +pub fn foo() { } + +//~ MONO_ITEM fn foo:: +//~ MONO_ITEM static FN + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + 0 +} diff --git a/tests/codegen-units/item-collection/statics-and-consts.rs b/tests/codegen-units/item-collection/statics-and-consts.rs new file mode 100644 index 000000000..49a8d3dff --- /dev/null +++ b/tests/codegen-units/item-collection/statics-and-consts.rs @@ -0,0 +1,54 @@ +// compile-flags:-Zprint-mono-items=eager + +#![deny(dead_code)] +#![feature(start)] + +static STATIC1: i64 = { + const STATIC1_CONST1: i64 = 2; + 1 + CONST1 as i64 + STATIC1_CONST1 +}; + +const CONST1: i64 = { + const CONST1_1: i64 = { + const CONST1_1_1: i64 = 2; + CONST1_1_1 + 1 + }; + 1 + CONST1_1 as i64 +}; + +fn foo() { + let _ = { + const CONST2: i64 = 0; + static STATIC2: i64 = CONST2; + + let x = { + const CONST2: i64 = 1; + static STATIC2: i64 = CONST2; + STATIC2 + }; + + x + STATIC2 + }; + + let _ = { + const CONST2: i64 = 0; + static STATIC2: i64 = CONST2; + STATIC2 + }; +} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + foo(); + let _ = STATIC1; + + 0 +} + +//~ MONO_ITEM static STATIC1 + +//~ MONO_ITEM fn foo +//~ MONO_ITEM static foo::STATIC2 +//~ MONO_ITEM static foo::STATIC2 +//~ MONO_ITEM static foo::STATIC2 diff --git a/tests/codegen-units/item-collection/trait-implementations.rs b/tests/codegen-units/item-collection/trait-implementations.rs new file mode 100644 index 000000000..a816cb032 --- /dev/null +++ b/tests/codegen-units/item-collection/trait-implementations.rs @@ -0,0 +1,73 @@ +// compile-flags:-Zprint-mono-items=eager + +#![deny(dead_code)] +#![feature(start)] + +pub trait SomeTrait { + fn foo(&self); + fn bar(&self, x: T); +} + +impl SomeTrait for i64 { + + //~ MONO_ITEM fn ::foo + fn foo(&self) {} + + fn bar(&self, _: T) {} +} + +impl SomeTrait for i32 { + + //~ MONO_ITEM fn ::foo + fn foo(&self) {} + + fn bar(&self, _: T) {} +} + +pub trait SomeGenericTrait { + fn foo(&self, x: T); + fn bar(&self, x: T, y: T2); +} + +// Concrete impl of generic trait +impl SomeGenericTrait for f64 { + + //~ MONO_ITEM fn >::foo + fn foo(&self, _: u32) {} + + fn bar(&self, _: u32, _: T2) {} +} + +// Generic impl of generic trait +impl SomeGenericTrait for f32 { + + fn foo(&self, _: T) {} + fn bar(&self, _: T, _: T2) {} +} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + //~ MONO_ITEM fn ::bar:: + 0i32.bar('x'); + + //~ MONO_ITEM fn >::bar::<&str> + 0f64.bar(0u32, "&str"); + + //~ MONO_ITEM fn >::bar::<()> + 0f64.bar(0u32, ()); + + //~ MONO_ITEM fn >::foo + 0f32.foo('x'); + + //~ MONO_ITEM fn >::foo + 0f32.foo(-1i64); + + //~ MONO_ITEM fn >::bar::<()> + 0f32.bar(0u32, ()); + + //~ MONO_ITEM fn >::bar::<&str> + 0f32.bar("&str", "&str"); + + 0 +} diff --git a/tests/codegen-units/item-collection/trait-method-as-argument.rs b/tests/codegen-units/item-collection/trait-method-as-argument.rs new file mode 100644 index 000000000..235569728 --- /dev/null +++ b/tests/codegen-units/item-collection/trait-method-as-argument.rs @@ -0,0 +1,60 @@ +// +// compile-flags:-Zprint-mono-items=eager + +#![deny(dead_code)] +#![feature(start)] + +trait Trait : Sized { + fn foo(self) -> Self { self } +} + +impl Trait for u32 { + fn foo(self) -> u32 { self } +} + +impl Trait for char { +} + +fn take_foo_once T>(f: F, arg: T) -> T { + (f)(arg) +} + +fn take_foo T>(f: F, arg: T) -> T { + (f)(arg) +} + +fn take_foo_mut T>(mut f: F, arg: T) -> T { + (f)(arg) +} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + //~ MONO_ITEM fn take_foo_once:: u32 {::foo}> + //~ MONO_ITEM fn ::foo + //~ MONO_ITEM fn u32 {::foo} as std::ops::FnOnce<(u32,)>>::call_once - shim(fn(u32) -> u32 {::foo}) + take_foo_once(Trait::foo, 0u32); + + //~ MONO_ITEM fn take_foo_once:: char {::foo}> + //~ MONO_ITEM fn ::foo + //~ MONO_ITEM fn char {::foo} as std::ops::FnOnce<(char,)>>::call_once - shim(fn(char) -> char {::foo}) + take_foo_once(Trait::foo, 'c'); + + //~ MONO_ITEM fn take_foo:: u32 {::foo}> + //~ MONO_ITEM fn u32 {::foo} as std::ops::Fn<(u32,)>>::call - shim(fn(u32) -> u32 {::foo}) + take_foo(Trait::foo, 0u32); + + //~ MONO_ITEM fn take_foo:: char {::foo}> + //~ MONO_ITEM fn char {::foo} as std::ops::Fn<(char,)>>::call - shim(fn(char) -> char {::foo}) + take_foo(Trait::foo, 'c'); + + //~ MONO_ITEM fn take_foo_mut:: u32 {::foo}> + //~ MONO_ITEM fn u32 {::foo} as std::ops::FnMut<(u32,)>>::call_mut - shim(fn(u32) -> u32 {::foo}) + take_foo_mut(Trait::foo, 0u32); + + //~ MONO_ITEM fn take_foo_mut:: char {::foo}> + //~ MONO_ITEM fn char {::foo} as std::ops::FnMut<(char,)>>::call_mut - shim(fn(char) -> char {::foo}) + take_foo_mut(Trait::foo, 'c'); + + 0 +} diff --git a/tests/codegen-units/item-collection/trait-method-default-impl.rs b/tests/codegen-units/item-collection/trait-method-default-impl.rs new file mode 100644 index 000000000..bfcdb6fa1 --- /dev/null +++ b/tests/codegen-units/item-collection/trait-method-default-impl.rs @@ -0,0 +1,61 @@ +// compile-flags:-Zprint-mono-items=eager -Zpolymorphize=on + +#![deny(dead_code)] +#![feature(start)] + +trait SomeTrait { + fn foo(&self) { } + fn bar(&self, x: T) -> T { x } +} + +impl SomeTrait for i8 { + // take the default implementations + + // For the non-generic foo(), we should generate a codegen-item even if it + // is not called anywhere + //~ MONO_ITEM fn ::foo +} + +trait SomeGenericTrait { + fn foo(&self) { } + fn bar(&self, x: T1, y: T2) {} +} + +// Non-generic impl of generic trait +impl SomeGenericTrait for i32 { + // take the default implementations + + // For the non-generic foo(), we should generate a codegen-item even if it + // is not called anywhere + //~ MONO_ITEM fn >::foo +} + +// Non-generic impl of generic trait +impl SomeGenericTrait for u32 { + // take the default implementations + // since nothing is monomorphic here, nothing should be generated unless used somewhere. +} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + //~ MONO_ITEM fn ::bar:: + let _ = 1i8.bar('c'); + + //~ MONO_ITEM fn ::bar::<&str> + let _ = 2i8.bar("&str"); + + //~ MONO_ITEM fn >::bar:: + 0i32.bar(0u64, 'c'); + + //~ MONO_ITEM fn >::bar::<&str> + 0i32.bar(0u64, "&str"); + + //~ MONO_ITEM fn >::bar::<&[char; 1]> + 0u32.bar(0i8, &['c']); + + //~ MONO_ITEM fn >::bar::<()> + 0u32.bar(0i16, ()); + + 0 +} diff --git a/tests/codegen-units/item-collection/transitive-drop-glue.rs b/tests/codegen-units/item-collection/transitive-drop-glue.rs new file mode 100644 index 000000000..e286c800b --- /dev/null +++ b/tests/codegen-units/item-collection/transitive-drop-glue.rs @@ -0,0 +1,46 @@ +// +// compile-flags:-Zprint-mono-items=eager +// compile-flags:-Zinline-in-all-cgus + +#![deny(dead_code)] +#![feature(start)] + +//~ MONO_ITEM fn std::ptr::drop_in_place:: - shim(Some(Root)) @@ transitive_drop_glue-cgu.0[Internal] +struct Root(#[allow(unused_tuple_struct_fields)] Intermediate); +//~ MONO_ITEM fn std::ptr::drop_in_place:: - shim(Some(Intermediate)) @@ transitive_drop_glue-cgu.0[Internal] +struct Intermediate(#[allow(unused_tuple_struct_fields)] Leaf); +//~ MONO_ITEM fn std::ptr::drop_in_place:: - shim(Some(Leaf)) @@ transitive_drop_glue-cgu.0[Internal] +struct Leaf; + +impl Drop for Leaf { + //~ MONO_ITEM fn ::drop + fn drop(&mut self) {} +} + +struct RootGen(#[allow(unused_tuple_struct_fields)] IntermediateGen); +struct IntermediateGen(#[allow(unused_tuple_struct_fields)] LeafGen); +struct LeafGen(#[allow(unused_tuple_struct_fields)] T); + +impl Drop for LeafGen { + fn drop(&mut self) {} +} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + let _ = Root(Intermediate(Leaf)); + + //~ MONO_ITEM fn std::ptr::drop_in_place::> - shim(Some(RootGen)) @@ transitive_drop_glue-cgu.0[Internal] + //~ MONO_ITEM fn std::ptr::drop_in_place::> - shim(Some(IntermediateGen)) @@ transitive_drop_glue-cgu.0[Internal] + //~ MONO_ITEM fn std::ptr::drop_in_place::> - shim(Some(LeafGen)) @@ transitive_drop_glue-cgu.0[Internal] + //~ MONO_ITEM fn as std::ops::Drop>::drop + let _ = RootGen(IntermediateGen(LeafGen(0u32))); + + //~ MONO_ITEM fn std::ptr::drop_in_place::> - shim(Some(RootGen)) @@ transitive_drop_glue-cgu.0[Internal] + //~ MONO_ITEM fn std::ptr::drop_in_place::> - shim(Some(IntermediateGen)) @@ transitive_drop_glue-cgu.0[Internal] + //~ MONO_ITEM fn std::ptr::drop_in_place::> - shim(Some(LeafGen)) @@ transitive_drop_glue-cgu.0[Internal] + //~ MONO_ITEM fn as std::ops::Drop>::drop + let _ = RootGen(IntermediateGen(LeafGen(0i16))); + + 0 +} diff --git a/tests/codegen-units/item-collection/tuple-drop-glue.rs b/tests/codegen-units/item-collection/tuple-drop-glue.rs new file mode 100644 index 000000000..ae3b2e081 --- /dev/null +++ b/tests/codegen-units/item-collection/tuple-drop-glue.rs @@ -0,0 +1,27 @@ +// +// compile-flags:-Zprint-mono-items=eager +// compile-flags:-Zinline-in-all-cgus + +#![deny(dead_code)] +#![feature(start)] + +//~ MONO_ITEM fn std::ptr::drop_in_place:: - shim(Some(Dropped)) @@ tuple_drop_glue-cgu.0[Internal] +struct Dropped; + +impl Drop for Dropped { + //~ MONO_ITEM fn ::drop + fn drop(&mut self) {} +} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + //~ MONO_ITEM fn std::ptr::drop_in_place::<(u32, Dropped)> - shim(Some((u32, Dropped))) @@ tuple_drop_glue-cgu.0[Internal] + let x = (0u32, Dropped); + + //~ MONO_ITEM fn std::ptr::drop_in_place::<(i16, (Dropped, bool))> - shim(Some((i16, (Dropped, bool)))) @@ tuple_drop_glue-cgu.0[Internal] + //~ MONO_ITEM fn std::ptr::drop_in_place::<(Dropped, bool)> - shim(Some((Dropped, bool))) @@ tuple_drop_glue-cgu.0[Internal] + let x = (0i16, (Dropped, true)); + + 0 +} diff --git a/tests/codegen-units/item-collection/unreferenced-const-fn.rs b/tests/codegen-units/item-collection/unreferenced-const-fn.rs new file mode 100644 index 000000000..17b92eae0 --- /dev/null +++ b/tests/codegen-units/item-collection/unreferenced-const-fn.rs @@ -0,0 +1,9 @@ +// compile-flags:-Zprint-mono-items=lazy + +#![deny(dead_code)] +#![crate_type = "rlib"] + +//~ MONO_ITEM fn foo @@ unreferenced_const_fn-cgu.0[External] +pub const fn foo(x: u32) -> u32 { + x + 0xf00 +} diff --git a/tests/codegen-units/item-collection/unreferenced-inline-function.rs b/tests/codegen-units/item-collection/unreferenced-inline-function.rs new file mode 100644 index 000000000..4d095e4d6 --- /dev/null +++ b/tests/codegen-units/item-collection/unreferenced-inline-function.rs @@ -0,0 +1,11 @@ +// compile-flags:-Zprint-mono-items=lazy + +// N.B., we do not expect *any* monomorphization to be generated here. + +#![deny(dead_code)] +#![crate_type = "rlib"] + +#[inline] +pub fn foo() -> bool { + [1, 2] == [3, 4] +} diff --git a/tests/codegen-units/item-collection/unsizing.rs b/tests/codegen-units/item-collection/unsizing.rs new file mode 100644 index 000000000..111a72312 --- /dev/null +++ b/tests/codegen-units/item-collection/unsizing.rs @@ -0,0 +1,79 @@ +// compile-flags:-Zprint-mono-items=eager +// compile-flags:-Zinline-in-all-cgus +// compile-flags:-Zmir-opt-level=0 + +#![deny(dead_code)] +#![feature(coerce_unsized)] +#![feature(unsize)] +#![feature(start)] + +use std::marker::Unsize; +use std::ops::CoerceUnsized; + +trait Trait { + fn foo(&self); +} + +// Simple Case +impl Trait for bool { + fn foo(&self) {} +} + +impl Trait for char { + fn foo(&self) {} +} + +// Struct Field Case +struct Struct { + _a: u32, + _b: i32, + _c: T +} + +impl Trait for f64 { + fn foo(&self) {} +} + +// Custom Coercion Case +impl Trait for u32 { + fn foo(&self) {} +} + +#[derive(Clone, Copy)] +struct Wrapper(#[allow(unused_tuple_struct_fields)] *const T); + +impl, U: ?Sized> CoerceUnsized> for Wrapper {} + +//~ MONO_ITEM fn start +#[start] +fn start(_: isize, _: *const *const u8) -> isize { + // simple case + let bool_sized = &true; + //~ MONO_ITEM fn std::ptr::drop_in_place:: - shim(None) @@ unsizing-cgu.0[Internal] + //~ MONO_ITEM fn ::foo + let _bool_unsized = bool_sized as &Trait; + + let char_sized = &'a'; + + //~ MONO_ITEM fn std::ptr::drop_in_place:: - shim(None) @@ unsizing-cgu.0[Internal] + //~ MONO_ITEM fn ::foo + let _char_unsized = char_sized as &Trait; + + // struct field + let struct_sized = &Struct { + _a: 1, + _b: 2, + _c: 3.0f64 + }; + //~ MONO_ITEM fn std::ptr::drop_in_place:: - shim(None) @@ unsizing-cgu.0[Internal] + //~ MONO_ITEM fn ::foo + let _struct_unsized = struct_sized as &Struct; + + // custom coercion + let wrapper_sized = Wrapper(&0u32); + //~ MONO_ITEM fn std::ptr::drop_in_place:: - shim(None) @@ unsizing-cgu.0[Internal] + //~ MONO_ITEM fn ::foo + let _wrapper_sized = wrapper_sized as Wrapper; + + 0 +} diff --git a/tests/codegen-units/item-collection/unused-traits-and-generics.rs b/tests/codegen-units/item-collection/unused-traits-and-generics.rs new file mode 100644 index 000000000..561dc1a5c --- /dev/null +++ b/tests/codegen-units/item-collection/unused-traits-and-generics.rs @@ -0,0 +1,77 @@ +// compile-flags:-Zprint-mono-items=eager + +#![crate_type="lib"] +#![deny(dead_code)] + +// This test asserts that no codegen items are generated for generic items that +// are never instantiated in the local crate. + +pub trait Trait { + fn foo() {} + fn bar(&self) {} +} + +pub fn foo(x: T) -> (T, T) { + (x, x) +} + +pub struct Struct { + x: T +} + +impl Struct { + pub fn foo(self) -> T { + self.x + } + + pub fn bar() {} +} + +pub enum Enum { + A(T), + B { x: T } +} + +impl Enum { + pub fn foo(self) -> T { + match self { + Enum::A(x) => x, + Enum::B { x } => x, + } + } + + pub fn bar() {} +} + +pub struct TupleStruct(T); + +impl TupleStruct { + pub fn foo(self) -> T { + self.0 + } + + pub fn bar() {} +} + +pub type Pair = (T, T); + +pub struct NonGeneric { + x: i32 +} + +impl NonGeneric { + pub fn foo(self) -> i32 { + self.x + } + + pub fn generic_foo(&self, x: T) -> (T, i32) { + (x, self.x) + } + + pub fn generic_bar(x: T) -> (T, T) { + (x, x) + } +} + +// Only the non-generic methods should be instantiated: +//~ MONO_ITEM fn NonGeneric::foo -- cgit v1.2.3