cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[BUG] UFCS Not Working with Pointer Dereferncing

Open jdbener opened this issue 3 years ago • 2 comments

I was experimenting a little bit and discovered that UFCS would not work on a pointer that is being de-referenced for example:

main : () -> int = {
	life := 42;
	p := life&;

	life.someFunction();
	p*.someFunction();  // Not UFCSed
}

someFunction : (x : int) -> int = {
	// Do something useful...
}

Will emit the following Syntax1 code:

// ----- Cpp2 support -----
#define CPP2_USE_SOURCE_LOCATION Yes
#include "cpp2util.h"


#line 1 "helloworld.cpp2"
[[nodiscard]] auto main() -> int;
#line 9 "helloworld.cpp2"
[[nodiscard]] auto someFunction(cpp2::in<int> x) -> int;

//=== Cpp2 definitions ==========================================================

#line 1 "helloworld.cpp2"
[[nodiscard]] auto main() -> int{
 auto life { 42 }; 
 auto p { &life }; 

 CPP2_UFCS_0(someFunction, life);
  *p .someFunction();  // Fails to compile!!
}

[[nodiscard]] auto someFunction(cpp2::in<int> x) -> int{
 // Do something useful...
}

Which fails to compile since we are trying to call someFunction as a member of p instead of running it through the UFCS system. I'm not entirely sure if this is intended behavior or not... but either way it is a little unintuitive.

jdbener avatar Oct 01 '22 06:10 jdbener

Right, at the moment UFCS is hardcoded for just x.f(...) at a single level. When I fix the other issues about chaining it should fix this too, but it may be a couple of weeks before I get to it.

However, even now that seems odd, because without UFCS p*.f() should be generating (*p).f(), not *p.f(), just as a matter of translating the operators correctly. I'll take a look at this too -- it's almost certainly an edge case in my logic that suppresses emitting needless/redundant parens in chains of operators.

hsutter avatar Oct 05 '22 00:10 hsutter

However, even now that seems odd, because without UFCS p*.f() should be generating (*p).f(), not *p.f(), just as a matter of translating the operators correctly. I'll take a look at this too -- it's almost certainly an edge case in my logic that suppresses emitting needless/redundant parens in chains of operators.

It seems the most recent set of bug fixes fixed this particular problem (at least in my non-thoroughly unit tested experiments).

jdbener avatar Oct 05 '22 04:10 jdbener