Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug IDE/Compiler bug?

Discussion in 'Scripting' started by ArachnidAnimal, May 12, 2024.

  1. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    I had a failed compile of my code which was caused by attempting to access the z component of a Vector2, which isn't defined in a Vector2.
    Finding the issue was made more difficult due to an apparent IDE/Compiler bug when an extension method named "z" exists somewhere.
    (Note: I'm still using Unity 2017.4).

    Just to simplify what happened:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public static class Extensions
    5. {
    6.     public static int z (this Component c)
    7.     {
    8.         return 0;
    9.     }
    10. }
    11.  
    12. public class CompilerBug : MonoBehaviour
    13. {
    14.     void Start ()
    15.     {
    16.         var x = 0 + Vector2.one.z;
    17.     }
    18. }
    19.  
    1. Surprising, the IDE (MonoDevelop) doesn't complain about anything here. It says x is an int. Nothing is flagged as wrong here.

    2. The compiler gives the following error: CompilerBug.cs(15,11): error CS0019: Operator `+' cannot be applied to operands of type `int' and `method group'

    Now, I would think the actual error would be error CS1061: Type `UnityEngine.Vector2' does not contain a definition for `z'. Had it given this error instead, I would have found the issue right away.

    Why is both the IDE/Compiler attempting to do anything regarding the Component extension method "z" on a Vector2? Vector2 is not even a component.

    This is quite strange. To me this seems like a bug. I just thought I would share the story here.
     
    Last edited: May 12, 2024
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,906
    Right. But MonoDevelop isn't an IDE. I was so glad to be able to ditch this once Unity supported Visual Studio.

    You could try open the solution in Visual Studio to see whether it provides a different perspective.

    About the only thing I could imagine that leads to this bug is if you have "one" as a extension method of Vector2 that returns a Component.
     
  3. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    Not sure what you mean. The MonoDevelop site describes it as "A cross platform IDE for C#"

    Tried this out. MonoDevelop and compiler knows to use Vector2.one defined in the Vector2 struct, instead of extension method "one".
     
  4. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    830
    The behavior isn’t specific to Unity. Recent versions of the compiler continue to do stuff like this.
    https://sharplab.io/#v2:CYLg1APgAgz...IPLHBOGEZ2QCWAHYxquSiABYl7qiMlAWhAOzGKW5OQA==
    It’s some artifact of the extension method resolution process. Maybe it just collects all the extension methods in scope without being concerned with the type at that point in time.
    Might be good to file a bug with the Roslyn people if you deem this worth that.
     
    Bunny83 likes this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,183
    Well, this may have something to do with how the compiler applies extension methods. Extension methods are not part of the type they are extending. So when an unknown identifier is encountered, the compiler searches for potential extensions. It found one but the first thing it noticed, since you didn't actually execute the method, is that since "x" is a method, your addition doesn't work with a method(group) / delegate. This syntactical check seemingly kicked in before any further type checks are carried out and it's actually correct. You will notice when you actually do

    Code (CSharp):
    1. Vector2.one.x();
    Which gets you past your error in the + operator semantic, you will get an error that tells you:
    So I would highly recommend to not write extension methods with such short names that have name collisions all over the place. In the end you made a mistake and currently you are complaining that the compiler doesn't tell you exactly what you should or shouldn't do. The compiler doesn't know what you actually want to do and always follows a certain order when it comes to errors. So resolving the addition requires evaluating each operands. Vector2 doesn't have a member of type int called z so it looks for extension methods, it has found a matching name but realised that since you directly reference the method and therefore you get a method group, the addition can not be done.

    Sure, compilers could try to look further ahead to potentially catch such issues and display a different error. However in the end it's still an error that can not be resolved by the compiler.

    We will run into much more issues once we get actual extension properties.

    Yes MonoDevelop is an IDE, but a crappy one. Especially the hinting when you have syntax errors always was a pain as a single syntax error would usually completely stop the IDE recognising any other variable, method, identifier as long as the syntax error is there. The intellisense of MD is also known for crashing quite often. I haven't used it in years. Maybe it got a bit better, but it can't really keep up with VS.
     
    Spy-Master likes this.
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,183
    Yes, extension methods only kick in when the compiler hits an undefined identifier. Extension methods can never override an actually existing thing. Though you can define an extension method that has the same name as a field since it syntactically is different as you have to "invoke" the method. Though if that field is a delegate type, an extension method could not override the behaviour because, again, extension methods only kick in when you would get an error otherwise.
     
    Spy-Master likes this.
  7. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    This isn't my extension method. It's from a 3rd party asset.
    I don't know why you're getting all worked up over my post.
    Spy-Master suggested it might be a bug, and you liked his post. But yet you're saying I "made a mistake and am complaining". So I'm confused.
     
    Last edited: May 12, 2024
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,183
    Yes, you made a mistake. A Vector2 obviously does not have a z component. So why did you try to use it? You essentially complained about that the error message wasn't helpful. That doesn't change your mistake in the first place. A Vector2 has 2 components: x and y.
    It certainly isn't a "bug". You could say that the behaviour could be more helpful to point out the mistake you made, though that's certainly not a bug. Broken code is broken code. In the past we had almost no useful error messages and the compiler just said: "Syntax error" and it was your job to figure out what you did wrong. Of course it would be great when the compiler can guide you to the most probable solution, but that's not always possible.

    If that extension method is not your own, I would be careful with such 3rd party assets. Extension methods are great, but single letter extension methods should be avoided, especially lower case.
     
    Spy-Master likes this.
  9. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    How is the IDE not flagging the line of code as an error not some "bug" in the IDE? The code doesn't compile. The IDE suggests it would. That's a bug.
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,381
    Remember, Unity is the compiler and your IDE is just a very fancy text-editor in the context of Unity. What your IDE and what Unity's compiler says are two separate things. If Monodevelop doesn't show a compiler error where there should be, that's something to take up with Monodevelop.
     
    Spy-Master and ArachnidAnimal like this.
  11. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    Oh, so it IS a bug.
     
  12. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,381
    Sure it's a bug with a janky IDE like Monodevelop. Visual Studio wouldn't have this issue.
     
    Spy-Master and ArachnidAnimal like this.
  13. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    830
    Bunny was probably referring to the compiler failing to compile the bugged code. The editor failing to detect problematic code is a separate issue, not what Bunny (probably) was referring to.

    I would say that the more useful error messages are, the better, especially in this case where at a surface level it should be obvious that the extension methods present shouldn’t be applicable. I think it’s appropriate to call the behavior of the compiler a bug. That again is something to take up with the people working on the compiler.
     
    Bunny83 and ArachnidAnimal like this.
  14. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,183
    Sorry, I missed the fact that MD doesn't recognise it as an error at all. Well, you named everything in your example as if it's a compiler bug, which it isn't. Yes when MD does not see it as an error at all, yes that would be one of multiple errors and problems that MD has. I think we already pointed out that MD is a horrible IDE :) Sorry for not realising that MD didn't report an error.

    ps: In the very distant past I also used MD for a while (at the time it was shipped with Unity). You should never use the refactoring feature in MD as it is horrible and more like a slightly beefed up search&replace. It can fail in too many situations and as a result it's unreliable. It often renames stuff that isn't even related to the type. Maybe that has improved as well over time, but I had very bad experiences with it.
     
    Spy-Master likes this.