mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Export methods taking self as const member functions (#7577)
A method declared with `self` does not modify the object, but it was
exported to C++ as a non-const member function, so calling it on a const
reference would fail.
```carbon
class C {
fn Get(self);
}
inline Cpp '''
void F(const Carbon::C& c) {
c.Get();
}
''';
```
```
error: 'this' argument to member function 'Get' has type 'const Carbon::C', but function is not marked const
```
Import already maps `f() const` to `fn f(self)`, and this PR implements
the same behavior for exporting. No ref-qualifier is added, since that
maps to `ref self`, so that is unchanged.
`GetThisArg()` now builds `this` from the method instead of the parent
record, so that it picks up the method's const-qualifier.
This commit is contained in:
@@ -53,6 +53,20 @@ void F() {
|
||||
}
|
||||
''';
|
||||
|
||||
// --- method_const_lvalue.carbon
|
||||
library "[[@TEST_NAME]]";
|
||||
import Cpp;
|
||||
|
||||
class C {
|
||||
fn M(self);
|
||||
}
|
||||
|
||||
inline Cpp '''
|
||||
void F(const Carbon::C& c) {
|
||||
c.M();
|
||||
}
|
||||
''';
|
||||
|
||||
// --- ref_method_lvalue.carbon
|
||||
library "[[@TEST_NAME]]";
|
||||
import Cpp;
|
||||
@@ -89,6 +103,27 @@ void F() {
|
||||
}
|
||||
''';
|
||||
|
||||
// --- fail_ref_method_const_lvalue.carbon
|
||||
library "[[@TEST_NAME]]";
|
||||
import Cpp;
|
||||
|
||||
class C {
|
||||
fn M(ref self);
|
||||
}
|
||||
|
||||
inline Cpp '''
|
||||
void F(const Carbon::C& c) {
|
||||
// CHECK:STDERR: fail_ref_method_const_lvalue.carbon:[[@LINE+7]]:3: error: 'this' argument to member function 'M' has type 'const Carbon::C', but function is not marked const [CppInteropParseError]
|
||||
// CHECK:STDERR: 17 | c.M();
|
||||
// CHECK:STDERR: | ^
|
||||
// CHECK:STDERR: fail_ref_method_const_lvalue.carbon:[[@LINE-8]]:17: note: 'M' declared here [CppInteropParseNote]
|
||||
// CHECK:STDERR: 5 | fn M(ref self);
|
||||
// CHECK:STDERR: | ^
|
||||
// CHECK:STDERR:
|
||||
c.M();
|
||||
}
|
||||
''';
|
||||
|
||||
// --- method_alias.carbon
|
||||
library "[[@TEST_NAME]]";
|
||||
import Cpp;
|
||||
|
||||
Reference in New Issue
Block a user