From 3b9ddebb3899ae02d295eadcd608c59d9812ee2b Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 13:48:44 +0000 Subject: [PATCH 1/9] Skip specializations instantiated by the library with its internal types --- cpp2rust/converter/converter.cpp | 12 +++++- cpp2rust/converter/converter_lib.cpp | 43 ++++++++++++++++++- cpp2rust/converter/converter_lib.h | 7 +++ rules/map/src.cpp | 19 +++++--- .../allocator.requirements.general.cpp | 38 ++++++++++++++++ 5 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 tests/std/library/requirements/utility.requirements/allocator.requirements/allocator.requirements.general.cpp diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index d1a32a9e08..f797ab69b7 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); @@ -882,7 +883,8 @@ void Converter::EmitRustStructOrUnion(clang::RecordDecl *decl) { if (auto *tmpl = clang::dyn_cast_or_null( friend_decl->getFriendDecl())) { for (auto *spec : tmpl->specializations()) { - if (spec->isThisDeclarationADefinition()) { + if (spec->isThisDeclarationADefinition() && + !IsLibraryOnlySpecialization(spec)) { VisitFunctionDecl(spec); } } @@ -904,6 +906,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 +4382,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..81a23ed2d5 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -303,6 +303,47 @@ bool IsUserDefinedDecl(const clang::Decl *decl) { !src_mgr.isInSystemMacro(src_loc); } +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 type = arg.getAsType().getCanonicalType(); + while (type->isPointerType() || type->isReferenceType() || + type->isArrayType()) { + type = type->isArrayType() + ? clang::QualType(type->getArrayElementTypeNoTypeQual(), 0) + : type->getPointeeType(); + } + auto tag = type->getAsTagDecl(); + return tag && ctx.getSourceManager().isInSystemHeader(tag->getLocation()) && + 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 +457,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); } } diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index acdb97eccb..85ecda3e0e 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -57,6 +57,13 @@ bool IsUserDefinedDecl(const clang::Decl *decl); bool IsAbstractUserClass(const clang::RecordDecl *decl); +// A specialization instantiated by the library with one of its internal +// types, e.g. a user allocator rebound to a library node type. +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..48300fe83f 100644 --- a/rules/map/src.cpp +++ b/rules/map/src.cpp @@ -6,7 +6,8 @@ #include #include -template using t1 = std::map; +template +using t1 = std::map; template using t4 = typename std::map::size_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 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..8e9a81b34e --- /dev/null +++ b/tests/std/library/requirements/utility.requirements/allocator.requirements/allocator.requirements.general.cpp @@ -0,0 +1,38 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include +#include +#include +#include +#include + +// 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() { + p18_rebind_to_container_node::test(); + return 0; +} From abab925b4f7de84161e09ea60de850ad5f672098 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 13:48:44 +0000 Subject: [PATCH 2/9] Skip invalid record definitions --- cpp2rust/converter/converter_lib.cpp | 3 ++- tests/std/temp/temp.spec/temp.inst.cpp | 33 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/std/temp/temp.spec/temp.inst.cpp diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 81a23ed2d5..f6e472e0b2 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -846,7 +846,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) { 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; +} From fb21e05818b1b215bc747691b876d1cf2c7e8362 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 14:23:24 +0000 Subject: [PATCH 3/9] Update tests --- .../allocator.requirements.general.rs | 51 +++++++++++++++++ .../unsafe/allocator.requirements.general.rs | 28 +++++++++ .../temp/temp.spec/out/refcount/temp.inst.rs | 57 +++++++++++++++++++ .../temp/temp.spec/out/unsafe/temp.inst.rs | 57 +++++++++++++++++++ 4 files changed, 193 insertions(+) create mode 100644 tests/std/library/requirements/utility.requirements/allocator.requirements/out/refcount/allocator.requirements.general.rs create mode 100644 tests/std/library/requirements/utility.requirements/allocator.requirements/out/unsafe/allocator.requirements.general.rs create mode 100644 tests/std/temp/temp.spec/out/refcount/temp.inst.rs create mode 100644 tests/std/temp/temp.spec/out/unsafe/temp.inst.rs 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..6ae3994353 --- /dev/null +++ b/tests/std/library/requirements/utility.requirements/allocator.requirements/out/refcount/allocator.requirements.general.rs @@ -0,0 +1,51 @@ +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 p18_rebind_to_container_node_Alloc_std_pair_const_int__int__ {} +pub fn test_0() { + 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_u64) })); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + return 0; +} +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..0138151dd8 --- /dev/null +++ b/tests/std/library/requirements/utility.requirements/allocator.requirements/out/unsafe/allocator.requirements.general.rs @@ -0,0 +1,28 @@ +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 p18_rebind_to_container_node_Alloc_std_pair_const_int__int__ {} +pub unsafe fn test_0() { + 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_u64))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_0() }); + 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() {} From 40f06f0604daf21eb4482237dde29003ce2a77c4 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 14:32:42 +0000 Subject: [PATCH 4/9] Add helper --- cpp2rust/converter/converter_lib.cpp | 22 +++++++++++++--------- cpp2rust/converter/converter_lib.h | 2 -- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index f6e472e0b2..c709386306 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -303,6 +303,16 @@ 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) { @@ -310,15 +320,9 @@ HasLibraryInternalArgument(clang::ASTContext &ctx, if (arg.getKind() != clang::TemplateArgument::Type) { return false; } - auto type = arg.getAsType().getCanonicalType(); - while (type->isPointerType() || type->isReferenceType() || - type->isArrayType()) { - type = type->isArrayType() - ? clang::QualType(type->getArrayElementTypeNoTypeQual(), 0) - : type->getPointeeType(); - } - auto tag = type->getAsTagDecl(); - return tag && ctx.getSourceManager().isInSystemHeader(tag->getLocation()) && + auto tag = + StripPointersAndArrays(arg.getAsType().getCanonicalType())->getAsTagDecl(); + return tag && !IsUserDefinedDecl(tag) && clang::isReservedInAllContexts(tag->isReserved(ctx.getLangOpts())); }); } diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index 85ecda3e0e..80842e0ee7 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -57,8 +57,6 @@ bool IsUserDefinedDecl(const clang::Decl *decl); bool IsAbstractUserClass(const clang::RecordDecl *decl); -// A specialization instantiated by the library with one of its internal -// types, e.g. a user allocator rebound to a library node type. bool IsLibraryOnlySpecialization( const clang::ClassTemplateSpecializationDecl *decl); From 7a173cebf3cc126541aa033320a7d6a4e16f937f Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 14:33:18 +0000 Subject: [PATCH 5/9] Update map rules --- rules/map/src.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/rules/map/src.cpp b/rules/map/src.cpp index 48300fe83f..86f28b47a8 100644 --- a/rules/map/src.cpp +++ b/rules/map/src.cpp @@ -9,8 +9,8 @@ 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; @@ -88,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 From 4c7a5d2ff3a7b1c2f7a7e5ea43caa6f29b2f42a1 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 14:37:50 +0000 Subject: [PATCH 6/9] Update tests --- cpp2rust/converter/converter.cpp | 3 +- cpp2rust/converter/converter_lib.cpp | 4 +- .../allocator.requirements.general.cpp | 41 ++++++++++++- .../allocator.requirements.general.rs | 57 ++++++++++++++++++- .../unsafe/allocator.requirements.general.rs | 26 ++++++++- 5 files changed, 120 insertions(+), 11 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index f797ab69b7..32b385f073 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -883,8 +883,7 @@ void Converter::EmitRustStructOrUnion(clang::RecordDecl *decl) { if (auto *tmpl = clang::dyn_cast_or_null( friend_decl->getFriendDecl())) { for (auto *spec : tmpl->specializations()) { - if (spec->isThisDeclarationADefinition() && - !IsLibraryOnlySpecialization(spec)) { + if (spec->isThisDeclarationADefinition()) { VisitFunctionDecl(spec); } } diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index c709386306..054a0f4dcb 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -320,8 +320,8 @@ HasLibraryInternalArgument(clang::ASTContext &ctx, if (arg.getKind() != clang::TemplateArgument::Type) { return false; } - auto tag = - StripPointersAndArrays(arg.getAsType().getCanonicalType())->getAsTagDecl(); + auto tag = StripPointersAndArrays(arg.getAsType().getCanonicalType()) + ->getAsTagDecl(); return tag && !IsUserDefinedDecl(tag) && clang::isReservedInAllContexts(tag->isReserved(ctx.getLangOpts())); }); 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 index 8e9a81b34e..892e99e8bb 100644 --- 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 @@ -6,6 +6,42 @@ #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. @@ -19,9 +55,7 @@ template struct 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 deallocate(T *p, std::size_t n) { std::allocator().deallocate(p, n); } }; void test() { @@ -33,6 +67,7 @@ void test() { } // 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 index 6ae3994353..75316cbd9d 100644 --- 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 @@ -8,8 +8,48 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; #[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] #[byte_size(1)] -pub struct p18_rebind_to_container_node_Alloc_std_pair_const_int__int__ {} +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_i32 } } + } +} 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(), @@ -26,7 +66,7 @@ pub fn test_0() { .read()) == 2) ); - assert!(({ ((*m.borrow()).len() == 1_u64) })); + assert!(({ ((*m.borrow()).len() == 1_usize) })); } pub fn main() { __cpp2rust_init_globals(); @@ -34,8 +74,21 @@ pub fn main() { } 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!() 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 index 0138151dd8..8aba8630fe 100644 --- 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 @@ -8,12 +8,33 @@ use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] #[derive(Copy, Clone, VaArg, FnPtrArg, Default)] -pub struct p18_rebind_to_container_node_Alloc_std_pair_const_int__int__ {} +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_i32 } } + } +} 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_u64))); + assert!(((m.len()) == (1_usize))); } pub fn main() { unsafe { @@ -23,6 +44,7 @@ pub fn main() { } unsafe fn main_0() -> i32 { (unsafe { test_0() }); + (unsafe { test_1() }); return 0; } pub unsafe fn __cpp2rust_init_globals() {} From 663f183fcbc1fbe6048b350701be8328d82218e1 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 15:07:05 +0000 Subject: [PATCH 7/9] Instantiate missing default member initializers of record fields --- cpp2rust/converter/converter_lib.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 054a0f4dcb..ac40ed70d0 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -1753,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()); From 62089e12cd451951c4330ad4eaabf9837d2b452a Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 15:30:45 +0000 Subject: [PATCH 8/9] Drop the comment on uninstantiated in-class initializers --- tests/unit/in_class_field_initializer.cpp | 7 ------- 1 file changed, 7 deletions(-) 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; From 49ed01912326affe6c171431b43a196d89c5f3ae Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 16:15:19 +0000 Subject: [PATCH 9/9] Update tests --- .../out/refcount/allocator.requirements.general.rs | 2 +- .../out/unsafe/allocator.requirements.general.rs | 2 +- tests/unit/out/refcount/in_class_field_initializer.rs | 7 +------ tests/unit/out/unsafe/in_class_field_initializer.rs | 7 +------ 4 files changed, 4 insertions(+), 14 deletions(-) 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 index 75316cbd9d..9daaa06350 100644 --- 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 @@ -17,7 +17,7 @@ pub struct p16_rebind_member_Alloc_std_pair_const_int__int__ { } 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_i32 } } + { p16_rebind_member_Alloc_std_pair_const_int__int__ { id: 0 } } } } pub fn test_0() { 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 index 8aba8630fe..648fa7e8a5 100644 --- 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 @@ -16,7 +16,7 @@ pub struct p16_rebind_member_Alloc_std_pair_const_int__int__ { } 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_i32 } } + unsafe { p16_rebind_member_Alloc_std_pair_const_int__int__ { id: 0 } } } } pub unsafe fn test_0() { 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() {