D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 11522 - mixing template mixins of template functions creates issues for dmd
Summary: mixing template mixins of template functions creates issues for dmd
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-11-15 13:36 UTC by Phil Lavoie
Modified: 2022-06-09 13:57 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Phil Lavoie 2013-11-15 13:36:22 UTC
mixin template innerMixin() {
  
  void someFunc( string s )() if( s == "tata" ) {
  
  }
  
}

mixin template outerMixin() {
  
  //If you comment out this function, it compiles and work.
  void someFunc( string s )() if( s == "toto" ) {
  
  }
  
  
  mixin innerMixin;

}

void main( string[] args ) {

  mixin outerMixin;
  
  static assert( __traits( compiles, mixin( "someFunc!\"tata\"()" ) ) ); //Does not pass.
}

Shouldn't it work? Version is 2.064
Comment 1 Phil Lavoie 2013-11-15 13:42:10 UTC
(In reply to comment #0)
> mixin template innerMixin() {
> 
>   void someFunc( string s )() if( s == "tata" ) {
> 
>   }
> 
> }
> 
> mixin template outerMixin() {
> 
>   //If you comment out this function, it compiles and work.
>   void someFunc( string s )() if( s == "toto" ) {
> 
>   }
> 
> 
>   mixin innerMixin;
> 
> }
> 
> void main( string[] args ) {
> 
>   mixin outerMixin;
> 
>   static assert( __traits( compiles, mixin( "someFunc!\"tata\"()" ) ) ); //Does
> not pass.
> }
> 
> Shouldn't it work? Version is 2.064

K, if you move the "outer" "someFunc" into its own mixin template, like such:

mixin template otherInnerMixin() {

  void someFunc( string s )() if( s == "toto" ) {
  
  }

}

and change "outerMixin" for this:

mixin template outerMixin() {
 
  mixin innerMixin;
  mixin otherInnerMixin;

}

Then it works.
Comment 2 Phil Lavoie 2013-11-15 13:50:22 UTC
(In reply to comment #1)
> (In reply to comment #0)
> > mixin template innerMixin() {
> > 
> >   void someFunc( string s )() if( s == "tata" ) {
> > 
> >   }
> > 
> > }
> > 
> > mixin template outerMixin() {
> > 
> >   //If you comment out this function, it compiles and work.
> >   void someFunc( string s )() if( s == "toto" ) {
> > 
> >   }
> > 
> > 
> >   mixin innerMixin;
> > 
> > }
> > 
> > void main( string[] args ) {
> > 
> >   mixin outerMixin;
> > 
> >   static assert( __traits( compiles, mixin( "someFunc!\"tata\"()" ) ) ); //Does
> > not pass.
> > }
> > 
> > Shouldn't it work? Version is 2.064
> 
> K, if you move the "outer" "someFunc" into its own mixin template, like such:
> 
> mixin template otherInnerMixin() {
> 
>   void someFunc( string s )() if( s == "toto" ) {
> 
>   }
> 
> }
> 
> and change "outerMixin" for this:
> 
> mixin template outerMixin() {
> 
>   mixin innerMixin;
>   mixin otherInnerMixin;
> 
> }
> 
> Then it works.

Scratch that, it's working for this short example but not in my real world problem.
Comment 3 RazvanN 2022-06-09 13:57:17 UTC
This is the expected behavior. The innerMixin introduces a new scope so the function that is constrainted to "toto" actually has priority over the function constrained to "tata". To achieve what you want you have to introduce innerMixin.someFunc in the same overload set as outerMixin.somefunc:

mixin template outerMixin() {
  
  
  mixin innerMixin im;
  alias somefunc = im.someFunc();

  //If you comment out this function, it compiles and work.
  void someFunc( string s )() if( s == "toto" ) {
  
  }
}

Then the code will compile.