Skip to content

Commit 098f870

Browse files
authored
[TS/Python] Fix thisArg type for overloads in structs (#4453) (#4466)
1 parent a19a90a commit 098f870

9 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/Fable.Cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
* [Python] Add `Math.DivRem` support for int, int64, and bigint (by @dbrattli)
1515
* [Python] Fix modulo with negative numbers using Python floored semantics instead of .NET truncated semantics for bigint (fixes #4462) (by @dbrattli)
1616
* [Beam] Fix `System.String.Concat` with 4+ arguments not being supported (by @dbrattli)
17+
* [TS/Python] Fix thisArg type for overloads in structs (#4453) (by @ncave)
1718
* [TS/Python] Fix invalid `this` argument type in structs (#4453) (by @ncave)
1819
* [JS/TS] Fix `N` format specifier (`ToString("N0")`, `String.Format("{0:N0}", ...)`) producing a trailing dot when precision is 0 (fix #2582) (by @MangelMaxime)
1920
* [JS/TS] Fix `C0` and `P0` format specifiers producing trailing dot (e.g., `"¤1,000."``"¤1,000"`) (by @MangelMaxime)

src/Fable.Compiler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
* [Python] Add `Math.DivRem` support for int, int64, and bigint (by @dbrattli)
1515
* [Python] Fix modulo with negative numbers using Python floored semantics instead of .NET truncated semantics for bigint (fixes #4462) (by @dbrattli)
1616
* [Beam] Fix `System.String.Concat` with 4+ arguments not being supported (by @dbrattli)
17+
* [TS/Python] Fix thisArg type for overloads in structs (#4453) (by @ncave)
1718
* [TS/Python] Fix invalid `this` argument type in structs (#4453) (by @ncave)
1819
* [JS/TS] Fix `N` format specifier (`ToString("N0")`, `String.Format("{0:N0}", ...)`) producing a trailing dot when precision is 0 (fix #2582) (by @MangelMaxime)
1920
* [JS/TS] Fix `C0` and `P0` format specifiers producing trailing dot (e.g., `"¤1,000."``"¤1,000"`) (by @MangelMaxime)

src/Fable.Transforms/Fable2Babel.fs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,12 @@ module Util =
13461346
let body =
13471347
// TODO: If ident is not captured maybe we can just replace it with "this"
13481348
if isIdentUsed thisArg.Name body then
1349+
let thisArgType =
1350+
match thisArg.Type with
1351+
| Replacements.Util.IsByRefType com typ -> typ
1352+
| typ -> typ
1353+
1354+
let thisArg = { thisArg with Type = thisArgType }
13491355
let thisIdent = Fable.IdentExpr { thisArg with Name = "this" }
13501356

13511357
let thisIdent =
@@ -3331,7 +3337,6 @@ but thanks to the optimisation done below we get
33313337
List.zip args tc.Args
33323338
|> List.map (fun (id, tcArg) ->
33333339
let ta = makeArgTypeAnnotation com ctx id
3334-
33353340
Parameter.parameter (tcArg, ?typeAnnotation = ta)
33363341
)
33373342

src/Fable.Transforms/Python/Fable2Python.Transforms.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ let getMemberArgsAndBody (com: IPythonCompiler) ctx kind hasSpread (args: Fable.
8383

8484
let body =
8585
if isIdentUsed thisArg.Name body then
86+
let thisArgType =
87+
match thisArg.Type with
88+
| Replacements.Util.IsByRefType com typ -> typ
89+
| typ -> typ
90+
91+
let thisArg = { thisArg with Type = thisArgType }
8692
let thisKeyword = Fable.IdentExpr { thisArg with Name = "self" }
8793

8894
Fable.Let(thisArg, thisKeyword, body)

tests/Beam/TypeTests.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type ValueTypeR =
4848
val mutable X: float
4949
new(x: float) = { X = x }
5050
member this.IsEmpty() = this.X < 0.0
51+
override this.ToString() = $"{this.X}"
5152

5253
[<Struct>]
5354
type StructUnion = Value of string

tests/Dart/src/TypeTests.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ open Util
269269
// val mutable X: float
270270
// new(x: float) = { X = x }
271271
// member this.IsEmpty() = this.X < 0.0
272+
// override this.ToString() = $"{this.X}"
272273

273274
// [<Struct>]
274275
// type StructUnion = Value of string

tests/Js/Main/TypeTests.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ type ValueTypeR =
272272
val mutable X: float
273273
new(x: float) = { X = x }
274274
member this.IsEmpty() = this.X < 0.0
275+
override this.ToString() = $"{this.X}"
275276

276277
[<Struct>]
277278
type StructUnion = Value of string

tests/Python/TestType.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ type ValueTypeR =
303303
val mutable X: float
304304
new(x: float) = { X = x }
305305
member this.IsEmpty() = this.X < 0.0
306+
override this.ToString() = $"{this.X}"
306307

307308
[<Struct>]
308309
type StructUnion = Value of string

tests/Rust/tests/src/TypeTests.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ type ValueTypeR =
269269
val mutable X: float
270270
new(x: float) = { X = x }
271271
member this.IsEmpty() = this.X < 0.0
272+
override this.ToString() = $"{this.X}"
272273

273274
[<Struct>]
274275
type StructUnion = Value of string

0 commit comments

Comments
 (0)