Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2021-12

Assigning instance methods, accessed via a type, to method pointers

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

Subj silently produces invalid codegen:
-------8<-------
var Z: procedure of object;

type R = record
	procedure Foo;
end;
procedure R.Foo; begin end;
type O = object
	procedure Foo;
end;
procedure O.Foo; begin end;
type C = class
	procedure Foo;
	class procedure ClassCtx;
end;
procedure C.Foo; begin end;
class procedure C.ClassCtx;
begin
	Z := Foo; // BadCG: .Code = C.Foo; .Data = C
end;
type CC = class of C;
var aCC: CC = nil;
type H = class helper for C
	procedure Bar;
	class procedure ClassCtx2;
end;
procedure H.Bar; begin end;
class procedure H.ClassCtx2;
begin
	Z := Bar; // BadCG: .Code = H.Bar; .Data = C
end;
begin
	Z := R.Foo; // BadCG: GARBAGE
	Z := O.Foo; // BadCG: GARBAGE
	Z := C.Foo; // BadCG: .Code = C.Foo; .Data = C
	Z := CC.Foo; // BadCG: GARBAGE
	Z := aCC.Foo; // BadCG: .Code = C.Foo; .Data = aCC
	// Currently allowed, and we get the fix for this for free.
	// Such qualification may become rejected;
	//	see https://lists.freepascal.org/pipermail/fpc-devel/2021-December/044251.html
	Z := H.Bar; // BadCG: GARBAGE
end.
-------8<-------

The attached methptr_to_instancemeth_via_type.patch catches all these cases and reports the proper error:

Error: Only class methods, class properties and class variables can be referred with class references
βþ

Reply by email

Sven Barth
to Unknown author
Blaise--- wrote 57 lines
Subj silently produces invalid codegen: -------8<------- var Z: procedure of object; type R = record     procedure Foo; end; procedure R.Foo; begin end; type O = object     procedure Foo; end; procedure O.Foo; begin end; type C = class     procedure Foo;     class procedure ClassCtx; end; procedure C.Foo; begin end; class procedure C.ClassCtx; begin     Z := Foo; // BadCG: .Code = C.Foo; .Data = C end; type CC = class of C; var aCC: CC = nil; type H = class helper for C     procedure Bar;     class procedure ClassCtx2; end; procedure H.Bar; begin end; class procedure H.ClassCtx2; begin     Z := Bar; // BadCG: .Code = H.Bar; .Data = C end; begin     Z := R.Foo; // BadCG: GARBAGE     Z := O.Foo; // BadCG: GARBAGE     Z := C.Foo; // BadCG: .Code = C.Foo; .Data = C     Z := CC.Foo; // BadCG: GARBAGE     Z := aCC.Foo; // BadCG: .Code = C.Foo; .Data = aCC     // Currently allowed, and we get the fix for this for free.     // Such qualification may become rejected;     //    see https://lists.freepascal.org/pipermail/fpc-devel/2021-December/044251.html     Z := H.Bar; // BadCG: GARBAGE end. -------8<------- The attached methptr_to_instancemeth_via_type.patch catches all these cases and reports the proper error: > Error: Only class methods, class properties and class variables can > be referred with class references

Accepted.

Regards,
Sven

Reply by email