diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index d1a32a9e08..32b385f073 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -526,7 +526,8 @@ bool Converter::VisitFunctionTemplateDecl(clang::FunctionTemplateDecl *decl) { } for (auto *function_decl : decl->specializations()) { if (function_decl->getTemplateSpecializationKind() == - clang::TSK_ExplicitSpecialization) { + clang::TSK_ExplicitSpecialization || + IsLibraryOnlySpecialization(function_decl)) { continue; } VisitFunctionDecl(function_decl); @@ -904,6 +905,9 @@ void Converter::EmitRustStructOrUnion(clang::RecordDecl *decl) { } if (auto *nested_tmpl = clang::dyn_cast(d)) { for (auto *spec : nested_tmpl->specializations()) { + if (IsLibraryOnlySpecialization(spec)) { + continue; + } inner_structs_[GetID(spec)] = GetRecordName(spec); VisitCXXRecordDecl(spec); } @@ -4377,6 +4381,9 @@ bool Converter::VisitPredefinedExpr(clang::PredefinedExpr *expr) { bool Converter::VisitClassTemplateDecl(clang::ClassTemplateDecl *decl) { for (auto decl : decl->specializations()) { + if (IsLibraryOnlySpecialization(decl)) { + continue; + } VisitCXXRecordDecl(decl); } return false; diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 8499531211..ac40ed70d0 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -303,6 +303,51 @@ bool IsUserDefinedDecl(const clang::Decl *decl) { !src_mgr.isInSystemMacro(src_loc); } +static clang::QualType StripPointersAndArrays(clang::QualType type) { + while (type->isPointerType() || type->isReferenceType() || + type->isArrayType()) { + type = type->isArrayType() + ? clang::QualType(type->getArrayElementTypeNoTypeQual(), 0) + : type->getPointeeType(); + } + return type; +} + +static bool +HasLibraryInternalArgument(clang::ASTContext &ctx, + llvm::ArrayRef args) { + return llvm::any_of(args, [&](const clang::TemplateArgument &arg) { + if (arg.getKind() != clang::TemplateArgument::Type) { + return false; + } + auto tag = StripPointersAndArrays(arg.getAsType().getCanonicalType()) + ->getAsTagDecl(); + return tag && !IsUserDefinedDecl(tag) && + clang::isReservedInAllContexts(tag->isReserved(ctx.getLangOpts())); + }); +} + +static bool IsInstantiatedInSystemHeader(clang::ASTContext &ctx, + clang::SourceLocation loc) { + return loc.isValid() && ctx.getSourceManager().isInSystemHeader( + ctx.getSourceManager().getExpansionLoc(loc)); +} + +bool IsLibraryOnlySpecialization( + const clang::ClassTemplateSpecializationDecl *decl) { + auto &ctx = decl->getASTContext(); + return IsInstantiatedInSystemHeader(ctx, decl->getPointOfInstantiation()) && + HasLibraryInternalArgument(ctx, decl->getTemplateArgs().asArray()); +} + +bool IsLibraryOnlySpecialization(const clang::FunctionDecl *decl) { + auto &ctx = decl->getASTContext(); + auto args = decl->getTemplateSpecializationArgs(); + return args && + IsInstantiatedInSystemHeader(ctx, decl->getPointOfInstantiation()) && + HasLibraryInternalArgument(ctx, args->asArray()); +} + bool IsAbstractUserClass(const clang::RecordDecl *decl) { auto cxx = llvm::dyn_cast(decl); auto definition = cxx ? cxx->getDefinition() : nullptr; @@ -416,7 +461,7 @@ static void ForEachTemplateInstantiation( llvm::dyn_cast(d)) { for (auto s : function_template_decl->specializations()) { if (auto m = clang::dyn_cast(s); - m && m->getDefinition()) { + m && m->getDefinition() && !IsLibraryOnlySpecialization(m)) { fn(m); } } @@ -805,7 +850,8 @@ bool IsPassThroughConstructor(const clang::CXXConstructorDecl *ctor) { } bool IsConvertibleCXXRecordDecl(const clang::CXXRecordDecl *decl) { - return decl->isThisDeclarationADefinition() && !decl->isDependentContext(); + return decl->isThisDeclarationADefinition() && !decl->isDependentContext() && + !decl->isInvalidDecl(); } bool IsConvertibleCXXMethodDecl(const clang::CXXMethodDecl *decl) { @@ -1707,6 +1753,15 @@ void DefineImplicitMembers(clang::Sema &sema, clang::CXXRecordDecl *decl) { tu_scope.setEntity(sema.Context.getTranslationUnitDecl()); auto *saved_tu_scope = std::exchange(sema.TUScope, &tu_scope); sema.ForceDeclarationOfImplicitMembers(decl); + for (auto *field : decl->fields()) { + if (field->hasInClassInitializer() && !field->getInClassInitializer()) { + RuleDecls::Trial trial(sema); + clang::Sema::GlobalEagerInstantiationScope instantiations( + sema, /*Enabled=*/true, /*AtEndOfTU=*/false); + sema.BuildCXXDefaultInitExpr(decl->getLocation(), field); + instantiations.perform(); + } + } auto define = [&](clang::FunctionDecl *fn, auto &&definer) { RuleDecls::Trial trial(sema); clang::DiagnosticErrorTrap errors(sema.getDiagnostics()); diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index acdb97eccb..80842e0ee7 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -57,6 +57,11 @@ bool IsUserDefinedDecl(const clang::Decl *decl); bool IsAbstractUserClass(const clang::RecordDecl *decl); +bool IsLibraryOnlySpecialization( + const clang::ClassTemplateSpecializationDecl *decl); + +bool IsLibraryOnlySpecialization(const clang::FunctionDecl *decl); + bool ExprHasUserDefinedDecl(const clang::Expr *expr); bool IsUnsignedArithOp(const clang::BinaryOperator *expr); diff --git a/rules/map/src.cpp b/rules/map/src.cpp index 53ff394f75..86f28b47a8 100644 --- a/rules/map/src.cpp +++ b/rules/map/src.cpp @@ -6,10 +6,11 @@ #include #include -template using t1 = std::map; +template +using t1 = std::map; -template -using t4 = typename std::map::size_type; +template +using t4 = typename std::map::size_type; template using t5 = typename std::map::key_type; @@ -21,8 +22,10 @@ template T2 &f1(std::map &o, const T1 &key) { return o[key]; } -template std::size_t f2(const std::map &o) { - return o.size(); +template +typename std::map::size_type +f2(const std::map &a0) { + return a0.size(); } template @@ -31,8 +34,9 @@ typename std::map::iterator f3(std::map &o, return o.erase(it); } -template std::map f5() { - return std::map(); +template +std::map f5() { + return std::map(); } template @@ -44,8 +48,9 @@ template T2 &f7(std::map &o, const T1 &key) { return o.at(key); } -template T2 &f8(std::map &o, T1 &&key) { - return o[std::move(key)]; +template +T2 &f8(std::map &a0, T1 &&a1) { + return a0.operator[](std::move(a1)); } template @@ -83,9 +88,10 @@ template std::map f24(std::map &&o) { return std::map(std::move(o)); } -template -std::map &f25(std::map &dst, std::map &&src) { - return dst = std::move(src); +template +std::map &f25(std::map &a0, + std::map &&a1) { + return a0.operator=(std::move(a1)); } template diff --git a/tests/std/library/requirements/utility.requirements/allocator.requirements/allocator.requirements.general.cpp b/tests/std/library/requirements/utility.requirements/allocator.requirements/allocator.requirements.general.cpp new file mode 100644 index 0000000000..892e99e8bb --- /dev/null +++ b/tests/std/library/requirements/utility.requirements/allocator.requirements/allocator.requirements.general.cpp @@ -0,0 +1,73 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include +#include +#include +#include +#include + +// N4950 [allocator.requirements.general]/16: X::rebind::other is the +// allocator of U that corresponds to X. +namespace p16_rebind_member { +template struct Alloc { + using value_type = T; + + int id = 0; + + Alloc() = default; + + template Alloc(const Alloc &other) : id(other.id) {} + + template struct rebind { + using other = Alloc; + }; + + T *allocate(std::size_t n) { return std::allocator().allocate(n); } + + void deallocate(T *p, std::size_t n) { std::allocator().deallocate(p, n); } +}; + +template +bool operator==(const Alloc &a, const Alloc &b) { + return a.id == b.id; +} + +void test() { + std::map, Alloc>> m; + std::map, Alloc>> n; + m[1] = 2; + n = std::move(m); + assert(n[1] == 2); + assert(n.size() == 1); +} +} // namespace p16_rebind_member + +// N4950 [allocator.requirements.general]/18: if Allocator is SomeAllocator +// and does not supply a rebind member template, rebind_alloc is +// SomeAllocator. +namespace p18_rebind_to_container_node { +template struct Alloc { + using value_type = T; + + Alloc() = default; + + template Alloc(const Alloc &) {} + + T *allocate(std::size_t n) { return std::allocator().allocate(n); } + + void deallocate(T *p, std::size_t n) { std::allocator().deallocate(p, n); } +}; + +void test() { + std::map, Alloc>> m; + m[1] = 2; + assert(m[1] == 2); + assert(m.size() == 1); +} +} // namespace p18_rebind_to_container_node + +int main() { + p16_rebind_member::test(); + p18_rebind_to_container_node::test(); + return 0; +} diff --git a/tests/std/library/requirements/utility.requirements/allocator.requirements/out/refcount/allocator.requirements.general.rs b/tests/std/library/requirements/utility.requirements/allocator.requirements/out/refcount/allocator.requirements.general.rs new file mode 100644 index 0000000000..9daaa06350 --- /dev/null +++ b/tests/std/library/requirements/utility.requirements/allocator.requirements/out/refcount/allocator.requirements.general.rs @@ -0,0 +1,104 @@ +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}; +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(1)] +pub struct p16_rebind_member_Alloc_std_pair_const_int__int___rebind_std_pair_const_int__int__ {} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[byte_size(4)] +pub struct p16_rebind_member_Alloc_std_pair_const_int__int__ { + #[offset(0)] + pub id: i32, +} +impl Default for p16_rebind_member_Alloc_std_pair_const_int__int__ { + fn default() -> Self { + { p16_rebind_member_Alloc_std_pair_const_int__int__ { id: 0 } } + } +} +pub fn test_0() { + let m: Value, Value)>> = Rc::new(RefCell::new(Map::new())); + let n: Value, Value)>> = Rc::new(RefCell::new(Map::new())); + Map::subscript_move( + m.as_pointer(), + Rc::new(RefCell::new(1)).as_pointer(), + |__k| cpp_move!(__k), + ) + .write(2); + { + let __src = m + .as_pointer() + .with_mut(|__m: &mut Map, Value)>| std::mem::take(__m)); + n.as_pointer().write(__src) + }; + assert!( + ((Map::subscript_move( + n.as_pointer(), + Rc::new(RefCell::new(1)).as_pointer(), + |__k| cpp_move!(__k) + ) + .read()) + == 2) + ); + assert!(({ ((*n.borrow()).len() == 1_usize) })); +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(1)] +pub struct p18_rebind_to_container_node_Alloc_std_pair_const_int__int__ {} +pub fn test_1() { + let m: Value, Value)>> = Rc::new(RefCell::new(Map::new())); + Map::subscript_move( + m.as_pointer(), + Rc::new(RefCell::new(1)).as_pointer(), + |__k| cpp_move!(__k), + ) + .write(2); + assert!( + ((Map::subscript_move( + m.as_pointer(), + Rc::new(RefCell::new(1)).as_pointer(), + |__k| cpp_move!(__k) + ) + .read()) + == 2) + ); + assert!(({ ((*m.borrow()).len() == 1_usize) })); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + ({ test_1() }); + return 0; +} +pub trait p16_rebind_member_Alloc_std_pair_const_int__int__Impl { + fn allocate(&self, mut n: usize) -> Ptr<(Value, Value)> { + unimplemented!() + } + fn deallocate(&self, mut p: Ptr<(Value, Value)>, mut n: usize) { + unimplemented!() + } +} +impl p16_rebind_member_Alloc_std_pair_const_int__int__Impl + for Ptr +{ +} +pub trait p18_rebind_to_container_node_Alloc_std_pair_const_int__int__Impl { + fn allocate(&self, mut n: usize) -> Ptr<(Value, Value)> { + unimplemented!() + } + fn deallocate(&self, mut p: Ptr<(Value, Value)>, mut n: usize) { + unimplemented!() + } +} +impl p18_rebind_to_container_node_Alloc_std_pair_const_int__int__Impl + for Ptr +{ +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/library/requirements/utility.requirements/allocator.requirements/out/unsafe/allocator.requirements.general.rs b/tests/std/library/requirements/utility.requirements/allocator.requirements/out/unsafe/allocator.requirements.general.rs new file mode 100644 index 0000000000..648fa7e8a5 --- /dev/null +++ b/tests/std/library/requirements/utility.requirements/allocator.requirements/out/unsafe/allocator.requirements.general.rs @@ -0,0 +1,50 @@ +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; +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct p16_rebind_member_Alloc_std_pair_const_int__int___rebind_std_pair_const_int__int__ {} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg)] +pub struct p16_rebind_member_Alloc_std_pair_const_int__int__ { + pub id: i32, +} +impl Default for p16_rebind_member_Alloc_std_pair_const_int__int__ { + fn default() -> Self { + unsafe { p16_rebind_member_Alloc_std_pair_const_int__int__ { id: 0 } } + } +} +pub unsafe fn test_0() { + let mut m: UnsafeMap = UnsafeMap::new(); + let mut n: UnsafeMap = UnsafeMap::new(); + (*m.subscript_move(&mut 1, |__k| cpp_move_unsafe!(__k))) = 2; + n = std::mem::take(&mut m); + assert!(((*n.subscript_move(&mut 1, |__k| cpp_move_unsafe!(__k))) == (2))); + assert!(((n.len()) == (1_usize))); +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct p18_rebind_to_container_node_Alloc_std_pair_const_int__int__ {} +pub unsafe fn test_1() { + let mut m: UnsafeMap = UnsafeMap::new(); + (*m.subscript_move(&mut 1, |__k| cpp_move_unsafe!(__k))) = 2; + assert!(((*m.subscript_move(&mut 1, |__k| cpp_move_unsafe!(__k))) == (2))); + assert!(((m.len()) == (1_usize))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_0() }); + (unsafe { test_1() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/std/temp/temp.spec/out/refcount/temp.inst.rs b/tests/std/temp/temp.spec/out/refcount/temp.inst.rs new file mode 100644 index 0000000000..f0b1429eb1 --- /dev/null +++ b/tests/std/temp/temp.spec/out/refcount/temp.inst.rs @@ -0,0 +1,57 @@ +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}; +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[byte_size(4)] +pub struct p2_reference_does_not_instantiate_Member { + #[offset(0)] + pub value: i32, +} +impl p2_reference_does_not_instantiate_Member { + pub fn new_1(_a0: Ptr) -> Self { + Self { value: 2 } + } +} +impl Default for p2_reference_does_not_instantiate_Member { + fn default() -> Self { + { p2_reference_does_not_instantiate_Member { value: 1 } } + } +} +#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, Default)] +#[byte_size(4)] +pub struct p2_reference_does_not_instantiate_Holder { + #[offset(0)] + #[byte_size(4)] + pub member: p2_reference_does_not_instantiate_Member, +} +impl p2_reference_does_not_instantiate_Holder { + pub fn move_from(_a0: Ptr) -> Self { + Self { + member: { (*_a0.upgrade().deref()).member.clone() }, + } + } +} +pub fn test_0() { + let mut holder: p2_reference_does_not_instantiate_Holder = + ::default(); + assert!((holder.member.value == 1)); + holder.member.value = 3; + assert!((holder.member.value == 3)); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + return 0; +} +#[derive(Clone, Copy, Default, ByteRepr, VaArg, FnPtrArg)] +#[byte_size(1)] +pub struct p2_reference_does_not_instantiate_Wrapper_int_; +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/temp/temp.spec/out/unsafe/temp.inst.rs b/tests/std/temp/temp.spec/out/unsafe/temp.inst.rs new file mode 100644 index 0000000000..ea8cd8ebeb --- /dev/null +++ b/tests/std/temp/temp.spec/out/unsafe/temp.inst.rs @@ -0,0 +1,57 @@ +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; +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg)] +pub struct p2_reference_does_not_instantiate_Member { + pub value: i32, +} +impl p2_reference_does_not_instantiate_Member { + pub unsafe fn new_1(_a0: *const p2_reference_does_not_instantiate_Wrapper_int_) -> Self { + let mut this = Self { value: 2 }; + this + } +} +impl Default for p2_reference_does_not_instantiate_Member { + fn default() -> Self { + unsafe { p2_reference_does_not_instantiate_Member { value: 1 } } + } +} +#[repr(C)] +#[derive(FnPtrArg, MoveCtorUnsafe, Default)] +pub struct p2_reference_does_not_instantiate_Holder { + pub member: p2_reference_does_not_instantiate_Member, +} +impl p2_reference_does_not_instantiate_Holder { + pub unsafe fn move_from(_a0: *mut p2_reference_does_not_instantiate_Holder) -> Self { + let mut this = Self { + member: (*_a0).member, + }; + this + } +} +pub unsafe fn test_0() { + let mut holder: p2_reference_does_not_instantiate_Holder = + ::default(); + assert!(((holder.member.value) == (1))); + holder.member.value = 3; + assert!(((holder.member.value) == (3))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_0() }); + return 0; +} +#[derive(Clone, Copy, Default, VaArg, FnPtrArg)] +pub struct p2_reference_does_not_instantiate_Wrapper_int_; +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/std/temp/temp.spec/temp.inst.cpp b/tests/std/temp/temp.spec/temp.inst.cpp new file mode 100644 index 0000000000..a5896e335b --- /dev/null +++ b/tests/std/temp/temp.spec/temp.inst.cpp @@ -0,0 +1,33 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include + +// N4950 [temp.inst]/2: a class template specialization is implicitly +// instantiated only when it is referenced in a context that requires a +// completely-defined object type. +namespace p2_reference_does_not_instantiate { +template struct Wrapper { + typename T::type value; +}; + +struct Member { + int value = 1; + Member() = default; + explicit Member(const Wrapper &) : value(2) {} +}; + +struct Holder { + Member member; +}; + +void test() { + Holder holder; + assert(holder.member.value == 1); + holder.member.value = 3; + assert(holder.member.value == 3); +} +} // namespace p2_reference_does_not_instantiate + +int main() { + p2_reference_does_not_instantiate::test(); + return 0; +} diff --git a/tests/unit/in_class_field_initializer.cpp b/tests/unit/in_class_field_initializer.cpp index 70015ccf25..a99742d9f6 100644 --- a/tests/unit/in_class_field_initializer.cpp +++ b/tests/unit/in_class_field_initializer.cpp @@ -13,13 +13,6 @@ struct S { Inner d; }; -// Boxed::tag is in-class initialized. However the default constructor is never -// instantiated, only the explicit one is used. -// -// Because no default constructor is instantiated, the specialization does not -// contain the in-class initializer. In Rust, the Default trait initializes -// Boxed::tag with 0. This is correct because the C++ program never reads the -// in-class initializer of Boxed::tag, hence Rust also does not read it. template struct Boxed { T v = T(); int tag = 7; diff --git a/tests/unit/out/refcount/in_class_field_initializer.rs b/tests/unit/out/refcount/in_class_field_initializer.rs index 22463a37eb..a6bfe4905c 100644 --- a/tests/unit/out/refcount/in_class_field_initializer.rs +++ b/tests/unit/out/refcount/in_class_field_initializer.rs @@ -60,12 +60,7 @@ impl Boxed_int_ { } impl Default for Boxed_int_ { fn default() -> Self { - { - Boxed_int_ { - v: 0_i32, - tag: 0_i32, - } - } + { Boxed_int_ { v: 0_i32, tag: 7 } } } } pub fn main() { diff --git a/tests/unit/out/unsafe/in_class_field_initializer.rs b/tests/unit/out/unsafe/in_class_field_initializer.rs index 7a87ba2115..1aa9f77521 100644 --- a/tests/unit/out/unsafe/in_class_field_initializer.rs +++ b/tests/unit/out/unsafe/in_class_field_initializer.rs @@ -51,12 +51,7 @@ impl Boxed_int_ { } impl Default for Boxed_int_ { fn default() -> Self { - unsafe { - Boxed_int_ { - v: 0_i32, - tag: 0_i32, - } - } + unsafe { Boxed_int_ { v: 0_i32, tag: 7 } } } } pub fn main() {