Bug 8263

Summary: op_Explicit in literal conversion
Product: [Mono] Compilers Reporter: Erik Källén <erik_kallen>
Component: C#Assignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: erik_kallen, mono-bugs+mono
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Custom mscorlib

Description Erik Källén 2012-11-07 11:27:10 UTC
I'm using a custom mscorlib which contains (among other things)

struct System.Char {
    public static explicit operator string(char ch) {
        return null;
    }
}

This means that the following code compiles:

void M() {
    char c = 'c';
    string s = (string)c;
}

but this code fails to compile:

void M() {
    string s = (string)'c';
}

MS's compiler does successfully compile the second case.
Comment 1 Erik Källén 2012-11-07 12:39:25 UTC
Apparantly I was wrong, the first example does not compile either. Both examples compiles with MS's compiler.
Comment 2 Erik Källén 2012-11-07 13:00:36 UTC
The whole issue seems to be because of a hack in MemberCache.AddMember. I managed to hack around it in my case, so don't feel obligated to fix the problem because of me.
Comment 3 Marek Safar 2012-11-08 08:59:22 UTC
Could I download your mscorlib from somewhere (or you could attach to the bug report). It'd help me to reproduce the issue.
Comment 4 Erik Källén 2012-11-08 09:03:14 UTC
Created attachment 2884 [details]
Custom mscorlib
Comment 5 Erik Källén 2012-11-08 09:07:21 UTC
Btw, the problematic code is at line 300 in membercache.cs, clearly commenting why it does what it does.
Comment 6 Erik Källén 2012-11-11 09:12:11 UTC
I just saw the very hacky solution on github, and I just want to say that you don't need to do it for me. I have already solved my problem.
Comment 7 Marek Safar 2012-11-11 12:49:31 UTC
Fixed in master. The standard is not very clear on char predefined operators. I decided to allowed them to be compatible with csc.
Comment 8 Erik Källén 2012-11-11 15:20:41 UTC
FYI, there also exist other similar issues.

Eg:

class Delegate {
    ...
    public static explicit operator Delegate(MyClass c) { // This operator will not be used
    }
}

class string {
    ...
    public static explicit operator string(MyClass c) { // This operator will not be used
    }
}