diff --git a/CMakeLists.txt b/CMakeLists.txt index 8760321390..171be6eb4e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,6 +152,7 @@ add_custom_target("check-unit" "${PROJECT_SOURCE_DIR}/tests/lit/lit.py" ${LIT_FLAGS} "${PROJECT_SOURCE_DIR}/tests/unit" + "${PROJECT_SOURCE_DIR}/tests/std" "${PROJECT_SOURCE_DIR}/tests/ub" "${PROJECT_SOURCE_DIR}/tests/multi-file" DEPENDS cpp2rust libcc2rs diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index a819bfb022..083fa6ba96 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -1507,7 +1507,8 @@ static const clang::FieldDecl * AsLambdaCaptureField(const clang::FunctionDecl *fn, const clang::Expr *expr) { auto call = AsLambdaOperatorCall(fn); auto ref = clang::dyn_cast(expr); - if (!call || !ref || !ref->refersToEnclosingVariableOrCapture()) { + if (!call || !ref || !ref->refersToEnclosingVariableOrCapture() || + ref->isNonOdrUse() != clang::NOUR_None) { return nullptr; } llvm::DenseMap fields; diff --git a/tests/lit/lit/formats/Cpp2RustTest.py b/tests/lit/lit/formats/Cpp2RustTest.py index 809b33b23a..3b7cb10d1d 100644 --- a/tests/lit/lit/formats/Cpp2RustTest.py +++ b/tests/lit/lit/formats/Cpp2RustTest.py @@ -373,6 +373,8 @@ def getTestsInDirectory(self, testSuite, path_in_suite, litConfig, localConfig): yield lit.Test.Test(testSuite, path_in_suite, localConfig) return for entry in source_path.iterdir(): + if entry.name in localConfig.excludes: + continue if entry.is_file() and entry.suffix in (".cpp", ".c"): yield lit.Test.Test( testSuite, path_in_suite + (entry.name,), localConfig @@ -485,6 +487,7 @@ def cpp2rust_command(cc_input, build_dir, model, rs_file, extra_cxxflags): model, "-o", str(rs_file), + f"-cxxflags=-I{cc_input.parent}", ] for flag in extra_cxxflags: cmd.append(f"-cxxflags={flag}") diff --git a/tests/std/basic/basic.def.odr/lit.local.cfg b/tests/std/basic/basic.def.odr/lit.local.cfg new file mode 100644 index 0000000000..e2ce84f021 --- /dev/null +++ b/tests/std/basic/basic.def.odr/lit.local.cfg @@ -0,0 +1,4 @@ +# Clang treats a discarded-value use as an odr-use, so [=] captures the variable +# and the C++ program itself fails its asserts: +# https://github.com/llvm/llvm-project/issues/127086 +config.excludes = list(config.excludes) + ["p5.3-discarded-value-in-lambda.cpp"] diff --git a/tests/std/basic/basic.def.odr/out/refcount/p11-discarded-statement-in-template.rs b/tests/std/basic/basic.def.odr/out/refcount/p11-discarded-statement-in-template.rs new file mode 100644 index 0000000000..e7a86fa471 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p11-discarded-statement-in-template.rs @@ -0,0 +1,101 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +thread_local!( + pub static else_runs_4: Value = Rc::new(RefCell::new(0_i32)); +); +pub fn g_5(mut _a0: i32) { + { + (*else_runs_4.with(Value::clone).borrow_mut()).prefix_inc(); + } +} +pub fn g_6(mut _a0: i8) { + { + (*else_runs_4.with(Value::clone).borrow_mut()).prefix_inc(); + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ g_5(1) }); + ({ g_6(('c' as i8)) }); + assert!((else_runs_4.with(|rc| *rc.borrow()) == 2)); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0)); + assert!((useCount_3.with(|rc| *rc.borrow()) == 0)); + return 0; +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); + let _ = else_runs_4.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p11-discarded-statement.rs b/tests/std/basic/basic.def.odr/out/refcount/p11-discarded-statement.rs new file mode 100644 index 0000000000..fa6426b662 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p11-discarded-statement.rs @@ -0,0 +1,95 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +thread_local!( + pub static else_runs_4: Value = Rc::new(RefCell::new(0_i32)); +); +pub fn g_5() { + { + (*else_runs_4.with(Value::clone).borrow_mut()).prefix_inc(); + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ g_5() }); + assert!((else_runs_4.with(|rc| *rc.borrow()) == 1)); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0)); + assert!((useCount_3.with(|rc| *rc.borrow()) == 0)); + return 0; +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); + let _ = else_runs_4.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p11-undefined-variable-in-discarded-statement.rs b/tests/std/basic/basic.def.odr/out/refcount/p11-undefined-variable-in-discarded-statement.rs new file mode 100644 index 0000000000..6f83bb084a --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p11-undefined-variable-in-discarded-statement.rs @@ -0,0 +1,95 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +thread_local!( + pub static else_runs_4: Value = Rc::new(RefCell::new(0_i32)); +); +pub fn g_6() { + { + (*else_runs_4.with(Value::clone).borrow_mut()).prefix_inc(); + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ g_6() }); + assert!((else_runs_4.with(|rc| *rc.borrow()) == 1)); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0)); + assert!((useCount_3.with(|rc| *rc.borrow()) == 0)); + return 0; +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); + let _ = else_runs_4.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p12-inline-undefined-in-discarded-statement.rs b/tests/std/basic/basic.def.odr/out/refcount/p12-inline-undefined-in-discarded-statement.rs new file mode 100644 index 0000000000..373901489e --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p12-inline-undefined-in-discarded-statement.rs @@ -0,0 +1,27 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static g_calls_0: Value = Rc::new(RefCell::new(0_i32)); +); +pub fn g_1() { + {} + (*g_calls_0.with(Value::clone).borrow_mut()).prefix_inc(); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ g_1() }); + assert!((g_calls_0.with(|rc| *rc.borrow()) == 1)); + return 0; +} +pub fn __cpp2rust_init_globals() { + let _ = g_calls_0.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p13-pointer-to-incomplete-class.rs b/tests/std/basic/basic.def.odr/out/refcount/p13-pointer-to-incomplete-class.rs new file mode 100644 index 0000000000..d108faf3fd --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p13-pointer-to-incomplete-class.rs @@ -0,0 +1,41 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static x1_0: Value> = Rc::new(RefCell::new(Ptr::::null())); +); +thread_local!( + pub static x2_1: Value> = Rc::new(RefCell::new(Ptr::::null())); +); +pub fn identity_2(mut p: Ptr) -> Ptr { + return (p).clone(); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + assert!(({ (*x1_0.with(Value::clone).borrow()).is_null() })); + assert!( + ({ + ({ ({ identity_2((*x2_1.with(Value::clone).borrow()).clone()) }) } == { + (*x1_0.with(Value::clone).borrow()).clone() + }) + }) + ); + let mut pp: Ptr> = (x1_0.with(|v| v.as_pointer())); + assert!((pp.read()).is_null()); + return 0; +} +#[derive(Clone, Copy, Default, ByteRepr, VaArg, FnPtrArg)] +#[byte_size(1)] +pub struct X; +pub fn __cpp2rust_init_globals() { + let _ = x1_0.with(|_| ()); + let _ = x2_1.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p3.2-subscript.rs b/tests/std/basic/basic.def.odr/out/refcount/p3.2-subscript.rs new file mode 100644 index 0000000000..e19b445e6c --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p3.2-subscript.rs @@ -0,0 +1,68 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static copyCount_0: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] +#[cpp_from(i32, new, value)] +#[byte_size(4)] +pub struct HasConstexprCtor { + #[offset(0)] + pub m: i32, +} +impl HasConstexprCtor { + pub fn new(mut v: i32) -> Self { + Self { m: v } + } + pub fn copy_from(o: Ptr) -> Self { + let __this: HasConstexprCtor = Self { + m: o.with(|__s| __s.m), + }; + (*copyCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for HasConstexprCtor { + fn clone(&self) -> Self { + let __src: Value = + Rc::new(RefCell::new(HasConstexprCtor { m: self.m.clone() })); + HasConstexprCtor::copy_from(__src.as_pointer()) + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let mut arr: [HasConstexprCtor; 2] = + [HasConstexprCtor::new({ 3 }), HasConstexprCtor::new({ 4 })]; + let f: Value i32>> = + Rc::new(RefCell::new(FnPtr:: i32>::new(|| -> i32 { + { + return [HasConstexprCtor::new({ 3 }), HasConstexprCtor::new({ 4 })][(1) as usize] + .m; + } + }))); + assert!((copyCount_0.with(|rc| *rc.borrow()) == 0)); + assert!(({ (({ (*f.borrow()).call() }) == 4) })); + assert!((copyCount_0.with(|rc| *rc.borrow()) == 0)); + return 0; +} +pub trait HasConstexprCtorImpl { + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl HasConstexprCtorImpl for Ptr { + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), m).write({ _a0.with(|__s| __s.m) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = copyCount_0.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p3.3-member-access.rs b/tests/std/basic/basic.def.odr/out/refcount/p3.3-member-access.rs new file mode 100644 index 0000000000..efd144bcc1 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p3.3-member-access.rs @@ -0,0 +1,66 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static copyCount_0: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] +#[cpp_from(i32, new, value)] +#[byte_size(4)] +pub struct HasConstexprCtor { + #[offset(0)] + pub m: i32, +} +impl HasConstexprCtor { + pub fn new(mut v: i32) -> Self { + Self { m: v } + } + pub fn copy_from(o: Ptr) -> Self { + let __this: HasConstexprCtor = Self { + m: o.with(|__s| __s.m), + }; + (*copyCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for HasConstexprCtor { + fn clone(&self) -> Self { + let __src: Value = + Rc::new(RefCell::new(HasConstexprCtor { m: self.m.clone() })); + HasConstexprCtor::copy_from(__src.as_pointer()) + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let mut k: HasConstexprCtor = HasConstexprCtor::new({ 5 }); + let f: Value i32>> = + Rc::new(RefCell::new(FnPtr:: i32>::new(|| -> i32 { + { + return HasConstexprCtor::new({ 5 }).m; + } + }))); + assert!((copyCount_0.with(|rc| *rc.borrow()) == 0)); + assert!(({ (({ (*f.borrow()).call() }) == 5) })); + assert!((copyCount_0.with(|rc| *rc.borrow()) == 0)); + return 0; +} +pub trait HasConstexprCtorImpl { + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl HasConstexprCtorImpl for Ptr { + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), m).write({ _a0.with(|__s| __s.m) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = copyCount_0.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p3.7-conditional.rs b/tests/std/basic/basic.def.odr/out/refcount/p3.7-conditional.rs new file mode 100644 index 0000000000..25e7fb46c0 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p3.7-conditional.rs @@ -0,0 +1,76 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static copyCount_0: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] +#[cpp_from(i32, new, value)] +#[byte_size(4)] +pub struct HasConstexprCtor { + #[offset(0)] + pub m: i32, +} +impl HasConstexprCtor { + pub fn new(mut v: i32) -> Self { + Self { m: v } + } + pub fn copy_from(o: Ptr) -> Self { + let __this: HasConstexprCtor = Self { + m: o.with(|__s| __s.m), + }; + (*copyCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for HasConstexprCtor { + fn clone(&self) -> Self { + let __src: Value = + Rc::new(RefCell::new(HasConstexprCtor { m: self.m.clone() })); + HasConstexprCtor::copy_from(__src.as_pointer()) + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let mut k1: HasConstexprCtor = HasConstexprCtor::new({ 7 }); + let mut k2: HasConstexprCtor = HasConstexprCtor::new({ 9 }); + let f: Value i32>> = Rc::new(RefCell::new(FnPtr:: i32>::new( + |mut b: bool| -> i32 { + { + return { + (if b { + HasConstexprCtor::new({ 7 }) + } else { + HasConstexprCtor::new({ 9 }) + }) + .m + }; + } + }, + ))); + assert!((copyCount_0.with(|rc| *rc.borrow()) == 0)); + assert!(({ (({ (*f.borrow()).call(true,) }) == 7) })); + assert!(({ (({ (*f.borrow()).call(false,) }) == 9) })); + assert!((copyCount_0.with(|rc| *rc.borrow()) == 0)); + return 0; +} +pub trait HasConstexprCtorImpl { + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl HasConstexprCtorImpl for Ptr { + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), m).write({ _a0.with(|__s| __s.m) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = copyCount_0.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p3.8-comma.rs b/tests/std/basic/basic.def.odr/out/refcount/p3.8-comma.rs new file mode 100644 index 0000000000..5b56355563 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p3.8-comma.rs @@ -0,0 +1,78 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static copyCount_0: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] +#[cpp_from(i32, new, value)] +#[byte_size(4)] +pub struct HasConstexprCtor { + #[offset(0)] + pub m: i32, +} +impl HasConstexprCtor { + pub fn new(mut v: i32) -> Self { + Self { m: v } + } + pub fn copy_from(o: Ptr) -> Self { + let __this: HasConstexprCtor = Self { + m: o.with(|__s| __s.m), + }; + (*copyCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for HasConstexprCtor { + fn clone(&self) -> Self { + let __src: Value = + Rc::new(RefCell::new(HasConstexprCtor { m: self.m.clone() })); + HasConstexprCtor::copy_from(__src.as_pointer()) + } +} +thread_local!( + pub static left_1: Value = Rc::new(RefCell::new(0_i32)); +); +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let k: Value = Rc::new(RefCell::new(HasConstexprCtor::new({ 5 }))); + let f: Value i32>> = + Rc::new(RefCell::new(FnPtr:: i32>::new(|| -> i32 { + { + return { + { + (*left_1.with(Value::clone).borrow_mut()).prefix_inc(); + HasConstexprCtor::new({ 5 }) + } + .m + }; + } + }))); + assert!((copyCount_0.with(|rc| *rc.borrow()) == 0)); + assert!((left_1.with(|rc| *rc.borrow()) == 0)); + assert!(({ (({ (*f.borrow()).call() }) == 5) })); + assert!((left_1.with(|rc| *rc.borrow()) == 1)); + assert!((copyCount_0.with(|rc| *rc.borrow()) == 0)); + return 0; +} +pub trait HasConstexprCtorImpl { + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl HasConstexprCtorImpl for Ptr { + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), m).write({ _a0.with(|__s| __s.m) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = copyCount_0.with(|_| ()); + let _ = left_1.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p4.1-pure-virtual-qualified-call.rs b/tests/std/basic/basic.def.odr/out/refcount/p4.1-pure-virtual-qualified-call.rs new file mode 100644 index 0000000000..75c8783930 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p4.1-pure-virtual-qualified-call.rs @@ -0,0 +1,48 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static base_calls_0: Value = Rc::new(RefCell::new(0_i32)); +); +thread_local!( + pub static derived_calls_1: Value = Rc::new(RefCell::new(0_i32)); +); +pub trait HasPureVirtual { + fn f(&mut self); +} +impl HasPureVirtual { + fn f(&mut self); +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(8)] +pub struct OverridesPureVirtual {} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let d: Value = Rc::new(RefCell::new(::default())); + let mut p: PtrDyn = (d.as_pointer()).to_dyn::(|w| w); + ({ (*p.upgrade().deref_mut()).f() }); + assert!((derived_calls_1.with(|rc| *rc.borrow()) == 1)); + assert!((base_calls_0.with(|rc| *rc.borrow()) == 1)); + ({ ((*d.borrow_mut()) as HasPureVirtual).f() }); + assert!((derived_calls_1.with(|rc| *rc.borrow()) == 1)); + assert!((base_calls_0.with(|rc| *rc.borrow()) == 2)); + return 0; +} +impl HasPureVirtual for OverridesPureVirtual { + fn f(&mut self) { + (*derived_calls_1.with(Value::clone).borrow_mut()).prefix_inc(); + ({ self.f() }); + } +} +pub fn __cpp2rust_init_globals() { + let _ = base_calls_0.with(|_| ()); + let _ = derived_calls_1.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p4.1-pure-virtual-without-definition.rs b/tests/std/basic/basic.def.odr/out/refcount/p4.1-pure-virtual-without-definition.rs new file mode 100644 index 0000000000..914bf176fd --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p4.1-pure-virtual-without-definition.rs @@ -0,0 +1,36 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static derived_calls_0: Value = Rc::new(RefCell::new(0_i32)); +); +pub trait HasPureVirtual { + fn f(&mut self); +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(8)] +pub struct OverridesPureVirtual {} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let d: Value = Rc::new(RefCell::new(::default())); + let mut p: PtrDyn = (d.as_pointer()).to_dyn::(|w| w); + ({ (*p.upgrade().deref_mut()).f() }); + assert!((derived_calls_0.with(|rc| *rc.borrow()) == 1)); + return 0; +} +impl HasPureVirtual for OverridesPureVirtual { + fn f(&mut self) { + (*derived_calls_0.with(Value::clone).borrow_mut()).prefix_inc(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = derived_calls_0.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p5.1-constant-reference-in-lambda.rs b/tests/std/basic/basic.def.odr/out/refcount/p5.1-constant-reference-in-lambda.rs new file mode 100644 index 0000000000..3978f86f02 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p5.1-constant-reference-in-lambda.rs @@ -0,0 +1,95 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +thread_local!( + pub static global_4: Value = Rc::new(RefCell::new(LifetimeTracked::new())); +); +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let r: Ptr = global_4.with(|v| v.as_pointer()); + let f: Value> = Rc::new(RefCell::new(FnPtr::::new(|| { + ({ LifetimeTrackedImpl::use_(&global_4.with(|v| v.as_pointer())) }); + }))); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 0)); + ({ (*f.borrow()).call() }); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 0)); + assert!(({ ({ (*global_4.with(Value::clone).borrow()).uses } == 1) })); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 1)); + return 0; +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); + let _ = global_4.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p5.1-local-reference-is-odr-used.rs b/tests/std/basic/basic.def.odr/out/refcount/p5.1-local-reference-is-odr-used.rs new file mode 100644 index 0000000000..ccb9caa3d5 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p5.1-local-reference-is-odr-used.rs @@ -0,0 +1,101 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let t: Value = Rc::new(RefCell::new(LifetimeTracked::new())); + let _dtor_t = std::mem::DropGuard::new(t.clone(), |__p| __p.as_pointer().destructor()); + let r: Ptr = t.as_pointer(); + let f: Value> = Rc::new(RefCell::new(lambda!( + { + let r: Value = + Rc::new(RefCell::new(LifetimeTracked::copy_from({ (r).clone() }))); + }, + || { + ({ LifetimeTrackedImpl::use_(&r.as_pointer()) }); + } + ))); + let _dtor_f = + std::mem::DropGuard::new(f.clone(), |__p| __p.as_pointer().with(|__f| __f.destroy())); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 1)); + ({ (*f.borrow()).call() }); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 1)); + assert!((useCount_3.with(|rc| *rc.borrow()) == 1)); + assert!(({ ({ (*t.borrow()).uses } == 0) })); + return 0; +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p5.2-address-of-constant-is-odr-use.rs b/tests/std/basic/basic.def.odr/out/refcount/p5.2-address-of-constant-is-odr-use.rs new file mode 100644 index 0000000000..c51b5e41b9 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p5.2-address-of-constant-is-odr-use.rs @@ -0,0 +1,27 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let n: Value = Rc::new(RefCell::new(5)); + let f: Value Ptr>> = Rc::new(RefCell::new(lambda!( + { + let n: Value = Rc::new(RefCell::new((*n.borrow()))); + }, + || -> Ptr { + return (n.as_pointer()); + } + ))); + assert!(({ (({ (*f.borrow()).call() }) != (n.as_pointer())) })); + assert!(({ ((({ (*f.borrow()).call() }).read()) == 5) })); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p5.2-class-object-is-odr-used.rs b/tests/std/basic/basic.def.odr/out/refcount/p5.2-class-object-is-odr-used.rs new file mode 100644 index 0000000000..3ce8d8efea --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p5.2-class-object-is-odr-used.rs @@ -0,0 +1,71 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static copyCount_0: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] +#[cpp_from(i32, new, value)] +#[byte_size(4)] +pub struct HasConstexprCtor { + #[offset(0)] + pub m: i32, +} +impl HasConstexprCtor { + pub fn new(mut v: i32) -> Self { + Self { m: v } + } + pub fn copy_from(o: Ptr) -> Self { + let __this: HasConstexprCtor = Self { + m: o.with(|__s| __s.m), + }; + (*copyCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for HasConstexprCtor { + fn clone(&self) -> Self { + let __src: Value = + Rc::new(RefCell::new(HasConstexprCtor { m: self.m.clone() })); + HasConstexprCtor::copy_from(__src.as_pointer()) + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let k: Value = Rc::new(RefCell::new(HasConstexprCtor::new({ 5 }))); + let f: Value i32>> = Rc::new(RefCell::new(lambda!( + { + let k: Value = Rc::new(RefCell::new(HasConstexprCtor::copy_from({ + k.as_pointer() + }))); + }, + || -> i32 { + let mut c: HasConstexprCtor = HasConstexprCtor::copy_from({ k.as_pointer() }); + return c.m; + } + ))); + assert!((copyCount_0.with(|rc| *rc.borrow()) == 1)); + assert!(({ (({ (*f.borrow()).call() }) == 5) })); + assert!((copyCount_0.with(|rc| *rc.borrow()) == 2)); + return 0; +} +pub trait HasConstexprCtorImpl { + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl HasConstexprCtorImpl for Ptr { + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), m).write({ _a0.with(|__s| __s.m) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = copyCount_0.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p5.2-constant-in-lambda-without-capture.rs b/tests/std/basic/basic.def.odr/out/refcount/p5.2-constant-in-lambda-without-capture.rs new file mode 100644 index 0000000000..ba671e22c8 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p5.2-constant-in-lambda-without-capture.rs @@ -0,0 +1,24 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let mut n: i32 = 5; + let f: Value i32>> = + Rc::new(RefCell::new(FnPtr:: i32>::new(|| -> i32 { + { + return 5; + } + }))); + assert!(({ (({ (*f.borrow()).call() }) == 5) })); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p5.2-mutable-subobject-is-odr-used.rs b/tests/std/basic/basic.def.odr/out/refcount/p5.2-mutable-subobject-is-odr-used.rs new file mode 100644 index 0000000000..90658d3548 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p5.2-mutable-subobject-is-odr-used.rs @@ -0,0 +1,79 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static copies_0: Value = Rc::new(RefCell::new(0_i32)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] +#[cpp_from(i32, new, value)] +#[byte_size(8)] +pub struct HasMutableMember { + #[offset(0)] + pub value: i32, + #[offset(4)] + pub scratch: i32, +} +impl HasMutableMember { + pub fn new(mut v: i32) -> Self { + Self { + value: v, + scratch: 0, + } + } + pub fn copy_from(o: Ptr) -> Self { + let __this: HasMutableMember = Self { + value: o.with(|__s| __s.value), + scratch: o.with(|__s| __s.scratch), + }; + (*copies_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for HasMutableMember { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(HasMutableMember { + value: self.value.clone(), + scratch: self.scratch.clone(), + })); + HasMutableMember::copy_from(__src.as_pointer()) + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let k: Value = Rc::new(RefCell::new(HasMutableMember::new({ 5 }))); + let f: Value i32>> = Rc::new(RefCell::new(lambda!( + { + let k: Value = Rc::new(RefCell::new(HasMutableMember::copy_from({ + k.as_pointer() + }))); + }, + || -> i32 { + return { (*k.borrow()).value }; + } + ))); + assert!((copies_0.with(|rc| *rc.borrow()) == 1)); + assert!(({ (({ (*f.borrow()).call() }) == 5) })); + assert!((copies_0.with(|rc| *rc.borrow()) == 1)); + return 0; +} +pub trait HasMutableMemberImpl { + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl HasMutableMemberImpl for Ptr { + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), value).write({ _a0.with(|__s| __s.value) }); + field!((*self), scratch).write({ _a0.with(|__s| __s.scratch) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = copies_0.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p5.3-discarded-value-in-lambda.rs b/tests/std/basic/basic.def.odr/out/refcount/p5.3-discarded-value-in-lambda.rs new file mode 100644 index 0000000000..8f6aa1e0df --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p5.3-discarded-value-in-lambda.rs @@ -0,0 +1,150 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +thread_local!( + pub static copyCount_4: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] +#[cpp_from(i32, new, value)] +#[byte_size(4)] +pub struct HasConstexprCtor { + #[offset(0)] + pub m: i32, +} +impl HasConstexprCtor { + pub fn new(mut v: i32) -> Self { + Self { m: v } + } + pub fn copy_from(o: Ptr) -> Self { + let __this: HasConstexprCtor = Self { + m: o.with(|__s| __s.m), + }; + (*copyCount_4.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for HasConstexprCtor { + fn clone(&self) -> Self { + let __src: Value = + Rc::new(RefCell::new(HasConstexprCtor { m: self.m.clone() })); + HasConstexprCtor::copy_from(__src.as_pointer()) + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructor, Default)] +#[byte_size(4)] +pub struct AssertsDtorCountAtExit { + #[offset(0)] + pub expected: i32, +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let t: Value = Rc::new(RefCell::new(LifetimeTracked::new())); + let _dtor_t = std::mem::DropGuard::new(t.clone(), |__p| __p.as_pointer().destructor()); + let f: Value> = Rc::new(RefCell::new(lambda!( + { + let t: Value = + Rc::new(RefCell::new(LifetimeTracked::copy_from({ t.as_pointer() }))); + }, + || {} + ))); + let _dtor_f = + std::mem::DropGuard::new(f.clone(), |__p| __p.as_pointer().with(|__f| __f.destroy())); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 0)); + ({ (*f.borrow()).call() }); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 0)); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 1)); + return 0; +} +pub trait AssertsDtorCountAtExitImpl { + fn destructor(&self); +} +impl AssertsDtorCountAtExitImpl for Ptr { + fn destructor(&self) { + assert!((dtorCount_2.with(|rc| *rc.borrow()) == (*self).with(|__s| __s.expected))); + } +} +pub trait HasConstexprCtorImpl { + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl HasConstexprCtorImpl for Ptr { + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), m).write({ _a0.with(|__s| __s.m) }); + return (*self).clone(); + } +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); + let _ = copyCount_4.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p6-evaluated-binding-is-captured.rs b/tests/std/basic/basic.def.odr/out/refcount/p6-evaluated-binding-is-captured.rs new file mode 100644 index 0000000000..5bb4361fc1 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p6-evaluated-binding-is-captured.rs @@ -0,0 +1,191 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +thread_local!( + pub static copyCount_4: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] +#[cpp_from(i32, new, value)] +#[byte_size(4)] +pub struct HasConstexprCtor { + #[offset(0)] + pub m: i32, +} +impl HasConstexprCtor { + pub fn new(mut v: i32) -> Self { + Self { m: v } + } + pub fn copy_from(o: Ptr) -> Self { + let __this: HasConstexprCtor = Self { + m: o.with(|__s| __s.m), + }; + (*copyCount_4.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for HasConstexprCtor { + fn clone(&self) -> Self { + let __src: Value = + Rc::new(RefCell::new(HasConstexprCtor { m: self.m.clone() })); + HasConstexprCtor::copy_from(__src.as_pointer()) + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructor, Default)] +#[byte_size(4)] +pub struct AssertsDtorCountAtExit { + #[offset(0)] + pub expected: i32, +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor, Default)] +#[byte_size(8)] +pub struct HasTwoMembers { + #[offset(0)] + #[byte_size(4)] + pub a: LifetimeTracked, + #[offset(4)] + #[byte_size(4)] + pub b: LifetimeTracked, +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let p: Value = Rc::new(RefCell::new(HasTwoMembers { + a: LifetimeTracked::new(), + b: LifetimeTracked::new(), + })); + let _dtor_p = std::mem::DropGuard::new(p.clone(), |__p| __p.as_pointer().destructor()); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 2)); + let __decomp_5: Ptr = p.as_pointer(); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 0)); + let f: Value> = Rc::new(RefCell::new(lambda!( + { + let a: Value = Rc::new(RefCell::new(LifetimeTracked::copy_from({ + field_ptr!(__decomp_5, a) + }))); + }, + || { + ({ LifetimeTrackedImpl::use_(&field_ptr!(__decomp_5, a)) }); + } + ))); + let _dtor_f = + std::mem::DropGuard::new(f.clone(), |__p| __p.as_pointer().with(|__f| __f.destroy())); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 1)); + ({ (*f.borrow()).call() }); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 1)); + assert!((useCount_3.with(|rc| *rc.borrow()) == 1)); + assert!(({ ({ (*p.borrow()).a.uses } == 0) })); + return 0; +} +pub trait AssertsDtorCountAtExitImpl { + fn destructor(&self); +} +impl AssertsDtorCountAtExitImpl for Ptr { + fn destructor(&self) { + assert!((dtorCount_2.with(|rc| *rc.borrow()) == (*self).with(|__s| __s.expected))); + } +} +pub trait HasConstexprCtorImpl { + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl HasConstexprCtorImpl for Ptr { + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), m).write({ _a0.with(|__s| __s.m) }); + return (*self).clone(); + } +} +pub trait HasTwoMembersImpl { + fn copy_assign(&self, _a0: Ptr) -> Ptr; + fn destructor(&self); +} +impl HasTwoMembersImpl for Ptr { + fn copy_assign(&self, _a0: Ptr) -> Ptr { + ({ + let _arg0: Ptr = field_ptr!(_a0, a); + LifetimeTrackedImpl::copy_assign(&field_ptr!((*self), a), _arg0) + }); + ({ + let _arg0: Ptr = field_ptr!(_a0, b); + LifetimeTrackedImpl::copy_assign(&field_ptr!((*self), b), _arg0) + }); + return (*self).clone(); + } + fn destructor(&self) { + field_ptr!(self, b).destructor(); + field_ptr!(self, a).destructor(); + } +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); + let _ = copyCount_4.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p6-unevaluated-binding-needs-no-capture.rs b/tests/std/basic/basic.def.odr/out/refcount/p6-unevaluated-binding-needs-no-capture.rs new file mode 100644 index 0000000000..b7505989f2 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p6-unevaluated-binding-needs-no-capture.rs @@ -0,0 +1,129 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor, Default)] +#[byte_size(8)] +pub struct HasTwoMembers { + #[offset(0)] + #[byte_size(4)] + pub a: LifetimeTracked, + #[offset(4)] + #[byte_size(4)] + pub b: LifetimeTracked, +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let p: Value = Rc::new(RefCell::new(HasTwoMembers { + a: LifetimeTracked::new(), + b: LifetimeTracked::new(), + })); + let _dtor_p = std::mem::DropGuard::new(p.clone(), |__p| __p.as_pointer().destructor()); + let __decomp_4: Ptr = p.as_pointer(); + let f: Value usize>> = + Rc::new(RefCell::new(FnPtr:: usize>::new(|| -> usize { + { + return 4usize; + } + }))); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 0)); + assert!(({ (({ (*f.borrow()).call() }) == 4usize) })); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 0)); + assert!((useCount_3.with(|rc| *rc.borrow()) == 0)); + return 0; +} +pub trait HasTwoMembersImpl { + fn copy_assign(&self, _a0: Ptr) -> Ptr; + fn destructor(&self); +} +impl HasTwoMembersImpl for Ptr { + fn copy_assign(&self, _a0: Ptr) -> Ptr { + ({ + let _arg0: Ptr = field_ptr!(_a0, a); + LifetimeTrackedImpl::copy_assign(&field_ptr!((*self), a), _arg0) + }); + ({ + let _arg0: Ptr = field_ptr!(_a0, b); + LifetimeTrackedImpl::copy_assign(&field_ptr!((*self), b), _arg0) + }); + return (*self).clone(); + } + fn destructor(&self) { + field_ptr!(self, b).destructor(); + field_ptr!(self, a).destructor(); + } +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p7-member-access-captures-this.rs b/tests/std/basic/basic.def.odr/out/refcount/p7-member-access-captures-this.rs new file mode 100644 index 0000000000..3abce6375c --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p7-member-access-captures-this.rs @@ -0,0 +1,143 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(8)] +pub struct HasMemberUsedInLambda { + #[offset(0)] + #[byte_size(4)] + pub x: LifetimeTracked, + #[offset(4)] + pub n: i32, +} +impl Default for HasMemberUsedInLambda { + fn default() -> Self { + { + HasMemberUsedInLambda { + x: ::default(), + n: 1, + } + } + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let c: Value = Rc::new(RefCell::new(::default())); + let _dtor_c = std::mem::DropGuard::new(c.clone(), |__p| __p.as_pointer().destructor()); + let f: Value i32>> = Rc::new(RefCell::new( + ({ HasMemberUsedInLambdaImpl::makeLambda(&c.as_pointer()) }), + )); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 0)); + assert!(({ (({ (*f.borrow()).call() }) == 1) })); + assert!(({ ({ (*c.borrow()).x.uses } == 1) })); + (*c.borrow_mut()).n = 10; + assert!(({ (({ (*f.borrow()).call() }) == 10) })); + assert!(({ ({ (*c.borrow()).x.uses } == 2) })); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 0)); + return 0; +} +pub trait HasMemberUsedInLambdaImpl { + fn makeLambda(&self) -> FnPtr i32>; + fn copy_assign(&self, _a0: Ptr) -> Ptr; + fn destructor(&self); +} +impl HasMemberUsedInLambdaImpl for Ptr { + fn makeLambda(&self) -> FnPtr i32> { + return lambda!( + { + let this_: Value> = + Rc::new(RefCell::new((*self).clone())); + }, + || -> i32 { + ({ LifetimeTrackedImpl::use_(&field_ptr!((*this_.borrow()), x)) }); + return (*this_.borrow()).with(|__s| __s.n); + } + ); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + ({ + let _arg0: Ptr = field_ptr!(_a0, x); + LifetimeTrackedImpl::copy_assign(&field_ptr!((*self), x), _arg0) + }); + field!((*self), n).write({ _a0.with(|__s| __s.n) }); + return (*self).clone(); + } + fn destructor(&self) { + field_ptr!(self, x).destructor(); + } +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p7-unevaluated-this-needs-no-capture.rs b/tests/std/basic/basic.def.odr/out/refcount/p7-unevaluated-this-needs-no-capture.rs new file mode 100644 index 0000000000..eafa84bd78 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p7-unevaluated-this-needs-no-capture.rs @@ -0,0 +1,121 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor, Default)] +#[byte_size(4)] +pub struct HasMemberUsedInLambda { + #[offset(0)] + #[byte_size(4)] + pub x: LifetimeTracked, +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let c: Value = Rc::new(RefCell::new(::default())); + let _dtor_c = std::mem::DropGuard::new(c.clone(), |__p| __p.as_pointer().destructor()); + let f: Value usize>> = Rc::new(RefCell::new( + ({ HasMemberUsedInLambdaImpl::sizer(&c.as_pointer()) }), + )); + assert!(({ (({ (*f.borrow()).call() }) == 4usize) })); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 0)); + assert!((useCount_3.with(|rc| *rc.borrow()) == 0)); + return 0; +} +pub trait HasMemberUsedInLambdaImpl { + fn sizer(&self) -> FnPtr usize>; + fn copy_assign(&self, _a0: Ptr) -> Ptr; + fn destructor(&self); +} +impl HasMemberUsedInLambdaImpl for Ptr { + fn sizer(&self) -> FnPtr usize> { + return FnPtr:: usize>::new(|| -> usize { + { + return 4usize; + } + }); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + ({ + let _arg0: Ptr = field_ptr!(_a0, x); + LifetimeTrackedImpl::copy_assign(&field_ptr!((*self), x), _arg0) + }); + return (*self).clone(); + } + fn destructor(&self) { + field_ptr!(self, x).destructor(); + } +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p8-unevaluated-call-needs-no-definition.rs b/tests/std/basic/basic.def.odr/out/refcount/p8-unevaluated-call-needs-no-definition.rs new file mode 100644 index 0000000000..f605cf5f42 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p8-unevaluated-call-needs-no-definition.rs @@ -0,0 +1,100 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +thread_local!( + pub static noisy_calls_4: Value = Rc::new(RefCell::new(0_i32)); +); +pub fn noisy_5() -> i32 { + (*noisy_calls_4.with(Value::clone).borrow_mut()).prefix_inc(); + return 1; +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + assert!((4usize == 4usize)); + assert!((::std::mem::size_of::() == ::std::mem::size_of::())); + assert!(!(false)); + assert!(!(false)); + let mut n: i32 = 3; + assert!((n == 3)); + assert!((noisy_calls_4.with(|rc| *rc.borrow()) == 0)); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0)); + assert!((({ noisy_5() }) == 1)); + assert!((noisy_calls_4.with(|rc| *rc.borrow()) == 1)); + return 0; +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); + let _ = noisy_calls_4.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p9-destructor-not-potentially-invoked.rs b/tests/std/basic/basic.def.odr/out/refcount/p9-destructor-not-potentially-invoked.rs new file mode 100644 index 0000000000..d87ee90eb3 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p9-destructor-not-potentially-invoked.rs @@ -0,0 +1,45 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static t_ctors_0: Value = Rc::new(RefCell::new(0_i32)); +); +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[byte_size(1)] +pub struct HasUndefinedDestructor {} +impl HasUndefinedDestructor { + pub fn new() -> Self { + let __this: HasUndefinedDestructor = Self {}; + (*t_ctors_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Default for HasUndefinedDestructor { + fn default() -> Self { + { HasUndefinedDestructor::new() } + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let mut p: Ptr = Ptr::alloc(HasUndefinedDestructor::new()); + assert!(!((p).is_null())); + assert!((t_ctors_0.with(|rc| *rc.borrow()) == 1)); + return 0; +} +pub trait HasUndefinedDestructorImpl { + fn destructor(&self) { + unimplemented!() + } +} +impl HasUndefinedDestructorImpl for Ptr {} +pub fn __cpp2rust_init_globals() { + let _ = t_ctors_0.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p9-unused-copy-constructor.rs b/tests/std/basic/basic.def.odr/out/refcount/p9-unused-copy-constructor.rs new file mode 100644 index 0000000000..a06f95b486 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p9-unused-copy-constructor.rs @@ -0,0 +1,45 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctor_calls_0: Value = Rc::new(RefCell::new(0_i32)); +); +#[derive(Record, ByteRepr, FnPtrArg)] +#[byte_size(4)] +pub struct HasUndefinedCopyCtor { + #[offset(0)] + pub value: i32, +} +impl HasUndefinedCopyCtor { + pub fn new() -> Self { + let __this: HasUndefinedCopyCtor = Self { value: 3 }; + (*ctor_calls_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Default for HasUndefinedCopyCtor { + fn default() -> Self { + { HasUndefinedCopyCtor::new() } + } +} +pub fn read_1(object: Ptr) -> i32 { + return object.with(|__s| __s.value); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let a: Value = Rc::new(RefCell::new(HasUndefinedCopyCtor::new())); + assert!((({ read_1(a.as_pointer(),) }) == 3)); + assert!((ctor_calls_0.with(|rc| *rc.borrow()) == 1)); + return 0; +} +pub fn __cpp2rust_init_globals() { + let _ = ctor_calls_0.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/refcount/p9-unused-implicit-assignment.rs b/tests/std/basic/basic.def.odr/out/refcount/p9-unused-implicit-assignment.rs new file mode 100644 index 0000000000..452c6a79b8 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/refcount/p9-unused-implicit-assignment.rs @@ -0,0 +1,54 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static m_ctors_0: Value = Rc::new(RefCell::new(0_i32)); +); +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[byte_size(1)] +pub struct HasUndefinedCopyAssignment {} +impl HasUndefinedCopyAssignment { + pub fn new() -> Self { + let __this: HasUndefinedCopyAssignment = Self {}; + (*m_ctors_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Default for HasUndefinedCopyAssignment { + fn default() -> Self { + { HasUndefinedCopyAssignment::new() } + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(1)] +pub struct HasMemberWithUndefinedCopyAssignment { + #[offset(0)] + #[byte_size(1)] + pub m: HasUndefinedCopyAssignment, +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let mut a: HasMemberWithUndefinedCopyAssignment = + ::default(); + let mut b: HasMemberWithUndefinedCopyAssignment = + ::default(); + assert!((m_ctors_0.with(|rc| *rc.borrow()) == 2)); + return 0; +} +pub trait HasUndefinedCopyAssignmentImpl { + fn copy_assign(&self, _a0: Ptr) -> Ptr { + unimplemented!() + } +} +impl HasUndefinedCopyAssignmentImpl for Ptr {} +pub fn __cpp2rust_init_globals() { + let _ = m_ctors_0.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p11-discarded-statement-in-template.rs b/tests/std/basic/basic.def.odr/out/unsafe/p11-discarded-statement-in-template.rs new file mode 100644 index 0000000000..cc68b2c9da --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p11-discarded-statement-in-template.rs @@ -0,0 +1,83 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +pub static mut else_runs_4: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0_i32 }); +pub unsafe fn g_5(mut _a0: i32) { + { + (*std::cell::LazyCell::force_mut(&mut *&raw mut else_runs_4)).prefix_inc(); + } +} +pub unsafe fn g_6(mut _a0: libc::c_char) { + { + (*std::cell::LazyCell::force_mut(&mut *&raw mut else_runs_4)).prefix_inc(); + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { g_5(1) }); + (unsafe { g_6(('c' as libc::c_char)) }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut else_runs_4)) == (2))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (0))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)) == (0))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); + std::cell::LazyCell::force(&*&raw const else_runs_4); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p11-discarded-statement.rs b/tests/std/basic/basic.def.odr/out/unsafe/p11-discarded-statement.rs new file mode 100644 index 0000000000..62195d121e --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p11-discarded-statement.rs @@ -0,0 +1,77 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +pub static mut else_runs_4: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0_i32 }); +pub unsafe fn g_5() { + { + (*std::cell::LazyCell::force_mut(&mut *&raw mut else_runs_4)).prefix_inc(); + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { g_5() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut else_runs_4)) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (0))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)) == (0))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); + std::cell::LazyCell::force(&*&raw const else_runs_4); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p11-undefined-variable-in-discarded-statement.rs b/tests/std/basic/basic.def.odr/out/unsafe/p11-undefined-variable-in-discarded-statement.rs new file mode 100644 index 0000000000..c3919f0863 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p11-undefined-variable-in-discarded-statement.rs @@ -0,0 +1,77 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +pub static mut else_runs_4: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0_i32 }); +pub unsafe fn g_6() { + { + (*std::cell::LazyCell::force_mut(&mut *&raw mut else_runs_4)).prefix_inc(); + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { g_6() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut else_runs_4)) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (0))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)) == (0))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); + std::cell::LazyCell::force(&*&raw const else_runs_4); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p12-inline-undefined-in-discarded-statement.rs b/tests/std/basic/basic.def.odr/out/unsafe/p12-inline-undefined-in-discarded-statement.rs new file mode 100644 index 0000000000..5d9f1975a2 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p12-inline-undefined-in-discarded-statement.rs @@ -0,0 +1,27 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut g_calls_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0_i32 }); +pub unsafe fn g_1() { + {} + (*std::cell::LazyCell::force_mut(&mut *&raw mut g_calls_0)).prefix_inc(); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { g_1() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut g_calls_0)) == (1))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const g_calls_0); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p13-pointer-to-incomplete-class.rs b/tests/std/basic/basic.def.odr/out/unsafe/p13-pointer-to-incomplete-class.rs new file mode 100644 index 0000000000..a544e0f184 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p13-pointer-to-incomplete-class.rs @@ -0,0 +1,38 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut x1_0: std::cell::LazyCell<*mut X> = + std::cell::LazyCell::new(|| unsafe { std::ptr::null_mut() }); +pub static mut x2_1: std::cell::LazyCell<*mut X> = + std::cell::LazyCell::new(|| unsafe { std::ptr::null_mut() }); +pub unsafe fn identity_2(mut p: *mut X) -> *mut X { + return p; +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + assert!((*std::cell::LazyCell::force_mut(&mut *&raw mut x1_0)).is_null()); + assert!( + ((unsafe { identity_2((*std::cell::LazyCell::force_mut(&mut *&raw mut x2_1)),) }) + == (*std::cell::LazyCell::force_mut(&mut *&raw mut x1_0))) + ); + let mut pp: *mut *mut X = + (&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut x1_0)) as *mut *mut X); + assert!((*pp).is_null()); + return 0; +} +#[derive(Clone, Copy, Default, VaArg, FnPtrArg)] +pub struct X; +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const x1_0); + std::cell::LazyCell::force(&*&raw const x2_1); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p3.2-subscript.rs b/tests/std/basic/basic.def.odr/out/unsafe/p3.2-subscript.rs new file mode 100644 index 0000000000..4f6c3c7548 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p3.2-subscript.rs @@ -0,0 +1,56 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut copyCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CppFromUnsafe, CopyAssignUnsafe, Default)] +#[cpp_from(i32, new, value)] +pub struct HasConstexprCtor { + pub m: i32, +} +impl HasConstexprCtor { + pub unsafe fn new(mut v: i32) -> Self { + let mut this = Self { m: v }; + this + } + pub unsafe fn copy_from(o: *const HasConstexprCtor) -> Self { + let mut this = Self { m: (*o).m }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_assign(&mut self, _a0: *const HasConstexprCtor) -> *mut HasConstexprCtor { + self.m = (*_a0).m; + return &mut (*(self as *mut HasConstexprCtor)); + } +} +impl Clone for HasConstexprCtor { + fn clone(&self) -> Self { + unsafe { HasConstexprCtor::copy_from(self as *const HasConstexprCtor) } + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let arr: [HasConstexprCtor; 2] = [HasConstexprCtor::new({ 3 }), HasConstexprCtor::new({ 4 })]; + let mut f: FnPtr i32> = FnPtr:: i32>::new(|| -> i32 { + unsafe { + return [HasConstexprCtor::new({ 3 }), HasConstexprCtor::new({ 4 })][(1) as usize].m; + } + }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)) == (0))); + assert!(((unsafe { f.call() }) == (4))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)) == (0))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const copyCount_0); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p3.3-member-access.rs b/tests/std/basic/basic.def.odr/out/unsafe/p3.3-member-access.rs new file mode 100644 index 0000000000..653affb1b1 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p3.3-member-access.rs @@ -0,0 +1,56 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut copyCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CppFromUnsafe, CopyAssignUnsafe, Default)] +#[cpp_from(i32, new, value)] +pub struct HasConstexprCtor { + pub m: i32, +} +impl HasConstexprCtor { + pub unsafe fn new(mut v: i32) -> Self { + let mut this = Self { m: v }; + this + } + pub unsafe fn copy_from(o: *const HasConstexprCtor) -> Self { + let mut this = Self { m: (*o).m }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_assign(&mut self, _a0: *const HasConstexprCtor) -> *mut HasConstexprCtor { + self.m = (*_a0).m; + return &mut (*(self as *mut HasConstexprCtor)); + } +} +impl Clone for HasConstexprCtor { + fn clone(&self) -> Self { + unsafe { HasConstexprCtor::copy_from(self as *const HasConstexprCtor) } + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let k: HasConstexprCtor = HasConstexprCtor::new({ 5 }); + let mut f: FnPtr i32> = FnPtr:: i32>::new(|| -> i32 { + unsafe { + return HasConstexprCtor::new({ 5 }).m; + } + }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)) == (0))); + assert!(((unsafe { f.call() }) == (5))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)) == (0))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const copyCount_0); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p3.7-conditional.rs b/tests/std/basic/basic.def.odr/out/unsafe/p3.7-conditional.rs new file mode 100644 index 0000000000..b24fe03291 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p3.7-conditional.rs @@ -0,0 +1,63 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut copyCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CppFromUnsafe, CopyAssignUnsafe, Default)] +#[cpp_from(i32, new, value)] +pub struct HasConstexprCtor { + pub m: i32, +} +impl HasConstexprCtor { + pub unsafe fn new(mut v: i32) -> Self { + let mut this = Self { m: v }; + this + } + pub unsafe fn copy_from(o: *const HasConstexprCtor) -> Self { + let mut this = Self { m: (*o).m }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_assign(&mut self, _a0: *const HasConstexprCtor) -> *mut HasConstexprCtor { + self.m = (*_a0).m; + return &mut (*(self as *mut HasConstexprCtor)); + } +} +impl Clone for HasConstexprCtor { + fn clone(&self) -> Self { + unsafe { HasConstexprCtor::copy_from(self as *const HasConstexprCtor) } + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let k1: HasConstexprCtor = HasConstexprCtor::new({ 7 }); + let k2: HasConstexprCtor = HasConstexprCtor::new({ 9 }); + let mut f: FnPtr i32> = FnPtr:: i32>::new(|mut b: bool| -> i32 { + unsafe { + return (if b { + HasConstexprCtor::new({ 7 }) + } else { + HasConstexprCtor::new({ 9 }) + }) + .m; + } + }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)) == (0))); + assert!(((unsafe { f.call(true,) }) == (7))); + assert!(((unsafe { f.call(false,) }) == (9))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)) == (0))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const copyCount_0); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p3.8-comma.rs b/tests/std/basic/basic.def.odr/out/unsafe/p3.8-comma.rs new file mode 100644 index 0000000000..01300a5bd5 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p3.8-comma.rs @@ -0,0 +1,64 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut copyCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CppFromUnsafe, CopyAssignUnsafe, Default)] +#[cpp_from(i32, new, value)] +pub struct HasConstexprCtor { + pub m: i32, +} +impl HasConstexprCtor { + pub unsafe fn new(mut v: i32) -> Self { + let mut this = Self { m: v }; + this + } + pub unsafe fn copy_from(o: *const HasConstexprCtor) -> Self { + let mut this = Self { m: (*o).m }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_assign(&mut self, _a0: *const HasConstexprCtor) -> *mut HasConstexprCtor { + self.m = (*_a0).m; + return &mut (*(self as *mut HasConstexprCtor)); + } +} +impl Clone for HasConstexprCtor { + fn clone(&self) -> Self { + unsafe { HasConstexprCtor::copy_from(self as *const HasConstexprCtor) } + } +} +pub static mut left_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0_i32 }); +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let k: HasConstexprCtor = HasConstexprCtor::new({ 5 }); + let mut f: FnPtr i32> = FnPtr:: i32>::new(|| -> i32 { + unsafe { + return { + (*std::cell::LazyCell::force_mut(&mut *&raw mut left_1)).prefix_inc(); + HasConstexprCtor::new({ 5 }) + } + .m; + } + }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)) == (0))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut left_1)) == (0))); + assert!(((unsafe { f.call() }) == (5))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut left_1)) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)) == (0))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const copyCount_0); + std::cell::LazyCell::force(&*&raw const left_1); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p4.1-pure-virtual-qualified-call.rs b/tests/std/basic/basic.def.odr/out/unsafe/p4.1-pure-virtual-qualified-call.rs new file mode 100644 index 0000000000..a6989bf591 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p4.1-pure-virtual-qualified-call.rs @@ -0,0 +1,48 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut base_calls_0: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0_i32 }); +pub static mut derived_calls_1: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0_i32 }); +pub unsafe trait HasPureVirtual { + unsafe fn f(&mut self); +} +impl HasPureVirtual { + unsafe fn f(&mut self); +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct OverridesPureVirtual {} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut d: OverridesPureVirtual = ::default(); + let mut p: *mut dyn HasPureVirtual = (&mut d as *mut OverridesPureVirtual); + (unsafe { (*p).f() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut derived_calls_1)) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut base_calls_0)) == (1))); + (unsafe { (d as HasPureVirtual).f() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut derived_calls_1)) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut base_calls_0)) == (2))); + return 0; +} +unsafe impl HasPureVirtual for OverridesPureVirtual { + unsafe fn f(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut derived_calls_1)).prefix_inc(); + (unsafe { self.f() }); + } +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const base_calls_0); + std::cell::LazyCell::force(&*&raw const derived_calls_1); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p4.1-pure-virtual-without-definition.rs b/tests/std/basic/basic.def.odr/out/unsafe/p4.1-pure-virtual-without-definition.rs new file mode 100644 index 0000000000..ca7ced08fd --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p4.1-pure-virtual-without-definition.rs @@ -0,0 +1,37 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut derived_calls_0: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0_i32 }); +pub unsafe trait HasPureVirtual { + unsafe fn f(&mut self); +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct OverridesPureVirtual {} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut d: OverridesPureVirtual = ::default(); + let mut p: *mut dyn HasPureVirtual = (&mut d as *mut OverridesPureVirtual); + (unsafe { (*p).f() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut derived_calls_0)) == (1))); + return 0; +} +unsafe impl HasPureVirtual for OverridesPureVirtual { + unsafe fn f(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut derived_calls_0)).prefix_inc(); + } +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const derived_calls_0); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p5.1-constant-reference-in-lambda.rs b/tests/std/basic/basic.def.odr/out/unsafe/p5.1-constant-reference-in-lambda.rs new file mode 100644 index 0000000000..95cacb98f4 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p5.1-constant-reference-in-lambda.rs @@ -0,0 +1,79 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +pub static mut global_4: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() }); +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let r: *mut LifetimeTracked = &mut (*std::cell::LazyCell::force_mut(&mut *&raw mut global_4)); + let mut f: FnPtr = FnPtr::::new(|| unsafe { + (unsafe { + LifetimeTracked::use_(&(*std::cell::LazyCell::force_mut(&mut *&raw mut global_4))) + }); + }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (0))); + (unsafe { f.call() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (0))); + assert!((((*std::cell::LazyCell::force_mut(&mut *&raw mut global_4)).uses) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (1))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); + std::cell::LazyCell::force(&*&raw const global_4); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p5.1-local-reference-is-odr-used.rs b/tests/std/basic/basic.def.odr/out/unsafe/p5.1-local-reference-is-odr-used.rs new file mode 100644 index 0000000000..22c0286d10 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p5.1-local-reference-is-odr-used.rs @@ -0,0 +1,82 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut t: LifetimeTracked = LifetimeTracked::new(); + let _dtor_t = std::mem::DropGuard::new(&raw mut t, |__p| unsafe { (*__p).destructor() }); + let r: *mut LifetimeTracked = &mut t; + let mut f: FnPtr = lambda_unsafe!( + { + let r: LifetimeTracked = LifetimeTracked::copy_from({ r }); + }, + || { + (unsafe { LifetimeTracked::use_(&r) }); + } + ); + let _dtor_f = std::mem::DropGuard::new(&raw mut f, |__p| unsafe { (*__p).destroy() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (1))); + (unsafe { f.call() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)) == (1))); + assert!(((t.uses) == (0))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p5.2-address-of-constant-is-odr-use.rs b/tests/std/basic/basic.def.odr/out/unsafe/p5.2-address-of-constant-is-odr-use.rs new file mode 100644 index 0000000000..da8f8cb966 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p5.2-address-of-constant-is-odr-use.rs @@ -0,0 +1,29 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let n: i32 = 5; + let mut f: FnPtr *const i32> = lambda_unsafe!( + { + let n: i32 = n; + }, + || -> *const i32 { + return (&n as *const i32); + } + ); + assert!(((unsafe { f.call() }) != (&n as *const i32))); + assert!(((*(unsafe { f.call() })) == (5))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p5.2-class-object-is-odr-used.rs b/tests/std/basic/basic.def.odr/out/unsafe/p5.2-class-object-is-odr-used.rs new file mode 100644 index 0000000000..2e412490ba --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p5.2-class-object-is-odr-used.rs @@ -0,0 +1,60 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut copyCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CppFromUnsafe, CopyAssignUnsafe, Default)] +#[cpp_from(i32, new, value)] +pub struct HasConstexprCtor { + pub m: i32, +} +impl HasConstexprCtor { + pub unsafe fn new(mut v: i32) -> Self { + let mut this = Self { m: v }; + this + } + pub unsafe fn copy_from(o: *const HasConstexprCtor) -> Self { + let mut this = Self { m: (*o).m }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_assign(&mut self, _a0: *const HasConstexprCtor) -> *mut HasConstexprCtor { + self.m = (*_a0).m; + return &mut (*(self as *mut HasConstexprCtor)); + } +} +impl Clone for HasConstexprCtor { + fn clone(&self) -> Self { + unsafe { HasConstexprCtor::copy_from(self as *const HasConstexprCtor) } + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let k: HasConstexprCtor = HasConstexprCtor::new({ 5 }); + let mut f: FnPtr i32> = lambda_unsafe!( + { + let k: HasConstexprCtor = HasConstexprCtor::copy_from({ &k }); + }, + || -> i32 { + let mut c: HasConstexprCtor = HasConstexprCtor::copy_from({ &k }); + return c.m; + } + ); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)) == (1))); + assert!(((unsafe { f.call() }) == (5))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)) == (2))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const copyCount_0); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p5.2-constant-in-lambda-without-capture.rs b/tests/std/basic/basic.def.odr/out/unsafe/p5.2-constant-in-lambda-without-capture.rs new file mode 100644 index 0000000000..97326917b0 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p5.2-constant-in-lambda-without-capture.rs @@ -0,0 +1,25 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let n: i32 = 5; + let mut f: FnPtr i32> = FnPtr:: i32>::new(|| -> i32 { + unsafe { + return 5; + } + }); + assert!(((unsafe { f.call() }) == (5))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p5.2-mutable-subobject-is-odr-used.rs b/tests/std/basic/basic.def.odr/out/unsafe/p5.2-mutable-subobject-is-odr-used.rs new file mode 100644 index 0000000000..7144f6258c --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p5.2-mutable-subobject-is-odr-used.rs @@ -0,0 +1,67 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut copies_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0_i32 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CppFromUnsafe, CopyAssignUnsafe, Default)] +#[cpp_from(i32, new, value)] +pub struct HasMutableMember { + pub value: i32, + pub scratch: i32, +} +impl HasMutableMember { + pub unsafe fn new(mut v: i32) -> Self { + let mut this = Self { + value: v, + scratch: 0, + }; + this + } + pub unsafe fn copy_from(o: *const HasMutableMember) -> Self { + let mut this = Self { + value: (*o).value, + scratch: (*o).scratch, + }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)).prefix_inc(); + this + } + pub unsafe fn copy_assign(&mut self, _a0: *const HasMutableMember) -> *mut HasMutableMember { + self.value = (*_a0).value; + self.scratch = (*_a0).scratch; + return &mut (*(self as *mut HasMutableMember)); + } +} +impl Clone for HasMutableMember { + fn clone(&self) -> Self { + unsafe { HasMutableMember::copy_from(self as *const HasMutableMember) } + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let k: HasMutableMember = HasMutableMember::new({ 5 }); + let mut f: FnPtr i32> = lambda_unsafe!( + { + let k: HasMutableMember = HasMutableMember::copy_from({ &k }); + }, + || -> i32 { + return k.value; + } + ); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (1))); + assert!(((unsafe { f.call() }) == (5))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (1))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const copies_0); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p5.3-discarded-value-in-lambda.rs b/tests/std/basic/basic.def.odr/out/unsafe/p5.3-discarded-value-in-lambda.rs new file mode 100644 index 0000000000..1c13438d11 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p5.3-discarded-value-in-lambda.rs @@ -0,0 +1,116 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +pub static mut copyCount_4: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CppFromUnsafe, CopyAssignUnsafe, Default)] +#[cpp_from(i32, new, value)] +pub struct HasConstexprCtor { + pub m: i32, +} +impl HasConstexprCtor { + pub unsafe fn new(mut v: i32) -> Self { + let mut this = Self { m: v }; + this + } + pub unsafe fn copy_from(o: *const HasConstexprCtor) -> Self { + let mut this = Self { m: (*o).m }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_4)).prefix_inc(); + this + } + pub unsafe fn copy_assign(&mut self, _a0: *const HasConstexprCtor) -> *mut HasConstexprCtor { + self.m = (*_a0).m; + return &mut (*(self as *mut HasConstexprCtor)); + } +} +impl Clone for HasConstexprCtor { + fn clone(&self) -> Self { + unsafe { HasConstexprCtor::copy_from(self as *const HasConstexprCtor) } + } +} +#[repr(C)] +#[derive(Clone, VaArg, FnPtrArg, DestructorUnsafe, Default)] +pub struct AssertsDtorCountAtExit { + pub expected: i32, +} +impl AssertsDtorCountAtExit { + pub unsafe fn destructor(&mut self) { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)) == (self.expected))); + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut t: LifetimeTracked = LifetimeTracked::new(); + let _dtor_t = std::mem::DropGuard::new(&raw mut t, |__p| unsafe { (*__p).destructor() }); + let mut f: FnPtr = lambda_unsafe!( + { + let t: LifetimeTracked = LifetimeTracked::copy_from({ &t }); + }, + || {} + ); + let _dtor_f = std::mem::DropGuard::new(&raw mut f, |__p| unsafe { (*__p).destroy() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (0))); + (unsafe { f.call() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (0))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (1))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); + std::cell::LazyCell::force(&*&raw const copyCount_4); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p6-evaluated-binding-is-captured.rs b/tests/std/basic/basic.def.odr/out/unsafe/p6-evaluated-binding-is-captured.rs new file mode 100644 index 0000000000..547b680d00 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p6-evaluated-binding-is-captured.rs @@ -0,0 +1,150 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +pub static mut copyCount_4: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CppFromUnsafe, CopyAssignUnsafe, Default)] +#[cpp_from(i32, new, value)] +pub struct HasConstexprCtor { + pub m: i32, +} +impl HasConstexprCtor { + pub unsafe fn new(mut v: i32) -> Self { + let mut this = Self { m: v }; + this + } + pub unsafe fn copy_from(o: *const HasConstexprCtor) -> Self { + let mut this = Self { m: (*o).m }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_4)).prefix_inc(); + this + } + pub unsafe fn copy_assign(&mut self, _a0: *const HasConstexprCtor) -> *mut HasConstexprCtor { + self.m = (*_a0).m; + return &mut (*(self as *mut HasConstexprCtor)); + } +} +impl Clone for HasConstexprCtor { + fn clone(&self) -> Self { + unsafe { HasConstexprCtor::copy_from(self as *const HasConstexprCtor) } + } +} +#[repr(C)] +#[derive(Clone, VaArg, FnPtrArg, DestructorUnsafe, Default)] +pub struct AssertsDtorCountAtExit { + pub expected: i32, +} +impl AssertsDtorCountAtExit { + pub unsafe fn destructor(&mut self) { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)) == (self.expected))); + } +} +#[repr(C)] +#[derive(Clone, VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe, Default)] +pub struct HasTwoMembers { + pub a: LifetimeTracked, + pub b: LifetimeTracked, +} +impl HasTwoMembers { + pub unsafe fn copy_assign(&mut self, _a0: *const HasTwoMembers) -> *mut HasTwoMembers { + (unsafe { + let _arg0: *const LifetimeTracked = &(*_a0).a; + LifetimeTracked::copy_assign(&mut self.a, _arg0) + }); + (unsafe { + let _arg0: *const LifetimeTracked = &(*_a0).b; + LifetimeTracked::copy_assign(&mut self.b, _arg0) + }); + return &mut (*(self as *mut HasTwoMembers)); + } +} +impl HasTwoMembers { + pub unsafe fn destructor(&mut self) { + LifetimeTracked::destructor(&mut self.b); + LifetimeTracked::destructor(&mut self.a); + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut p: HasTwoMembers = HasTwoMembers { + a: LifetimeTracked::new(), + b: LifetimeTracked::new(), + }; + let _dtor_p = std::mem::DropGuard::new(&raw mut p, |__p| unsafe { (*__p).destructor() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (2))); + let __decomp_5: *mut HasTwoMembers = &mut p; + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (0))); + let mut f: FnPtr = lambda_unsafe!( + { + let a: LifetimeTracked = LifetimeTracked::copy_from({ &(*__decomp_5).a }); + }, + || { + (unsafe { LifetimeTracked::use_(&(*__decomp_5).a) }); + } + ); + let _dtor_f = std::mem::DropGuard::new(&raw mut f, |__p| unsafe { (*__p).destroy() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (1))); + (unsafe { f.call() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)) == (1))); + assert!(((p.a.uses) == (0))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); + std::cell::LazyCell::force(&*&raw const copyCount_4); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p6-unevaluated-binding-needs-no-capture.rs b/tests/std/basic/basic.def.odr/out/unsafe/p6-unevaluated-binding-needs-no-capture.rs new file mode 100644 index 0000000000..b03f0eb757 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p6-unevaluated-binding-needs-no-capture.rs @@ -0,0 +1,105 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +#[repr(C)] +#[derive(Clone, VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe, Default)] +pub struct HasTwoMembers { + pub a: LifetimeTracked, + pub b: LifetimeTracked, +} +impl HasTwoMembers { + pub unsafe fn copy_assign(&mut self, _a0: *const HasTwoMembers) -> *mut HasTwoMembers { + (unsafe { + let _arg0: *const LifetimeTracked = &(*_a0).a; + LifetimeTracked::copy_assign(&mut self.a, _arg0) + }); + (unsafe { + let _arg0: *const LifetimeTracked = &(*_a0).b; + LifetimeTracked::copy_assign(&mut self.b, _arg0) + }); + return &mut (*(self as *mut HasTwoMembers)); + } +} +impl HasTwoMembers { + pub unsafe fn destructor(&mut self) { + LifetimeTracked::destructor(&mut self.b); + LifetimeTracked::destructor(&mut self.a); + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut p: HasTwoMembers = HasTwoMembers { + a: LifetimeTracked::new(), + b: LifetimeTracked::new(), + }; + let _dtor_p = std::mem::DropGuard::new(&raw mut p, |__p| unsafe { (*__p).destructor() }); + let __decomp_4: *mut HasTwoMembers = &mut p; + let mut f: FnPtr usize> = FnPtr:: usize>::new(|| -> usize { + unsafe { + return ::std::mem::size_of::(); + } + }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (0))); + assert!(((unsafe { f.call() }) == (::std::mem::size_of::()))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (0))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)) == (0))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p7-member-access-captures-this.rs b/tests/std/basic/basic.def.odr/out/unsafe/p7-member-access-captures-this.rs new file mode 100644 index 0000000000..03b40c5401 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p7-member-access-captures-this.rs @@ -0,0 +1,120 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +#[repr(C)] +#[derive(Clone, VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct HasMemberUsedInLambda { + pub x: LifetimeTracked, + pub n: i32, +} +impl HasMemberUsedInLambda { + pub unsafe fn makeLambda(&mut self) -> FnPtr i32> { + return lambda_unsafe!( + { + let this_: *mut HasMemberUsedInLambda = (self as *mut HasMemberUsedInLambda); + }, + || -> i32 { + (unsafe { LifetimeTracked::use_(&(*this_).x) }); + return (*this_).n; + } + ); + } + pub unsafe fn copy_assign( + &mut self, + _a0: *const HasMemberUsedInLambda, + ) -> *mut HasMemberUsedInLambda { + (unsafe { + let _arg0: *const LifetimeTracked = &(*_a0).x; + LifetimeTracked::copy_assign(&mut self.x, _arg0) + }); + self.n = (*_a0).n; + return &mut (*(self as *mut HasMemberUsedInLambda)); + } +} +impl HasMemberUsedInLambda { + pub unsafe fn destructor(&mut self) { + LifetimeTracked::destructor(&mut self.x); + } +} +impl Default for HasMemberUsedInLambda { + fn default() -> Self { + unsafe { + HasMemberUsedInLambda { + x: ::default(), + n: 1, + } + } + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut c: HasMemberUsedInLambda = ::default(); + let _dtor_c = std::mem::DropGuard::new(&raw mut c, |__p| unsafe { (*__p).destructor() }); + let mut f: FnPtr i32> = (unsafe { HasMemberUsedInLambda::makeLambda(&mut c) }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (0))); + assert!(((unsafe { f.call() }) == (1))); + assert!(((c.x.uses) == (1))); + c.n = 10; + assert!(((unsafe { f.call() }) == (10))); + assert!(((c.x.uses) == (2))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (0))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p7-unevaluated-this-needs-no-capture.rs b/tests/std/basic/basic.def.odr/out/unsafe/p7-unevaluated-this-needs-no-capture.rs new file mode 100644 index 0000000000..698ef473c9 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p7-unevaluated-this-needs-no-capture.rs @@ -0,0 +1,100 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +#[repr(C)] +#[derive(Clone, VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe, Default)] +pub struct HasMemberUsedInLambda { + pub x: LifetimeTracked, +} +impl HasMemberUsedInLambda { + pub unsafe fn sizer(&mut self) -> FnPtr usize> { + return FnPtr:: usize>::new(|| -> usize { + unsafe { + return ::std::mem::size_of::(); + } + }); + } + pub unsafe fn copy_assign( + &mut self, + _a0: *const HasMemberUsedInLambda, + ) -> *mut HasMemberUsedInLambda { + (unsafe { + let _arg0: *const LifetimeTracked = &(*_a0).x; + LifetimeTracked::copy_assign(&mut self.x, _arg0) + }); + return &mut (*(self as *mut HasMemberUsedInLambda)); + } +} +impl HasMemberUsedInLambda { + pub unsafe fn destructor(&mut self) { + LifetimeTracked::destructor(&mut self.x); + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut c: HasMemberUsedInLambda = ::default(); + let _dtor_c = std::mem::DropGuard::new(&raw mut c, |__p| unsafe { (*__p).destructor() }); + let mut f: FnPtr usize> = (unsafe { HasMemberUsedInLambda::sizer(&mut c) }); + assert!(((unsafe { f.call() }) == (::std::mem::size_of::()))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (0))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)) == (0))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p8-unevaluated-call-needs-no-definition.rs b/tests/std/basic/basic.def.odr/out/unsafe/p8-unevaluated-call-needs-no-definition.rs new file mode 100644 index 0000000000..b2bdbdae9e --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p8-unevaluated-call-needs-no-definition.rs @@ -0,0 +1,84 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +pub static mut noisy_calls_4: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0_i32 }); +pub unsafe fn noisy_5() -> i32 { + (*std::cell::LazyCell::force_mut(&mut *&raw mut noisy_calls_4)).prefix_inc(); + return 1; +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + assert!( + ((::std::mem::size_of::()) == (::std::mem::size_of::())) + ); + assert!(((::std::mem::size_of::()) == (::std::mem::size_of::()))); + assert!(!(false)); + assert!(!(false)); + let mut n: i32 = 3; + assert!(((n) == (3))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut noisy_calls_4)) == (0))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (0))); + assert!(((unsafe { noisy_5() }) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut noisy_calls_4)) == (1))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); + std::cell::LazyCell::force(&*&raw const noisy_calls_4); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p9-destructor-not-potentially-invoked.rs b/tests/std/basic/basic.def.odr/out/unsafe/p9-destructor-not-potentially-invoked.rs new file mode 100644 index 0000000000..7d1fd8bd23 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p9-destructor-not-potentially-invoked.rs @@ -0,0 +1,40 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut t_ctors_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0_i32 }); +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg)] +pub struct HasUndefinedDestructor {} +impl HasUndefinedDestructor { + pub unsafe fn new() -> Self { + let mut this = Self {}; + (*std::cell::LazyCell::force_mut(&mut *&raw mut t_ctors_0)).prefix_inc(); + this + } +} +impl Default for HasUndefinedDestructor { + fn default() -> Self { + unsafe { HasUndefinedDestructor::new() } + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut p: *mut HasUndefinedDestructor = + (Box::leak(Box::new(HasUndefinedDestructor::new())) as *mut HasUndefinedDestructor); + assert!(!((p).is_null())); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut t_ctors_0)) == (1))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const t_ctors_0); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p9-unused-copy-constructor.rs b/tests/std/basic/basic.def.odr/out/unsafe/p9-unused-copy-constructor.rs new file mode 100644 index 0000000000..26571439b9 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p9-unused-copy-constructor.rs @@ -0,0 +1,45 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctor_calls_0: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0_i32 }); +#[repr(C)] +#[derive(FnPtrArg)] +pub struct HasUndefinedCopyCtor { + pub value: i32, +} +impl HasUndefinedCopyCtor { + pub unsafe fn new() -> Self { + let mut this = Self { value: 3 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctor_calls_0)).prefix_inc(); + this + } +} +impl Default for HasUndefinedCopyCtor { + fn default() -> Self { + unsafe { HasUndefinedCopyCtor::new() } + } +} +pub unsafe fn read_1(object: *const HasUndefinedCopyCtor) -> i32 { + return (*object).value; +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut a: HasUndefinedCopyCtor = HasUndefinedCopyCtor::new(); + assert!(((unsafe { read_1(&a,) }) == (3))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctor_calls_0)) == (1))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctor_calls_0); +} diff --git a/tests/std/basic/basic.def.odr/out/unsafe/p9-unused-implicit-assignment.rs b/tests/std/basic/basic.def.odr/out/unsafe/p9-unused-implicit-assignment.rs new file mode 100644 index 0000000000..b879f8f9e0 --- /dev/null +++ b/tests/std/basic/basic.def.odr/out/unsafe/p9-unused-implicit-assignment.rs @@ -0,0 +1,46 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut m_ctors_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0_i32 }); +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg)] +pub struct HasUndefinedCopyAssignment {} +impl HasUndefinedCopyAssignment { + pub unsafe fn new() -> Self { + let mut this = Self {}; + (*std::cell::LazyCell::force_mut(&mut *&raw mut m_ctors_0)).prefix_inc(); + this + } +} +impl Default for HasUndefinedCopyAssignment { + fn default() -> Self { + unsafe { HasUndefinedCopyAssignment::new() } + } +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct HasMemberWithUndefinedCopyAssignment { + pub m: HasUndefinedCopyAssignment, +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut a: HasMemberWithUndefinedCopyAssignment = + ::default(); + let mut b: HasMemberWithUndefinedCopyAssignment = + ::default(); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut m_ctors_0)) == (2))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const m_ctors_0); +} diff --git a/tests/std/basic/basic.def.odr/p11-discarded-statement-in-template.cpp b/tests/std/basic/basic.def.odr/p11-discarded-statement-in-template.cpp new file mode 100644 index 0000000000..2109b89302 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p11-discarded-statement-in-template.cpp @@ -0,0 +1,29 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/11: a definition is required only for functions +// odr-used outside of a discarded statement. In a template the discarded +// branch is not instantiated; undefined() is called only there, so it is never +// defined and never runs. +#include + +#include "../../support/lifetime-tracked.h" + +static int else_runs; + +LifetimeTracked undefined(); + +template void g(T) { + if constexpr (sizeof(T) > 1000) { + undefined().use(); + } else { + ++else_runs; + } +} + +int main() { + g(1); + g('c'); + assert(else_runs == 2); + assert(LifetimeTracked::ctorCount == 0); + assert(LifetimeTracked::useCount == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p11-discarded-statement.cpp b/tests/std/basic/basic.def.odr/p11-discarded-statement.cpp new file mode 100644 index 0000000000..7b558b4429 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p11-discarded-statement.cpp @@ -0,0 +1,27 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/11: a definition is required only for functions +// odr-used outside of a discarded statement. undefined() is called only in a +// discarded statement, so it is never defined and never runs. +#include + +#include "../../support/lifetime-tracked.h" + +static int else_runs; + +LifetimeTracked undefined(); + +void g() { + if constexpr (false) { + undefined().use(); + } else { + ++else_runs; + } +} + +int main() { + g(); + assert(else_runs == 1); + assert(LifetimeTracked::ctorCount == 0); + assert(LifetimeTracked::useCount == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p11-undefined-variable-in-discarded-statement.cpp b/tests/std/basic/basic.def.odr/p11-undefined-variable-in-discarded-statement.cpp new file mode 100644 index 0000000000..129d2582c5 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p11-undefined-variable-in-discarded-statement.cpp @@ -0,0 +1,27 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/11: a definition is required only for variables +// odr-used outside of a discarded statement. undefined is used only in a +// discarded statement, so it is never defined. +#include + +#include "../../support/lifetime-tracked.h" + +static int else_runs; + +extern LifetimeTracked undefined; + +void g() { + if constexpr (false) { + undefined.use(); + } else { + ++else_runs; + } +} + +int main() { + g(); + assert(else_runs == 1); + assert(LifetimeTracked::ctorCount == 0); + assert(LifetimeTracked::useCount == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p12-inline-undefined-in-discarded-statement.cpp b/tests/std/basic/basic.def.odr/p12-inline-undefined-in-discarded-statement.cpp new file mode 100644 index 0000000000..d9c6c5374d --- /dev/null +++ b/tests/std/basic/basic.def.odr/p12-inline-undefined-in-discarded-statement.cpp @@ -0,0 +1,22 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/12: an inline function needs a reachable definition +// only where it is odr-used outside of a discarded statement. f is used only in +// a discarded statement, so it is never defined and never runs. +#include + +static int g_calls; + +inline void f(); + +void g() { + if constexpr (false) { + f(); + } + ++g_calls; +} + +int main() { + g(); + assert(g_calls == 1); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p13-pointer-to-incomplete-class.cpp b/tests/std/basic/basic.def.odr/p13-pointer-to-incomplete-class.cpp new file mode 100644 index 0000000000..7ddee346b5 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p13-pointer-to-incomplete-class.cpp @@ -0,0 +1,18 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/13, Example 4: X is never defined. Forming, copying and +// comparing pointers to X does not require X to be complete. +#include + +struct X; +struct X *x1; +X *x2; + +X *identity(X *p) { return p; } + +int main() { + assert(x1 == nullptr); + assert(identity(x2) == x1); + X **pp = &x1; + assert(*pp == nullptr); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p14-class-in-two-files/CMakeLists.txt b/tests/std/basic/basic.def.odr/p14-class-in-two-files/CMakeLists.txt new file mode 100644 index 0000000000..c204d3f2ab --- /dev/null +++ b/tests/std/basic/basic.def.odr/p14-class-in-two-files/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.20) +project(p14_class_in_two_files LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +add_executable(app a.cpp b.cpp) diff --git a/tests/std/basic/basic.def.odr/p14-class-in-two-files/a.cpp b/tests/std/basic/basic.def.odr/p14-class-in-two-files/a.cpp new file mode 100644 index 0000000000..17f3ad662f --- /dev/null +++ b/tests/std/basic/basic.def.odr/p14-class-in-two-files/a.cpp @@ -0,0 +1,16 @@ +#include + +#include "h.h" + +int main() { + HasInlineMembers::shared.use(); + HasInlineMembers::instance().use(); + b_use(); + assert(LifetimeTracked::ctorCount == 2); + assert(HasInlineMembers::shared.uses == 2); + assert(HasInlineMembers::instance().uses == 2); + assert(&HasInlineMembers::shared == b_shared_address()); + assert(&HasInlineMembers::instance() == b_instance_address()); + assert(LifetimeTracked::copyCount == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p14-class-in-two-files/b.cpp b/tests/std/basic/basic.def.odr/p14-class-in-two-files/b.cpp new file mode 100644 index 0000000000..bf04b66e58 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p14-class-in-two-files/b.cpp @@ -0,0 +1,10 @@ +#include "h.h" + +void b_use() { + HasInlineMembers::shared.use(); + HasInlineMembers::instance().use(); +} + +LifetimeTracked *b_shared_address() { return &HasInlineMembers::shared; } + +LifetimeTracked *b_instance_address() { return &HasInlineMembers::instance(); } diff --git a/tests/std/basic/basic.def.odr/p14-class-in-two-files/h.h b/tests/std/basic/basic.def.odr/p14-class-in-two-files/h.h new file mode 100644 index 0000000000..7e37538241 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p14-class-in-two-files/h.h @@ -0,0 +1,23 @@ +// N4950 [basic.def.odr]/14 and /15: a class may be defined in several +// translation units. A member function defined in the class and an inline +// static data member are then single entities: one static local variable and +// one object, each constructed once and destroyed once. +#pragma once + +#include "../../../support/asserts-dtor-count-at-exit.h" +#include "../../../support/lifetime-tracked.h" + +inline AssertsDtorCountAtExit check{2}; + +struct HasInlineMembers { + static inline LifetimeTracked shared; + + static LifetimeTracked &instance() { + static LifetimeTracked local; + return local; + } +}; + +void b_use(); +LifetimeTracked *b_shared_address(); +LifetimeTracked *b_instance_address(); diff --git a/tests/std/basic/basic.def.odr/p14-class-in-two-files/out/refcount/p14-class-in-two-files.rs b/tests/std/basic/basic.def.odr/p14-class-in-two-files/out/refcount/p14-class-in-two-files.rs new file mode 100644 index 0000000000..1f68bc14db --- /dev/null +++ b/tests/std/basic/basic.def.odr/p14-class-in-two-files/out/refcount/p14-class-in-two-files.rs @@ -0,0 +1,135 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructor, Default)] +#[byte_size(4)] +pub struct AssertsDtorCountAtExit { + #[offset(0)] + pub expected: i32, +} +thread_local!( + pub static check_4: Value = + Rc::new(RefCell::new(AssertsDtorCountAtExit { expected: 2 })); +); +thread_local!( + pub static shared_5: Value = Rc::new(RefCell::new(LifetimeTracked::new())); +); +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(1)] +pub struct HasInlineMembers {} +impl HasInlineMembers { + pub fn instance() -> Ptr { + return local_6.with(|v| v.as_pointer()); + } +} +thread_local!( + static local_6: Value = Rc::new(RefCell::new(LifetimeTracked::new())); +); +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ LifetimeTrackedImpl::use_(&shared_5.with(|v| v.as_pointer())) }); + ({ LifetimeTrackedImpl::use_(&({ HasInlineMembers::instance() })) }); + ({ b_use_7() }); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 2)); + assert!(({ ({ (*shared_5.with(Value::clone).borrow()).uses } == 2) })); + assert!((({ HasInlineMembers::instance() }).with(|__s| __s.uses) == 2)); + assert!(((shared_5.with(|v| v.as_pointer())) == ({ b_shared_address_8() }))); + assert!((({ HasInlineMembers::instance() }) == ({ b_instance_address_9() }))); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 0)); + return 0; +} +pub fn b_use_7() { + ({ LifetimeTrackedImpl::use_(&shared_5.with(|v| v.as_pointer())) }); + ({ LifetimeTrackedImpl::use_(&({ HasInlineMembers::instance() })) }); +} +pub fn b_shared_address_8() -> Ptr { + return (shared_5.with(|v| v.as_pointer())); +} +pub fn b_instance_address_9() -> Ptr { + return ({ HasInlineMembers::instance() }); +} +pub trait AssertsDtorCountAtExitImpl { + fn destructor(&self); +} +impl AssertsDtorCountAtExitImpl for Ptr { + fn destructor(&self) { + assert!((dtorCount_2.with(|rc| *rc.borrow()) == (*self).with(|__s| __s.expected))); + } +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); + let _ = check_4.with(|_| ()); + let _ = shared_5.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/p14-class-in-two-files/out/unsafe/p14-class-in-two-files.rs b/tests/std/basic/basic.def.odr/p14-class-in-two-files/out/unsafe/p14-class-in-two-files.rs new file mode 100644 index 0000000000..80f51d4a8d --- /dev/null +++ b/tests/std/basic/basic.def.odr/p14-class-in-two-files/out/unsafe/p14-class-in-two-files.rs @@ -0,0 +1,115 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +#[repr(C)] +#[derive(Clone, VaArg, FnPtrArg, DestructorUnsafe, Default)] +pub struct AssertsDtorCountAtExit { + pub expected: i32, +} +impl AssertsDtorCountAtExit { + pub unsafe fn destructor(&mut self) { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)) == (self.expected))); + } +} +pub static mut check_4: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { AssertsDtorCountAtExit { expected: 2 } }); +pub static mut shared_5: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() }); +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct HasInlineMembers {} +impl HasInlineMembers { + pub unsafe fn instance() -> *mut LifetimeTracked { + return &mut (*std::cell::LazyCell::force_mut(&mut *&raw mut local_6)); + } +} +static mut local_6: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() }); +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { LifetimeTracked::use_(&(*std::cell::LazyCell::force_mut(&mut *&raw mut shared_5))) }); + (unsafe { LifetimeTracked::use_(&(*(unsafe { HasInlineMembers::instance() }))) }); + (unsafe { b_use_7() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (2))); + assert!((((*std::cell::LazyCell::force_mut(&mut *&raw mut shared_5)).uses) == (2))); + assert!((((*(unsafe { HasInlineMembers::instance() })).uses) == (2))); + assert!( + ((&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut shared_5)) + as *mut LifetimeTracked) + == (unsafe { b_shared_address_8() })) + ); + assert!(((unsafe { HasInlineMembers::instance() }) == (unsafe { b_instance_address_9() }))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (0))); + return 0; +} +pub unsafe fn b_use_7() { + (unsafe { LifetimeTracked::use_(&(*std::cell::LazyCell::force_mut(&mut *&raw mut shared_5))) }); + (unsafe { LifetimeTracked::use_(&(*(unsafe { HasInlineMembers::instance() }))) }); +} +pub unsafe fn b_shared_address_8() -> *mut LifetimeTracked { + return (&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut shared_5)) + as *mut LifetimeTracked); +} +pub unsafe fn b_instance_address_9() -> *mut LifetimeTracked { + return (unsafe { HasInlineMembers::instance() }); +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); + std::cell::LazyCell::force(&*&raw const check_4); + std::cell::LazyCell::force(&*&raw const shared_5); +} diff --git a/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/CMakeLists.txt b/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/CMakeLists.txt new file mode 100644 index 0000000000..a572cd5054 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.20) +project(p14_5_1_inline_function_reads_per_file_constant LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +add_executable(app a.cpp b.cpp) diff --git a/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/a.cpp b/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/a.cpp new file mode 100644 index 0000000000..099798ab92 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/a.cpp @@ -0,0 +1,11 @@ +#include + +#include "h.h" + +int main() { + assert(&factor != b_factor_address()); + assert(&scaled == b_scaled_address()); + assert(scaled(2) == 6); + assert(b_scaled(2) == 6); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/b.cpp b/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/b.cpp new file mode 100644 index 0000000000..848465ce25 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/b.cpp @@ -0,0 +1,7 @@ +#include "h.h" + +const int *b_factor_address() { return &factor; } + +int (*b_scaled_address())(int) { return &scaled; } + +int b_scaled(int v) { return scaled(v); } diff --git a/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/h.h b/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/h.h new file mode 100644 index 0000000000..2900f41af9 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/h.h @@ -0,0 +1,14 @@ +// N4950 [basic.def.odr]/14.5.1: an inline function defined in several +// translation units may name a const object with internal linkage, although +// that is a different object in each translation unit, provided the object has +// the same value everywhere and is only read by value. factor is such an +// object: each file has its own, and scaled is still one function. +#pragma once + +const int factor = 3; + +inline int scaled(int v) { return v * factor; } + +const int *b_factor_address(); +int (*b_scaled_address())(int); +int b_scaled(int v); diff --git a/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/out/refcount/p14.5.1-inline-function-reads-per-file-constant.rs b/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/out/refcount/p14.5.1-inline-function-reads-per-file-constant.rs new file mode 100644 index 0000000000..4d0260cbf4 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/out/refcount/p14.5.1-inline-function-reads-per-file-constant.rs @@ -0,0 +1,37 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static factor_0: Value = Rc::new(RefCell::new(3)); +); +pub fn scaled_1(mut v: i32) -> i32 { + return (v * factor_0.with(|rc| *rc.borrow())); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + assert!(((factor_0.with(|v| v.as_pointer())) != ({ b_factor_address_2() }))); + assert!(((FnPtr:: i32>::new(scaled_1)) == ({ b_scaled_address_3() }))); + assert!((({ scaled_1(2,) }) == 6)); + assert!((({ b_scaled_4(2,) }) == 6)); + return 0; +} +pub fn b_factor_address_2() -> Ptr { + return (factor_0.with(|v| v.as_pointer())); +} +pub fn b_scaled_address_3() -> FnPtr i32> { + return (FnPtr:: i32>::new(scaled_1)); +} +pub fn b_scaled_4(mut v: i32) -> i32 { + return ({ scaled_1(v) }); +} +pub fn __cpp2rust_init_globals() { + let _ = factor_0.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/out/unsafe/p14.5.1-inline-function-reads-per-file-constant.rs b/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/out/unsafe/p14.5.1-inline-function-reads-per-file-constant.rs new file mode 100644 index 0000000000..044ff38d0b --- /dev/null +++ b/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/out/unsafe/p14.5.1-inline-function-reads-per-file-constant.rs @@ -0,0 +1,40 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut factor_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 3 }); +pub unsafe fn scaled_1(mut v: i32) -> i32 { + return ((v) * (*std::cell::LazyCell::force_mut(&mut *&raw mut factor_0))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + assert!( + ((&raw const (*std::cell::LazyCell::force_mut(&mut *&raw mut factor_0)) as *const i32) + != (unsafe { b_factor_address_2() })) + ); + assert!(((Some(scaled_1)) == (unsafe { b_scaled_address_3() }))); + assert!(((unsafe { scaled_1(2,) }) == (6))); + assert!(((unsafe { b_scaled_4(2,) }) == (6))); + return 0; +} +pub unsafe fn b_factor_address_2() -> *const i32 { + return (&raw const (*std::cell::LazyCell::force_mut(&mut *&raw mut factor_0)) as *const i32); +} +pub unsafe fn b_scaled_address_3() -> Option i32> { + return (Some(scaled_1)); +} +pub unsafe fn b_scaled_4(mut v: i32) -> i32 { + return (unsafe { scaled_1(v) }); +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const factor_0); +} diff --git a/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/test.expectations b/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/test.expectations new file mode 100644 index 0000000000..b147980970 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p14.5.1-inline-function-reads-per-file-constant/test.expectations @@ -0,0 +1,5 @@ +// panic: refcount +// no-compile: unsafe +// The converter emits one object for a constant that has internal linkage in +// each file, and in the unsafe model compares a function with a function +// pointer of a different type. diff --git a/tests/std/basic/basic.def.odr/p15-class-template-static-member/CMakeLists.txt b/tests/std/basic/basic.def.odr/p15-class-template-static-member/CMakeLists.txt new file mode 100644 index 0000000000..1d28aeea84 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-class-template-static-member/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.20) +project(p15_class_template_static_member LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +add_executable(app a.cpp b.cpp) diff --git a/tests/std/basic/basic.def.odr/p15-class-template-static-member/a.cpp b/tests/std/basic/basic.def.odr/p15-class-template-static-member/a.cpp new file mode 100644 index 0000000000..787d3e845b --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-class-template-static-member/a.cpp @@ -0,0 +1,15 @@ +#include + +#include "h.h" + +int main() { + HasStaticMember::member.use(); + b_use_int(); + b_use_long(); + assert(HasStaticMember::member.uses == 2); + assert(HasStaticMember::member.uses == 1); + assert(&HasStaticMember::member == b_int_address()); + assert(LifetimeTracked::ctorCount == 2); + assert(LifetimeTracked::copyCount == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p15-class-template-static-member/b.cpp b/tests/std/basic/basic.def.odr/p15-class-template-static-member/b.cpp new file mode 100644 index 0000000000..65fe7d2a9f --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-class-template-static-member/b.cpp @@ -0,0 +1,7 @@ +#include "h.h" + +void b_use_int() { HasStaticMember::member.use(); } + +void b_use_long() { HasStaticMember::member.use(); } + +LifetimeTracked *b_int_address() { return &HasStaticMember::member; } diff --git a/tests/std/basic/basic.def.odr/p15-class-template-static-member/h.h b/tests/std/basic/basic.def.odr/p15-class-template-static-member/h.h new file mode 100644 index 0000000000..6860be6688 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-class-template-static-member/h.h @@ -0,0 +1,16 @@ +// N4950 [basic.def.odr]/15: a static data member of a class template may be +// defined in several translation units and behaves as a single entity: one +// object per specialization. +#pragma once + +#include "../../../support/lifetime-tracked.h" + +template struct HasStaticMember { + static LifetimeTracked member; +}; + +template LifetimeTracked HasStaticMember::member; + +void b_use_int(); +void b_use_long(); +LifetimeTracked *b_int_address(); diff --git a/tests/std/basic/basic.def.odr/p15-class-template-static-member/out/refcount/p15-class-template-static-member.rs b/tests/std/basic/basic.def.odr/p15-class-template-static-member/out/refcount/p15-class-template-static-member.rs new file mode 100644 index 0000000000..e5a214a38b --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-class-template-static-member/out/refcount/p15-class-template-static-member.rs @@ -0,0 +1,113 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +thread_local!( + pub static member_4: Value = Rc::new(RefCell::new(LifetimeTracked::new())); +); +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(1)] +pub struct HasStaticMember_int_ {} +thread_local!( + pub static member_5: Value = Rc::new(RefCell::new(LifetimeTracked::new())); +); +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(1)] +pub struct HasStaticMember_long_ {} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ LifetimeTrackedImpl::use_(&member_4.with(|v| v.as_pointer())) }); + ({ b_use_int_6() }); + ({ b_use_long_7() }); + assert!(({ ({ (*member_4.with(Value::clone).borrow()).uses } == 2) })); + assert!(({ ({ (*member_5.with(Value::clone).borrow()).uses } == 1) })); + assert!(((member_4.with(|v| v.as_pointer())) == ({ b_int_address_8() }))); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 2)); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 0)); + return 0; +} +pub fn b_use_int_6() { + ({ LifetimeTrackedImpl::use_(&member_4.with(|v| v.as_pointer())) }); +} +pub fn b_use_long_7() { + ({ LifetimeTrackedImpl::use_(&member_5.with(|v| v.as_pointer())) }); +} +pub fn b_int_address_8() -> Ptr { + return (member_4.with(|v| v.as_pointer())); +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); + let _ = member_4.with(|_| ()); + let _ = member_5.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/p15-class-template-static-member/out/unsafe/p15-class-template-static-member.rs b/tests/std/basic/basic.def.odr/p15-class-template-static-member/out/unsafe/p15-class-template-static-member.rs new file mode 100644 index 0000000000..e82c2f66c3 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-class-template-static-member/out/unsafe/p15-class-template-static-member.rs @@ -0,0 +1,99 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +pub static mut member_4: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() }); +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct HasStaticMember_int_ {} +pub static mut member_5: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() }); +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct HasStaticMember_long_ {} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { LifetimeTracked::use_(&(*std::cell::LazyCell::force_mut(&mut *&raw mut member_4))) }); + (unsafe { b_use_int_6() }); + (unsafe { b_use_long_7() }); + assert!((((*std::cell::LazyCell::force_mut(&mut *&raw mut member_4)).uses) == (2))); + assert!((((*std::cell::LazyCell::force_mut(&mut *&raw mut member_5)).uses) == (1))); + assert!( + ((&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut member_4)) + as *mut LifetimeTracked) + == (unsafe { b_int_address_8() })) + ); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (2))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (0))); + return 0; +} +pub unsafe fn b_use_int_6() { + (unsafe { LifetimeTracked::use_(&(*std::cell::LazyCell::force_mut(&mut *&raw mut member_4))) }); +} +pub unsafe fn b_use_long_7() { + (unsafe { LifetimeTracked::use_(&(*std::cell::LazyCell::force_mut(&mut *&raw mut member_5))) }); +} +pub unsafe fn b_int_address_8() -> *mut LifetimeTracked { + return (&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut member_4)) + as *mut LifetimeTracked); +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); + std::cell::LazyCell::force(&*&raw const member_4); + std::cell::LazyCell::force(&*&raw const member_5); +} diff --git a/tests/std/basic/basic.def.odr/p15-function-template/CMakeLists.txt b/tests/std/basic/basic.def.odr/p15-function-template/CMakeLists.txt new file mode 100644 index 0000000000..84598b92bd --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-function-template/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.20) +project(p15_function_template LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +add_executable(app a.cpp b.cpp) diff --git a/tests/std/basic/basic.def.odr/p15-function-template/a.cpp b/tests/std/basic/basic.def.odr/p15-function-template/a.cpp new file mode 100644 index 0000000000..6462d0bf58 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-function-template/a.cpp @@ -0,0 +1,18 @@ +#include + +#include "../../../support/asserts-dtor-count-at-exit.h" +#include "h.h" + +AssertsDtorCountAtExit check{2}; + +int main() { + assert(LifetimeTracked::ctorCount == 0); + assert(count() == 1); + assert(b_count_int() == 2); + assert(count() == 3); + assert(LifetimeTracked::ctorCount == 1); + assert(count() == 1); + assert(b_count_long() == 2); + assert(LifetimeTracked::ctorCount == 2); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p15-function-template/b.cpp b/tests/std/basic/basic.def.odr/p15-function-template/b.cpp new file mode 100644 index 0000000000..1068ec9648 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-function-template/b.cpp @@ -0,0 +1,5 @@ +#include "h.h" + +int b_count_int() { return count(); } + +int b_count_long() { return count(); } diff --git a/tests/std/basic/basic.def.odr/p15-function-template/h.h b/tests/std/basic/basic.def.odr/p15-function-template/h.h new file mode 100644 index 0000000000..59a1696008 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-function-template/h.h @@ -0,0 +1,16 @@ +// N4950 [basic.def.odr]/15: a template defined in several translation units +// behaves as a single entity, so the same specialization instantiated in two +// translation units has one set of static local variables, constructed once and +// destroyed once. Different specializations have their own. +#pragma once + +#include "../../../support/lifetime-tracked.h" + +template int count() { + static LifetimeTracked t; + static int n = 0; + return ++n; +} + +int b_count_int(); +int b_count_long(); diff --git a/tests/std/basic/basic.def.odr/p15-function-template/out/refcount/p15-function-template.rs b/tests/std/basic/basic.def.odr/p15-function-template/out/refcount/p15-function-template.rs new file mode 100644 index 0000000000..5c75474fd6 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-function-template/out/refcount/p15-function-template.rs @@ -0,0 +1,133 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructor, Default)] +#[byte_size(4)] +pub struct AssertsDtorCountAtExit { + #[offset(0)] + pub expected: i32, +} +pub fn count_4() -> i32 { + return (*n_6.with(Value::clone).borrow_mut()).prefix_inc(); +} +pub fn count_7() -> i32 { + return (*n_9.with(Value::clone).borrow_mut()).prefix_inc(); +} +thread_local!( + static t_5: Value = Rc::new(RefCell::new(LifetimeTracked::new())); +); +thread_local!( + static n_6: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + static t_8: Value = Rc::new(RefCell::new(LifetimeTracked::new())); +); +thread_local!( + static n_9: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static check_10: Value = + Rc::new(RefCell::new(AssertsDtorCountAtExit { expected: 2 })); +); +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0)); + assert!((({ count_4() }) == 1)); + assert!((({ b_count_int_11() }) == 2)); + assert!((({ count_4() }) == 3)); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 1)); + assert!((({ count_7() }) == 1)); + assert!((({ b_count_long_12() }) == 2)); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 2)); + return 0; +} +pub fn b_count_int_11() -> i32 { + return ({ count_4() }); +} +pub fn b_count_long_12() -> i32 { + return ({ count_7() }); +} +pub trait AssertsDtorCountAtExitImpl { + fn destructor(&self); +} +impl AssertsDtorCountAtExitImpl for Ptr { + fn destructor(&self) { + assert!((dtorCount_2.with(|rc| *rc.borrow()) == (*self).with(|__s| __s.expected))); + } +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); + let _ = check_10.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/p15-function-template/out/unsafe/p15-function-template.rs b/tests/std/basic/basic.def.odr/p15-function-template/out/unsafe/p15-function-template.rs new file mode 100644 index 0000000000..5d4d3276ae --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-function-template/out/unsafe/p15-function-template.rs @@ -0,0 +1,104 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +#[repr(C)] +#[derive(Clone, VaArg, FnPtrArg, DestructorUnsafe, Default)] +pub struct AssertsDtorCountAtExit { + pub expected: i32, +} +impl AssertsDtorCountAtExit { + pub unsafe fn destructor(&mut self) { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)) == (self.expected))); + } +} +pub unsafe fn count_4() -> i32 { + return (*std::cell::LazyCell::force_mut(&mut *&raw mut n_6)).prefix_inc(); +} +pub unsafe fn count_7() -> i32 { + return (*std::cell::LazyCell::force_mut(&mut *&raw mut n_9)).prefix_inc(); +} +static mut t_5: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() }); +static mut n_6: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +static mut t_8: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() }); +static mut n_9: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut check_10: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { AssertsDtorCountAtExit { expected: 2 } }); +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (0))); + assert!(((unsafe { count_4() }) == (1))); + assert!(((unsafe { b_count_int_11() }) == (2))); + assert!(((unsafe { count_4() }) == (3))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (1))); + assert!(((unsafe { count_7() }) == (1))); + assert!(((unsafe { b_count_long_12() }) == (2))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (2))); + return 0; +} +pub unsafe fn b_count_int_11() -> i32 { + return (unsafe { count_4() }); +} +pub unsafe fn b_count_long_12() -> i32 { + return (unsafe { count_7() }); +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); + std::cell::LazyCell::force(&*&raw const check_10); +} diff --git a/tests/std/basic/basic.def.odr/p15-function-template/test.expectations b/tests/std/basic/basic.def.odr/p15-function-template/test.expectations new file mode 100644 index 0000000000..36b34de9d4 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-function-template/test.expectations @@ -0,0 +1,3 @@ +// panic: refcount, unsafe +// The converter initializes a static local lazily, so one that is never used +// is never constructed. diff --git a/tests/std/basic/basic.def.odr/p15-inline-function/CMakeLists.txt b/tests/std/basic/basic.def.odr/p15-inline-function/CMakeLists.txt new file mode 100644 index 0000000000..0c4b775c56 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-inline-function/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.20) +project(p15_inline_function LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +add_executable(app a.cpp b.cpp) diff --git a/tests/std/basic/basic.def.odr/p15-inline-function/a.cpp b/tests/std/basic/basic.def.odr/p15-inline-function/a.cpp new file mode 100644 index 0000000000..4e62fd8048 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-inline-function/a.cpp @@ -0,0 +1,19 @@ +#include + +#include "../../../support/asserts-dtor-count-at-exit.h" +#include "h.h" + +AssertsDtorCountAtExit check{1}; + +int main() { + assert(LifetimeTracked::ctorCount == 0); + instance().use(); + b_use(); + assert(LifetimeTracked::ctorCount == 1); + assert(LifetimeTracked::useCount == 2); + assert(instance().uses == 2); + assert(&instance() == b_address()); + assert(LifetimeTracked::ctorCount == 1); + assert(LifetimeTracked::copyCount == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p15-inline-function/b.cpp b/tests/std/basic/basic.def.odr/p15-inline-function/b.cpp new file mode 100644 index 0000000000..1316ce0053 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-inline-function/b.cpp @@ -0,0 +1,5 @@ +#include "h.h" + +void b_use() { instance().use(); } + +LifetimeTracked *b_address() { return &instance(); } diff --git a/tests/std/basic/basic.def.odr/p15-inline-function/h.h b/tests/std/basic/basic.def.odr/p15-inline-function/h.h new file mode 100644 index 0000000000..1b8d0b388b --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-inline-function/h.h @@ -0,0 +1,14 @@ +// N4950 [basic.def.odr]/15: an inline function defined in several translation +// units behaves as a single entity with a single definition: one address and +// one static local variable, constructed once and destroyed once. +#pragma once + +#include "../../../support/lifetime-tracked.h" + +inline LifetimeTracked &instance() { + static LifetimeTracked t; + return t; +} + +void b_use(); +LifetimeTracked *b_address(); diff --git a/tests/std/basic/basic.def.odr/p15-inline-function/out/refcount/p15-inline-function.rs b/tests/std/basic/basic.def.odr/p15-inline-function/out/refcount/p15-inline-function.rs new file mode 100644 index 0000000000..4c66c42c17 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-inline-function/out/refcount/p15-inline-function.rs @@ -0,0 +1,122 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructor, Default)] +#[byte_size(4)] +pub struct AssertsDtorCountAtExit { + #[offset(0)] + pub expected: i32, +} +pub fn instance_4() -> Ptr { + return t_5.with(|v| v.as_pointer()); +} +thread_local!( + static t_5: Value = Rc::new(RefCell::new(LifetimeTracked::new())); +); +thread_local!( + pub static check_6: Value = + Rc::new(RefCell::new(AssertsDtorCountAtExit { expected: 1 })); +); +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0)); + ({ LifetimeTrackedImpl::use_(&({ instance_4() })) }); + ({ b_use_7() }); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 1)); + assert!((useCount_3.with(|rc| *rc.borrow()) == 2)); + assert!((({ instance_4() }).with(|__s| __s.uses) == 2)); + assert!((({ instance_4() }) == ({ b_address_8() }))); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 1)); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 0)); + return 0; +} +pub fn b_use_7() { + ({ LifetimeTrackedImpl::use_(&({ instance_4() })) }); +} +pub fn b_address_8() -> Ptr { + return ({ instance_4() }); +} +pub trait AssertsDtorCountAtExitImpl { + fn destructor(&self); +} +impl AssertsDtorCountAtExitImpl for Ptr { + fn destructor(&self) { + assert!((dtorCount_2.with(|rc| *rc.borrow()) == (*self).with(|__s| __s.expected))); + } +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); + let _ = check_6.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/p15-inline-function/out/unsafe/p15-inline-function.rs b/tests/std/basic/basic.def.odr/p15-inline-function/out/unsafe/p15-inline-function.rs new file mode 100644 index 0000000000..66646518b4 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-inline-function/out/unsafe/p15-inline-function.rs @@ -0,0 +1,98 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +#[repr(C)] +#[derive(Clone, VaArg, FnPtrArg, DestructorUnsafe, Default)] +pub struct AssertsDtorCountAtExit { + pub expected: i32, +} +impl AssertsDtorCountAtExit { + pub unsafe fn destructor(&mut self) { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)) == (self.expected))); + } +} +pub unsafe fn instance_4() -> *mut LifetimeTracked { + return &mut (*std::cell::LazyCell::force_mut(&mut *&raw mut t_5)); +} +static mut t_5: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() }); +pub static mut check_6: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { AssertsDtorCountAtExit { expected: 1 } }); +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (0))); + (unsafe { LifetimeTracked::use_(&(*(unsafe { instance_4() }))) }); + (unsafe { b_use_7() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)) == (2))); + assert!((((*(unsafe { instance_4() })).uses) == (2))); + assert!(((unsafe { instance_4() }) == (unsafe { b_address_8() }))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (0))); + return 0; +} +pub unsafe fn b_use_7() { + (unsafe { LifetimeTracked::use_(&(*(unsafe { instance_4() }))) }); +} +pub unsafe fn b_address_8() -> *mut LifetimeTracked { + return (unsafe { instance_4() }); +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); + std::cell::LazyCell::force(&*&raw const check_6); +} diff --git a/tests/std/basic/basic.def.odr/p15-inline-variable/CMakeLists.txt b/tests/std/basic/basic.def.odr/p15-inline-variable/CMakeLists.txt new file mode 100644 index 0000000000..1c17a0355c --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-inline-variable/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.20) +project(p15_inline_variable LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +add_executable(app a.cpp b.cpp) diff --git a/tests/std/basic/basic.def.odr/p15-inline-variable/a.cpp b/tests/std/basic/basic.def.odr/p15-inline-variable/a.cpp new file mode 100644 index 0000000000..ffb84d86fc --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-inline-variable/a.cpp @@ -0,0 +1,14 @@ +#include + +#include "h.h" + +int main() { + shared.use(); + b_use(); + assert(LifetimeTracked::ctorCount == 1); + assert(LifetimeTracked::useCount == 2); + assert(shared.uses == 2); + assert(&shared == b_address()); + assert(LifetimeTracked::copyCount == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p15-inline-variable/b.cpp b/tests/std/basic/basic.def.odr/p15-inline-variable/b.cpp new file mode 100644 index 0000000000..4e1ec9898c --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-inline-variable/b.cpp @@ -0,0 +1,5 @@ +#include "h.h" + +void b_use() { shared.use(); } + +LifetimeTracked *b_address() { return &shared; } diff --git a/tests/std/basic/basic.def.odr/p15-inline-variable/h.h b/tests/std/basic/basic.def.odr/p15-inline-variable/h.h new file mode 100644 index 0000000000..5128aff268 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-inline-variable/h.h @@ -0,0 +1,13 @@ +// N4950 [basic.def.odr]/15: an inline variable defined in several translation +// units behaves as a single entity: one object with one address, constructed +// once and destroyed once. +#pragma once + +#include "../../../support/asserts-dtor-count-at-exit.h" +#include "../../../support/lifetime-tracked.h" + +inline AssertsDtorCountAtExit check{1}; +inline LifetimeTracked shared; + +void b_use(); +LifetimeTracked *b_address(); diff --git a/tests/std/basic/basic.def.odr/p15-inline-variable/out/refcount/p15-inline-variable.rs b/tests/std/basic/basic.def.odr/p15-inline-variable/out/refcount/p15-inline-variable.rs new file mode 100644 index 0000000000..672ea8714b --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-inline-variable/out/refcount/p15-inline-variable.rs @@ -0,0 +1,118 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructor, Default)] +#[byte_size(4)] +pub struct AssertsDtorCountAtExit { + #[offset(0)] + pub expected: i32, +} +thread_local!( + pub static check_4: Value = + Rc::new(RefCell::new(AssertsDtorCountAtExit { expected: 1 })); +); +thread_local!( + pub static shared_5: Value = Rc::new(RefCell::new(LifetimeTracked::new())); +); +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ LifetimeTrackedImpl::use_(&shared_5.with(|v| v.as_pointer())) }); + ({ b_use_6() }); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 1)); + assert!((useCount_3.with(|rc| *rc.borrow()) == 2)); + assert!(({ ({ (*shared_5.with(Value::clone).borrow()).uses } == 2) })); + assert!(((shared_5.with(|v| v.as_pointer())) == ({ b_address_7() }))); + assert!((copyCount_1.with(|rc| *rc.borrow()) == 0)); + return 0; +} +pub fn b_use_6() { + ({ LifetimeTrackedImpl::use_(&shared_5.with(|v| v.as_pointer())) }); +} +pub fn b_address_7() -> Ptr { + return (shared_5.with(|v| v.as_pointer())); +} +pub trait AssertsDtorCountAtExitImpl { + fn destructor(&self); +} +impl AssertsDtorCountAtExitImpl for Ptr { + fn destructor(&self) { + assert!((dtorCount_2.with(|rc| *rc.borrow()) == (*self).with(|__s| __s.expected))); + } +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); + let _ = check_4.with(|_| ()); + let _ = shared_5.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/p15-inline-variable/out/unsafe/p15-inline-variable.rs b/tests/std/basic/basic.def.odr/p15-inline-variable/out/unsafe/p15-inline-variable.rs new file mode 100644 index 0000000000..2512f15427 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p15-inline-variable/out/unsafe/p15-inline-variable.rs @@ -0,0 +1,99 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +#[repr(C)] +#[derive(Clone, VaArg, FnPtrArg, DestructorUnsafe, Default)] +pub struct AssertsDtorCountAtExit { + pub expected: i32, +} +impl AssertsDtorCountAtExit { + pub unsafe fn destructor(&mut self) { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)) == (self.expected))); + } +} +pub static mut check_4: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { AssertsDtorCountAtExit { expected: 1 } }); +pub static mut shared_5: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() }); +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { LifetimeTracked::use_(&(*std::cell::LazyCell::force_mut(&mut *&raw mut shared_5))) }); + (unsafe { b_use_6() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)) == (2))); + assert!((((*std::cell::LazyCell::force_mut(&mut *&raw mut shared_5)).uses) == (2))); + assert!( + ((&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut shared_5)) + as *mut LifetimeTracked) + == (unsafe { b_address_7() })) + ); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) == (0))); + return 0; +} +pub unsafe fn b_use_6() { + (unsafe { LifetimeTracked::use_(&(*std::cell::LazyCell::force_mut(&mut *&raw mut shared_5))) }); +} +pub unsafe fn b_address_7() -> *mut LifetimeTracked { + return (&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut shared_5)) + as *mut LifetimeTracked); +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); + std::cell::LazyCell::force(&*&raw const check_4); + std::cell::LazyCell::force(&*&raw const shared_5); +} diff --git a/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/CMakeLists.txt b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/CMakeLists.txt new file mode 100644 index 0000000000..474c5df409 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.20) +project(p16_default_argument_lambda_in_class LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +add_executable(app a.cpp b.cpp) diff --git a/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/a.cpp b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/a.cpp new file mode 100644 index 0000000000..9190af58d3 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/a.cpp @@ -0,0 +1,18 @@ +#include + +#include "../../../support/asserts-dtor-count-at-exit.h" +#include "h.h" + +AssertsDtorCountAtExit check{1}; + +int main() { + assert(LifetimeTracked::ctorCount == 0); + X x; + x.h(true); + assert(LifetimeTracked::ctorCount == 1); + assert(LifetimeTracked::useCount == 1); + b_call(); + assert(LifetimeTracked::ctorCount == 1); + assert(LifetimeTracked::useCount == 2); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/b.cpp b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/b.cpp new file mode 100644 index 0000000000..5adfe787e8 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/b.cpp @@ -0,0 +1,6 @@ +#include "h.h" + +void b_call() { + X x; + x.h(true); +} diff --git a/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/h.h b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/h.h new file mode 100644 index 0000000000..85fdf40fd5 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/h.h @@ -0,0 +1,21 @@ +// N4950 [basic.def.odr]/16, Example 6: X is a class whose member function h +// has a lambda-expression in a default argument. Defined in several +// translation units, the lambda denotes the same closure type in each, so it +// has one static local variable. +#pragma once + +#include "../../../support/lifetime-tracked.h" + +struct X { + void h(bool cond, void (*p)() = [] { + static LifetimeTracked local; + local.use(); + }) { + if (cond) + h(false); + else + p(); + } +}; + +void b_call(); diff --git a/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/refcount/p16-default-argument-lambda-in-class.rs b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/refcount/p16-default-argument-lambda-in-class.rs new file mode 100644 index 0000000000..302c9a8750 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/refcount/p16-default-argument-lambda-in-class.rs @@ -0,0 +1,136 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructor, Default)] +#[byte_size(4)] +pub struct AssertsDtorCountAtExit { + #[offset(0)] + pub expected: i32, +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(1)] +pub struct X {} +thread_local!( + static local_4: Value = Rc::new(RefCell::new(LifetimeTracked::new())); +); +thread_local!( + pub static check_5: Value = + Rc::new(RefCell::new(AssertsDtorCountAtExit { expected: 1 })); +); +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0)); + let x: Value = Rc::new(RefCell::new(::default())); + ({ XImpl::h(&x.as_pointer(), true, None) }); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 1)); + assert!((useCount_3.with(|rc| *rc.borrow()) == 1)); + ({ b_call_6() }); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 1)); + assert!((useCount_3.with(|rc| *rc.borrow()) == 2)); + return 0; +} +pub fn b_call_6() { + let x: Value = Rc::new(RefCell::new(::default())); + ({ XImpl::h(&x.as_pointer(), true, None) }); +} +pub trait AssertsDtorCountAtExitImpl { + fn destructor(&self); +} +impl AssertsDtorCountAtExitImpl for Ptr { + fn destructor(&self) { + assert!((dtorCount_2.with(|rc| *rc.borrow()) == (*self).with(|__s| __s.expected))); + } +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub trait XImpl { + fn h(&self, cond: bool, p: Option>); +} +impl XImpl for Ptr { + fn h(&self, mut cond: bool, p: Option>) { + let mut p: FnPtr = p.unwrap_or_else(|| { + FnPtr::::new(|| { + ({ LifetimeTrackedImpl::use_(&local_4.with(|v| v.as_pointer())) }); + }) + }); + if cond { + ({ XImpl::h(self, false, None) }); + } else { + ({ p.call() }); + } + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); + let _ = check_5.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/unsafe/p16-default-argument-lambda-in-class.rs b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/unsafe/p16-default-argument-lambda-in-class.rs new file mode 100644 index 0000000000..e4dbd1b47e --- /dev/null +++ b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/unsafe/p16-default-argument-lambda-in-class.rs @@ -0,0 +1,113 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +#[repr(C)] +#[derive(Clone, VaArg, FnPtrArg, DestructorUnsafe, Default)] +pub struct AssertsDtorCountAtExit { + pub expected: i32, +} +impl AssertsDtorCountAtExit { + pub unsafe fn destructor(&mut self) { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)) == (self.expected))); + } +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct X {} +impl X { + pub unsafe fn h(&mut self, mut cond: bool, mut p: Option>) { + let mut p: Option = p.unwrap_or_else(|| unsafe { + Some(|| unsafe { + (unsafe { + LifetimeTracked::use_( + &(*std::cell::LazyCell::force_mut(&mut *&raw mut local_4)), + ) + }); + }) + }); + if cond { + (unsafe { X::h(self, false, None) }); + } else { + (unsafe { (p).unwrap()() }); + } + } +} +static mut local_4: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() }); +pub static mut check_5: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { AssertsDtorCountAtExit { expected: 1 } }); +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (0))); + let mut x: X = ::default(); + (unsafe { X::h(&mut x, true, None) }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)) == (1))); + (unsafe { b_call_6() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)) == (2))); + return 0; +} +pub unsafe fn b_call_6() { + let mut x: X = ::default(); + (unsafe { X::h(&mut x, true, None) }); +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); + std::cell::LazyCell::force(&*&raw const check_5); +} diff --git a/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/CMakeLists.txt b/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/CMakeLists.txt new file mode 100644 index 0000000000..bfe635d203 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.20) +project(p16_lambda_in_inline_function LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +add_executable(app a.cpp b.cpp) diff --git a/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/a.cpp b/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/a.cpp new file mode 100644 index 0000000000..0b5e83750c --- /dev/null +++ b/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/a.cpp @@ -0,0 +1,17 @@ +#include + +#include "../../../support/asserts-dtor-count-at-exit.h" +#include "h.h" + +AssertsDtorCountAtExit check{1}; + +int main() { + assert(LifetimeTracked::ctorCount == 0); + f(true, nullptr); + assert(LifetimeTracked::ctorCount == 1); + assert(LifetimeTracked::useCount == 1); + b_call(); + assert(LifetimeTracked::ctorCount == 1); + assert(LifetimeTracked::useCount == 2); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/b.cpp b/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/b.cpp new file mode 100644 index 0000000000..b79c8df557 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/b.cpp @@ -0,0 +1,3 @@ +#include "h.h" + +void b_call() { f(true, nullptr); } diff --git a/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/h.h b/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/h.h new file mode 100644 index 0000000000..666093e554 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/h.h @@ -0,0 +1,19 @@ +// N4950 [basic.def.odr]/16, Example 6: f is an inline function containing a +// lambda-expression. Defined in several translation units, it behaves as one +// definition, so the lambda inside it is one closure type with one static +// local variable. +#pragma once + +#include "../../../support/lifetime-tracked.h" + +inline void f(bool cond, void (*p)()) { + if (cond) + f(false, [] { + static LifetimeTracked local; + local.use(); + }); + else + p(); +} + +void b_call(); diff --git a/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/out/refcount/p16-lambda-in-inline-function.rs b/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/out/refcount/p16-lambda-in-inline-function.rs new file mode 100644 index 0000000000..58a226d25a --- /dev/null +++ b/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/out/refcount/p16-lambda-in-inline-function.rs @@ -0,0 +1,128 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static ctorCount_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static copyCount_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static dtorCount_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static useCount_3: Value = Rc::new(RefCell::new(0)); +); +#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)] +#[byte_size(4)] +pub struct LifetimeTracked { + #[offset(0)] + pub uses: i32, +} +impl LifetimeTracked { + pub fn new() -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } + pub fn copy_from(_a0: Ptr) -> Self { + let __this: LifetimeTracked = Self { uses: 0 }; + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(LifetimeTracked { + uses: self.uses.clone(), + })); + LifetimeTracked::copy_from(__src.as_pointer()) + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + { LifetimeTracked::new() } + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructor, Default)] +#[byte_size(4)] +pub struct AssertsDtorCountAtExit { + #[offset(0)] + pub expected: i32, +} +pub fn f_4(mut cond: bool, mut p: FnPtr) { + if cond { + ({ + f_4( + false, + FnPtr::::new(|| { + ({ LifetimeTrackedImpl::use_(&local_5.with(|v| v.as_pointer())) }); + }), + ) + }); + } else { + ({ p.call() }); + } +} +thread_local!( + static local_5: Value = Rc::new(RefCell::new(LifetimeTracked::new())); +); +thread_local!( + pub static check_6: Value = + Rc::new(RefCell::new(AssertsDtorCountAtExit { expected: 1 })); +); +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0)); + ({ f_4(true, FnPtr::::null()) }); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 1)); + assert!((useCount_3.with(|rc| *rc.borrow()) == 1)); + ({ b_call_7() }); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 1)); + assert!((useCount_3.with(|rc| *rc.borrow()) == 2)); + return 0; +} +pub fn b_call_7() { + ({ f_4(true, FnPtr::::null()) }); +} +pub trait AssertsDtorCountAtExitImpl { + fn destructor(&self); +} +impl AssertsDtorCountAtExitImpl for Ptr { + fn destructor(&self) { + assert!((dtorCount_2.with(|rc| *rc.borrow()) == (*self).with(|__s| __s.expected))); + } +} +pub trait LifetimeTrackedImpl { + fn destructor(&self); + fn use_(&self); + fn copy_assign(&self, _a0: Ptr) -> Ptr; +} +impl LifetimeTrackedImpl for Ptr { + fn destructor(&self) { + (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn use_(&self) { + field!((*self), uses).with_mut(|__v| __v.prefix_inc()); + (*useCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), uses).write({ _a0.with(|__s| __s.uses) }); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() { + let _ = ctorCount_0.with(|_| ()); + let _ = copyCount_1.with(|_| ()); + let _ = dtorCount_2.with(|_| ()); + let _ = useCount_3.with(|_| ()); + let _ = check_6.with(|_| ()); +} diff --git a/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/out/unsafe/p16-lambda-in-inline-function.rs b/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/out/unsafe/p16-lambda-in-inline-function.rs new file mode 100644 index 0000000000..997339451a --- /dev/null +++ b/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/out/unsafe/p16-lambda-in-inline-function.rs @@ -0,0 +1,108 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructorUnsafe)] +pub struct LifetimeTracked { + pub uses: i32, +} +impl LifetimeTracked { + pub unsafe fn new() -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); + this + } + pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { + let mut this = Self { uses: 0 }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); + this + } + pub unsafe fn destructor(&mut self) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); + } + pub unsafe fn use_(&self) { + (*(&raw const self.uses).cast_mut()).prefix_inc(); + (*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)).prefix_inc(); + } + pub unsafe fn copy_assign(&mut self, _a0: *const LifetimeTracked) -> *mut LifetimeTracked { + self.uses = (*_a0).uses; + return &mut (*(self as *mut LifetimeTracked)); + } +} +impl Clone for LifetimeTracked { + fn clone(&self) -> Self { + unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + } +} +impl Default for LifetimeTracked { + fn default() -> Self { + unsafe { LifetimeTracked::new() } + } +} +#[repr(C)] +#[derive(Clone, VaArg, FnPtrArg, DestructorUnsafe, Default)] +pub struct AssertsDtorCountAtExit { + pub expected: i32, +} +impl AssertsDtorCountAtExit { + pub unsafe fn destructor(&mut self) { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)) == (self.expected))); + } +} +pub unsafe fn f_4(mut cond: bool, mut p: Option) { + if cond { + (unsafe { + f_4( + false, + Some(|| unsafe { + (unsafe { + LifetimeTracked::use_( + &(*std::cell::LazyCell::force_mut(&mut *&raw mut local_5)), + ) + }); + }), + ) + }); + } else { + (unsafe { (p).unwrap()() }); + } +} +static mut local_5: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() }); +pub static mut check_6: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { AssertsDtorCountAtExit { expected: 1 } }); +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (0))); + (unsafe { f_4(true, None) }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)) == (1))); + (unsafe { b_call_7() }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)) == (2))); + return 0; +} +pub unsafe fn b_call_7() { + (unsafe { f_4(true, None) }); +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const ctorCount_0); + std::cell::LazyCell::force(&*&raw const copyCount_1); + std::cell::LazyCell::force(&*&raw const dtorCount_2); + std::cell::LazyCell::force(&*&raw const useCount_3); + std::cell::LazyCell::force(&*&raw const check_6); +} diff --git a/tests/std/basic/basic.def.odr/p3.2-subscript.cpp b/tests/std/basic/basic.def.odr/p3.2-subscript.cpp new file mode 100644 index 0000000000..e9f1ad02fe --- /dev/null +++ b/tests/std/basic/basic.def.odr/p3.2-subscript.cpp @@ -0,0 +1,16 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/3.2: the potential results of a subscripting operation +// with an array operand are those of the operand. Reading arr[1].m does not +// odr-use arr, so [=] does not capture it. +#include + +#include "../../support/has-constexpr-ctor.h" + +int main() { + constexpr HasConstexprCtor arr[2] = {3, 4}; + auto f = [=] { return arr[1].m; }; + assert(HasConstexprCtor::copyCount == 0); + assert(f() == 4); + assert(HasConstexprCtor::copyCount == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p3.3-member-access.cpp b/tests/std/basic/basic.def.odr/p3.3-member-access.cpp new file mode 100644 index 0000000000..879c2ce17b --- /dev/null +++ b/tests/std/basic/basic.def.odr/p3.3-member-access.cpp @@ -0,0 +1,16 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/3.3: the potential results of E1.E2 naming a non-static +// data member are those of E1. Reading k.m does not odr-use k, so [=] does not +// capture it. +#include + +#include "../../support/has-constexpr-ctor.h" + +int main() { + constexpr HasConstexprCtor k{5}; + auto f = [=] { return k.m; }; + assert(HasConstexprCtor::copyCount == 0); + assert(f() == 5); + assert(HasConstexprCtor::copyCount == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p3.5-pointer-to-member.cpp b/tests/std/basic/basic.def.odr/p3.5-pointer-to-member.cpp new file mode 100644 index 0000000000..35731e76c7 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p3.5-pointer-to-member.cpp @@ -0,0 +1,17 @@ +// XFAIL: refcount, unsafe +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/3.5: the potential results of E1.*E2 are those of E1. +// Reading k.*pm does not odr-use k, so [=] does not capture it. +#include + +#include "../../support/has-constexpr-ctor.h" + +int main() { + constexpr HasConstexprCtor k{5}; + constexpr int HasConstexprCtor::*pm = &HasConstexprCtor::m; + auto f = [=] { return k.*pm; }; + assert(HasConstexprCtor::copyCount == 0); + assert(f() == 5); + assert(HasConstexprCtor::copyCount == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p3.7-conditional.cpp b/tests/std/basic/basic.def.odr/p3.7-conditional.cpp new file mode 100644 index 0000000000..8efaea7d9b --- /dev/null +++ b/tests/std/basic/basic.def.odr/p3.7-conditional.cpp @@ -0,0 +1,18 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/3.7: the potential results of a glvalue conditional +// expression are those of its second and third operands. Neither k1 nor k2 is +// odr-used, so [=] captures neither. +#include + +#include "../../support/has-constexpr-ctor.h" + +int main() { + constexpr HasConstexprCtor k1{7}; + constexpr HasConstexprCtor k2{9}; + auto f = [=](bool b) { return (b ? k1 : k2).m; }; + assert(HasConstexprCtor::copyCount == 0); + assert(f(true) == 7); + assert(f(false) == 9); + assert(HasConstexprCtor::copyCount == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p3.8-comma.cpp b/tests/std/basic/basic.def.odr/p3.8-comma.cpp new file mode 100644 index 0000000000..db017bded3 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p3.8-comma.cpp @@ -0,0 +1,20 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/3.8: the potential results of a comma expression are +// those of its right operand. k is not odr-used, so [=] does not capture it; +// the left operand is still evaluated. +#include + +#include "../../support/has-constexpr-ctor.h" + +static int left; + +int main() { + constexpr HasConstexprCtor k{5}; + auto f = [=] { return (++left, k).m; }; + assert(HasConstexprCtor::copyCount == 0); + assert(left == 0); + assert(f() == 5); + assert(left == 1); + assert(HasConstexprCtor::copyCount == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p4.1-pure-virtual-member-pointer.cpp b/tests/std/basic/basic.def.odr/p4.1-pure-virtual-member-pointer.cpp new file mode 100644 index 0000000000..6f468ecdfa --- /dev/null +++ b/tests/std/basic/basic.def.odr/p4.1-pure-virtual-member-pointer.cpp @@ -0,0 +1,25 @@ +// XFAIL: refcount, unsafe +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/4.1: forming a pointer to member does not name a pure +// virtual function. HasPureVirtual::f is never defined; the call through the +// pointer to member runs the override. +#include + +static int derived_calls; + +struct HasPureVirtual { + virtual void f() = 0; +}; + +struct OverridesPureVirtual : HasPureVirtual { + void f() override { ++derived_calls; } +}; + +int main() { + void (HasPureVirtual::*pm)() = &HasPureVirtual::f; + OverridesPureVirtual d; + HasPureVirtual *p = &d; + (p->*pm)(); + assert(derived_calls == 1); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p4.1-pure-virtual-qualified-call.cpp b/tests/std/basic/basic.def.odr/p4.1-pure-virtual-qualified-call.cpp new file mode 100644 index 0000000000..fd9c9b823c --- /dev/null +++ b/tests/std/basic/basic.def.odr/p4.1-pure-virtual-qualified-call.cpp @@ -0,0 +1,35 @@ +// no-compile: refcount, unsafe +// The converter drops the body of a pure virtual function and the +// qualification of calls to it. +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/4.1: a pure virtual function is named by an +// id-expression with an explicitly qualified name. HasPureVirtual::f is pure +// but defined; the qualified call runs the base body, not the override. +#include + +static int base_calls, derived_calls; + +struct HasPureVirtual { + virtual void f() = 0; +}; + +void HasPureVirtual::f() { ++base_calls; } + +struct OverridesPureVirtual : HasPureVirtual { + void f() override { + ++derived_calls; + HasPureVirtual::f(); + } +}; + +int main() { + OverridesPureVirtual d; + HasPureVirtual *p = &d; + p->f(); + assert(derived_calls == 1); + assert(base_calls == 1); + d.HasPureVirtual::f(); + assert(derived_calls == 1); + assert(base_calls == 2); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p4.1-pure-virtual-without-definition.cpp b/tests/std/basic/basic.def.odr/p4.1-pure-virtual-without-definition.cpp new file mode 100644 index 0000000000..4a84943c6b --- /dev/null +++ b/tests/std/basic/basic.def.odr/p4.1-pure-virtual-without-definition.cpp @@ -0,0 +1,23 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/4.1: a pure virtual function is not named by a call +// that is not explicitly qualified. HasPureVirtual::f is never defined; the +// virtual call runs the override. +#include + +static int derived_calls; + +struct HasPureVirtual { + virtual void f() = 0; +}; + +struct OverridesPureVirtual : HasPureVirtual { + void f() override { ++derived_calls; } +}; + +int main() { + OverridesPureVirtual d; + HasPureVirtual *p = &d; + p->f(); + assert(derived_calls == 1); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p5.1-constant-reference-in-lambda.cpp b/tests/std/basic/basic.def.odr/p5.1-constant-reference-in-lambda.cpp new file mode 100644 index 0000000000..20f84999bd --- /dev/null +++ b/tests/std/basic/basic.def.odr/p5.1-constant-reference-in-lambda.cpp @@ -0,0 +1,20 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/5.1: a reference that is usable in constant expressions +// is not odr-used. [=] therefore does not capture r, and the lambda uses the +// object r refers to. +#include + +#include "../../support/lifetime-tracked.h" + +LifetimeTracked global; + +int main() { + constexpr LifetimeTracked &r = global; + auto f = [=] { r.use(); }; + assert(LifetimeTracked::copyCount == 0); + f(); + assert(LifetimeTracked::copyCount == 0); + assert(global.uses == 1); + assert(LifetimeTracked::ctorCount == 1); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p5.1-local-reference-is-odr-used.cpp b/tests/std/basic/basic.def.odr/p5.1-local-reference-is-odr-used.cpp new file mode 100644 index 0000000000..59ecfc0d83 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p5.1-local-reference-is-odr-used.cpp @@ -0,0 +1,19 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/5.1: only a reference that is usable in constant +// expressions is exempt. r refers to a local object, so it is odr-used and [=] +// captures it, which copies the object it refers to. +#include + +#include "../../support/lifetime-tracked.h" + +int main() { + LifetimeTracked t; + LifetimeTracked &r = t; + auto f = [=] { r.use(); }; + assert(LifetimeTracked::copyCount == 1); + f(); + assert(LifetimeTracked::copyCount == 1); + assert(LifetimeTracked::useCount == 1); + assert(t.uses == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p5.2-address-of-constant-is-odr-use.cpp b/tests/std/basic/basic.def.odr/p5.2-address-of-constant-is-odr-use.cpp new file mode 100644 index 0000000000..83d1d2871e --- /dev/null +++ b/tests/std/basic/basic.def.odr/p5.2-address-of-constant-is-odr-use.cpp @@ -0,0 +1,13 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/5.2: the exemption requires the lvalue-to-rvalue +// conversion. Taking the address of the constant n odr-uses it, so [=] captures +// a copy, and the address seen inside the lambda is not that of n. +#include + +int main() { + const int n = 5; + auto f = [=] { return &n; }; + assert(f() != &n); + assert(*f() == 5); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p5.2-class-object-is-odr-used.cpp b/tests/std/basic/basic.def.odr/p5.2-class-object-is-odr-used.cpp new file mode 100644 index 0000000000..591781df98 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p5.2-class-object-is-odr-used.cpp @@ -0,0 +1,18 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/5.2: the exemption covers only expressions of non-class +// type. Copying the constant k as a whole odr-uses it, so [=] captures it. +#include + +#include "../../support/has-constexpr-ctor.h" + +int main() { + constexpr HasConstexprCtor k{5}; + auto f = [=] { + HasConstexprCtor c = k; + return c.m; + }; + assert(HasConstexprCtor::copyCount == 1); + assert(f() == 5); + assert(HasConstexprCtor::copyCount == 2); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p5.2-constant-in-lambda-without-capture.cpp b/tests/std/basic/basic.def.odr/p5.2-constant-in-lambda-without-capture.cpp new file mode 100644 index 0000000000..3744c35dd6 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p5.2-constant-in-lambda-without-capture.cpp @@ -0,0 +1,11 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/5.2: a constant variable of non-class type read by +// value is not odr-used, so the lambda uses n with an empty capture list. +#include + +int main() { + const int n = 5; + auto f = [] { return n; }; + assert(f() == 5); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p5.2-mutable-subobject-is-odr-used.cpp b/tests/std/basic/basic.def.odr/p5.2-mutable-subobject-is-odr-used.cpp new file mode 100644 index 0000000000..83be4b8102 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p5.2-mutable-subobject-is-odr-used.cpp @@ -0,0 +1,26 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/5.2: the exemption requires a variable with no mutable +// subobjects. k has one, so reading k.value odr-uses k and [=] captures it. +#include + +static int copies; + +struct HasMutableMember { + int value; + mutable int scratch; + + constexpr HasMutableMember(int v) : value(v), scratch(0) {} + HasMutableMember(const HasMutableMember &o) + : value(o.value), scratch(o.scratch) { + ++copies; + } +}; + +int main() { + constexpr HasMutableMember k{5}; + auto f = [=] { return k.value; }; + assert(copies == 1); + assert(f() == 5); + assert(copies == 1); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p5.3-discarded-value-in-lambda.cpp b/tests/std/basic/basic.def.odr/p5.3-discarded-value-in-lambda.cpp new file mode 100644 index 0000000000..d0755e7112 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p5.3-discarded-value-in-lambda.cpp @@ -0,0 +1,17 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/5.3: a variable named by a discarded-value expression +// to which the lvalue-to-rvalue conversion is not applied is not odr-used, so +// [=] does not capture t. +#include + +#include "../../support/lifetime-tracked.h" + +int main() { + LifetimeTracked t; + auto f = [=] { (void)t; }; + assert(LifetimeTracked::copyCount == 0); + f(); + assert(LifetimeTracked::copyCount == 0); + assert(LifetimeTracked::ctorCount == 1); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p6-evaluated-binding-is-captured.cpp b/tests/std/basic/basic.def.odr/p6-evaluated-binding-is-captured.cpp new file mode 100644 index 0000000000..a1e0db0181 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p6-evaluated-binding-is-captured.cpp @@ -0,0 +1,28 @@ +// no-compile: refcount, unsafe +// Inside the lambda the converter refers to the original structured binding, +// not to the captured copy. +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/6: a structured binding is odr-used if it appears as a +// potentially-evaluated expression, so [=] captures a by copy. +#include + +#include "../../support/lifetime-tracked.h" + +struct HasTwoMembers { + LifetimeTracked a; + LifetimeTracked b; +}; + +int main() { + HasTwoMembers p{LifetimeTracked(), LifetimeTracked()}; + assert(LifetimeTracked::ctorCount == 2); + auto &[a, b] = p; + assert(LifetimeTracked::copyCount == 0); + auto f = [=] { a.use(); }; + assert(LifetimeTracked::copyCount == 1); + f(); + assert(LifetimeTracked::copyCount == 1); + assert(LifetimeTracked::useCount == 1); + assert(p.a.uses == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p6-unevaluated-binding-needs-no-capture.cpp b/tests/std/basic/basic.def.odr/p6-unevaluated-binding-needs-no-capture.cpp new file mode 100644 index 0000000000..b8598ea901 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p6-unevaluated-binding-needs-no-capture.cpp @@ -0,0 +1,23 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/6: a structured binding is odr-used only if it appears +// as a potentially-evaluated expression. In sizeof it is unevaluated, so [=] +// does not capture a. +#include + +#include "../../support/lifetime-tracked.h" + +struct HasTwoMembers { + LifetimeTracked a; + LifetimeTracked b; +}; + +int main() { + HasTwoMembers p{LifetimeTracked(), LifetimeTracked()}; + auto &[a, b] = p; + auto f = [=] { return sizeof(a); }; + assert(LifetimeTracked::copyCount == 0); + assert(f() == sizeof(LifetimeTracked)); + assert(LifetimeTracked::copyCount == 0); + assert(LifetimeTracked::useCount == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p7-member-access-captures-this.cpp b/tests/std/basic/basic.def.odr/p7-member-access-captures-this.cpp new file mode 100644 index 0000000000..a3482fe9d0 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p7-member-access-captures-this.cpp @@ -0,0 +1,32 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/7: *this is odr-used if this appears as a +// potentially-evaluated expression, including through the implicit +// transformation of a member name. [=] captures this, not the member x, and the +// lambda sees later changes to the object. +#include + +#include "../../support/lifetime-tracked.h" + +struct HasMemberUsedInLambda { + LifetimeTracked x; + int n = 1; + auto makeLambda() { + return [=] { + x.use(); + return n; + }; + } +}; + +int main() { + HasMemberUsedInLambda c; + auto f = c.makeLambda(); + assert(LifetimeTracked::copyCount == 0); + assert(f() == 1); + assert(c.x.uses == 1); + c.n = 10; + assert(f() == 10); + assert(c.x.uses == 2); + assert(LifetimeTracked::copyCount == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p7-unevaluated-this-needs-no-capture.cpp b/tests/std/basic/basic.def.odr/p7-unevaluated-this-needs-no-capture.cpp new file mode 100644 index 0000000000..74a80bc9a7 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p7-unevaluated-this-needs-no-capture.cpp @@ -0,0 +1,23 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/7: *this is odr-used only if this appears as a +// potentially-evaluated expression. In sizeof the member name is unevaluated, +// so the lambda needs no capture. +#include + +#include "../../support/lifetime-tracked.h" + +struct HasMemberUsedInLambda { + LifetimeTracked x; + auto sizer() { + return [] { return sizeof(x); }; + } +}; + +int main() { + HasMemberUsedInLambda c; + auto f = c.sizer(); + assert(f() == sizeof(LifetimeTracked)); + assert(LifetimeTracked::copyCount == 0); + assert(LifetimeTracked::useCount == 0); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p8-unevaluated-call-needs-no-definition.cpp b/tests/std/basic/basic.def.odr/p8-unevaluated-call-needs-no-definition.cpp new file mode 100644 index 0000000000..fb7fb7c2f5 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p8-unevaluated-call-needs-no-definition.cpp @@ -0,0 +1,31 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/8: a function is odr-used if it is named by a +// potentially-evaluated expression. make() is named only in unevaluated +// operands, so it is never defined; noisy() is defined, but an unevaluated call +// does not run it. +#include + +#include "../../support/lifetime-tracked.h" + +static int noisy_calls; + +LifetimeTracked make(); + +int noisy() { + ++noisy_calls; + return 1; +} + +int main() { + assert(sizeof(make()) == sizeof(LifetimeTracked)); + assert(sizeof(noisy()) == sizeof(int)); + assert(!noexcept(make())); + assert(!noexcept(noisy())); + decltype(noisy()) n = 3; + assert(n == 3); + assert(noisy_calls == 0); + assert(LifetimeTracked::ctorCount == 0); + assert(noisy() == 1); + assert(noisy_calls == 1); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p9-destructor-not-potentially-invoked.cpp b/tests/std/basic/basic.def.odr/p9-destructor-not-potentially-invoked.cpp new file mode 100644 index 0000000000..18412bf900 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p9-destructor-not-potentially-invoked.cpp @@ -0,0 +1,19 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/9: a destructor is odr-used if it is potentially +// invoked. A new-expression for a single object does not potentially invoke it, +// and the object is never deleted, so ~HasUndefinedDestructor is never defined. +#include + +static int t_ctors; + +struct HasUndefinedDestructor { + HasUndefinedDestructor() { ++t_ctors; } + ~HasUndefinedDestructor(); +}; + +int main() { + HasUndefinedDestructor *p = new HasUndefinedDestructor; + assert(p != nullptr); + assert(t_ctors == 1); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p9-unused-copy-constructor.cpp b/tests/std/basic/basic.def.odr/p9-unused-copy-constructor.cpp new file mode 100644 index 0000000000..28b95ee6e2 --- /dev/null +++ b/tests/std/basic/basic.def.odr/p9-unused-copy-constructor.cpp @@ -0,0 +1,23 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/9: a constructor is odr-used only where an +// initialization selects it. No HasUndefinedCopyCtor is ever copied, so its +// copy constructor is never defined. +#include + +static int ctor_calls; + +struct HasUndefinedCopyCtor { + int value = 3; + + HasUndefinedCopyCtor() { ++ctor_calls; } + HasUndefinedCopyCtor(const HasUndefinedCopyCtor &); +}; + +int read(const HasUndefinedCopyCtor &object) { return object.value; } + +int main() { + HasUndefinedCopyCtor a; + assert(read(a) == 3); + assert(ctor_calls == 1); + return 0; +} diff --git a/tests/std/basic/basic.def.odr/p9-unused-implicit-assignment.cpp b/tests/std/basic/basic.def.odr/p9-unused-implicit-assignment.cpp new file mode 100644 index 0000000000..b3e20b21bd --- /dev/null +++ b/tests/std/basic/basic.def.odr/p9-unused-implicit-assignment.cpp @@ -0,0 +1,25 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +// N4950 [basic.def.odr]/9: an assignment operator is odr-used by an +// implicitly-defined assignment operator of another class. +// HasMemberWithUndefinedCopyAssignment is never assigned, so its assignment +// operator is never implicitly defined and +// HasUndefinedCopyAssignment::operator= is never defined. +#include + +static int m_ctors; + +struct HasUndefinedCopyAssignment { + HasUndefinedCopyAssignment() { ++m_ctors; } + HasUndefinedCopyAssignment &operator=(const HasUndefinedCopyAssignment &); +}; + +struct HasMemberWithUndefinedCopyAssignment { + HasUndefinedCopyAssignment m; +}; + +int main() { + HasMemberWithUndefinedCopyAssignment a; + HasMemberWithUndefinedCopyAssignment b; + assert(m_ctors == 2); + return 0; +} diff --git a/tests/std/support/asserts-dtor-count-at-exit.h b/tests/std/support/asserts-dtor-count-at-exit.h new file mode 100644 index 0000000000..5e691219d2 --- /dev/null +++ b/tests/std/support/asserts-dtor-count-at-exit.h @@ -0,0 +1,9 @@ +#pragma once + +#include "lifetime-tracked.h" +#include + +struct AssertsDtorCountAtExit { + int expected; + ~AssertsDtorCountAtExit() { assert(LifetimeTracked::dtorCount == expected); } +}; diff --git a/tests/std/support/has-constexpr-ctor.h b/tests/std/support/has-constexpr-ctor.h new file mode 100644 index 0000000000..0b0f8a12d6 --- /dev/null +++ b/tests/std/support/has-constexpr-ctor.h @@ -0,0 +1,10 @@ +#pragma once + +struct HasConstexprCtor { + static inline int copyCount = 0; + + int m; + + constexpr HasConstexprCtor(int v) : m(v) {} + HasConstexprCtor(const HasConstexprCtor &o) : m(o.m) { ++copyCount; } +}; diff --git a/tests/std/support/lifetime-tracked.h b/tests/std/support/lifetime-tracked.h new file mode 100644 index 0000000000..7e80c55a8c --- /dev/null +++ b/tests/std/support/lifetime-tracked.h @@ -0,0 +1,18 @@ +#pragma once + +struct LifetimeTracked { + static inline int ctorCount = 0; + static inline int copyCount = 0; + static inline int dtorCount = 0; + static inline int useCount = 0; + + mutable int uses = 0; + + LifetimeTracked() { ++ctorCount; } + LifetimeTracked(const LifetimeTracked &) { ++copyCount; } + ~LifetimeTracked() { ++dtorCount; } + void use() const { + ++uses; + ++useCount; + } +};