D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 19381 - capture pointer in nested function should not be called "this"
Summary: capture pointer in nested function should not be called "this"
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All Windows
: P1 normal
Assignee: No Owner
URL:
Keywords: DebugInfo, pull
Depends on:
Blocks:
 
Reported: 2018-11-09 09:02 UTC by Rainer Schuetze
Modified: 2018-11-19 04:43 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Rainer Schuetze 2018-11-09 09:02:48 UTC
struct Struct
{
	int x = 3;
	int foo()
	{
		int y = 5;
		int nested()
		{
			int z = 7;
			return x + y + z;
		}
		return nested();
	}
}

int main()
{
   return Struct().foo();
}

When compiled with -g, this shows a variable in nested() called "this", but is not the struct, but the capture of the outer function:

(00004C) S_GPROC32: [0000:00000000], Cb: 0000002B, Type:             0x1004, dg.Struct.foo.nested
         Parent: 00000000, End: 00000000, Next: 00000000
         Debug start: 0000000C, Debug end: 00000026

(000088)  S_REGREL32: rbp+00000010, Type:             0x1019, this
(0000A0)  S_ENDARG
(0000A4)  S_REGREL32: rbp+FFFFFFF8, Type:       T_INT4(0074), z

(0000B4) S_END

The type of "this" used to be void*, but has recently been populated with the captured stack variables of the outer function:

0x1016 : Length = 42, Leaf = 0x1505 LF_STRUCTURE
	# members = 2,  field list type 0x1017, 
	Derivation list type 0x0000, VT shape type 0x0000
	Size = 0, class name = CAPTURE.dg.Struct.foo

0x1017 : Length = 34, Leaf = 0x1203 LF_FIELDLIST
	list[0] = LF_MEMBER, public, type = 0x1015, offset = 16
		member name = 'this'
	list[1] = LF_MEMBER, public, type = 0x1002, offset = (LF_SHORT) -8
		member name = 'y'

0x1019 : Length = 10, Leaf = 0x1002 LF_POINTER
	Pointer (__ptr64), Size: 8
	Element type : 0x1016

Note the "this" member of the capture struct. Having multiple "this" variables confuses both the debugger and the user.
Comment 1 Rainer Schuetze 2018-11-09 09:08:33 UTC
https://github.com/dlang/dmd/pull/8933
Comment 2 github-bugzilla 2018-11-09 13:34:51 UTC
Commits pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/7950336bbf810c62b2d3b3c75a3d48c94042c64f
fix issue 19381 - capture pointer in nested function should not be called "this"

rename the context pointer to the closure or the outer functions stack frame to '__capture'

https://github.com/dlang/dmd/commit/9f1454be12e0ea89c984d277143386aa2b584cda
Merge pull request #8933 from rainers/capture_name

fix issue 19381 - capture pointer in nested function should not be called "this"
merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>