References get pointer semantics In rules
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
The issue proposes splitting the translation of C++ pointers and references into separate Rust types (Ptr, LvalueRef, RvalueRef) so that rules and STL types like std::pair<int&, int> get correct semantics. Start from the examples in the issue, then find where the translator currently maps both T* and T& to Ptr<T>. Done means the assignment and reference-collapsing examples behave as C++ does, and the design is agreed in the thread.
由索引模型根据 Issue 内容生成。
描述
Both T& and T* are translated as Ptr<T>. When a rule is applied on a reference, the rule sees a Ptr<T> and applies pointer semantics on it.
int x = 1, y = 2;
std::pair<int&, int> a{x, 0}, b{y, 0};
// C++: ✅ assigns through a.first, x == 2
// Translated: ❌ a.first now points to y, x == 1
a = b;
a = std::move(b); // same
std::pair<int&, int> and std::pair<int*, int> are the same translated type so no rule can give them different semantics. Same happens for any STL type that can hold references.
I think that the complete solution is to translate pointers and references as:
T*->Ptr<T>T&->LvalueRef<T>T&&->RvalueRef<T>
All share the layout of Ptr: PtrKind + offset, but the operations differ:
std::pair<int*, int> a(&x, 0), b(&y, 0);
a = b; // T1 = Ptr<i32>: copy_assign copies the address; a.first points to y
std::pair<int&, int> c(x, 0), d(y, 0);
c = d; // T1 = LvalueRef<i32>: copy_assign writes y's value into x; c.first still refers to x
c == d; // LvalueRef<i32>'s == compares x and y, not their addresses
The distinction between Ptr<T>, LvalueRef<T> and RvalueRef<T> is also relevant for reference collapsing which master gets wrong:
int x = 1;
std::pair<int&, int> a(x, 0);
// C++: ✅ writes x, prints x=7
// Translated: ❌ does not compile, std::get<0>(a) is Ptr<Ptr<i32>> instead of Ptr<i32>
std::get<0>(a) = 7;
std::printf("x=%d\n", x);
The reference collapsing rules would be encoded as:
LvalueRef<LvalueRef<T>> => LvalueRef<T> // T& & -> T&
RvalueRef<LvalueRef<T>> => LvalueRef<T> // T& && -> T&
LvalueRef<RvalueRef<T>> => LvalueRef<T> // T&& & -> T&
RvalueRef<RvalueRef<T>> => RvalueRef<T> // T&& && -> T&&
- 主要语言
- Rust
- 星标
- 310
- 派生
- 18
- 平均合并
- 9 小时 46 分钟
- 30 天内合并 PR
- 82
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
Cpp2Rust/cpp2rust 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 79/100
Cpp2Rust/cpp2rust#610 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 30/100
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 22/100
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 35/100
Cpp2Rust/cpp2rust#674 · 1 条评论 ·
维护者通常 1 天内回复
查看 Cpp2Rust/cpp2rust 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 69/100
维护者通常 1 天内回复
-
test_a_503_raises_unavailable_error fails on macOS about one run in six可能已有人在做 @naman-modi 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
bug ui
难度 2/5 1-3 小时 新手友好度 72/100
userFRM/egui-charts#4168 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 85/100
维护者通常 1 天内回复