Bugzilla – Bug 321329
[PATCH] Error CS0120 often reports only last part of method signature
Last modified: 2007-09-15 21:24:23 UTC
---- Reported by juraj@hotfeet.ch 2006-06-07 10:40:46 MST ---- The error message resulting from compiling the code below is test.cs(5,3): error CS0120: `StringBuilder)': An object reference is required for the nonstatic field, method or property Clearly the quoted method signature has been truncated. ====================== using System.Text; class X { static void Main () { X.Y(null); } void Y(StringBuilder someParam) { } } ====================== The problem is the following statement in SimpleName.Error_ObjectRefRequired (...) (in ecore.cs): if (name.LastIndexOf ('.') > 0) name = name.Substring (name.LastIndexOf ('.') + 1); This cuts away anything before the last dot. ---- Additional Comments From juraj@hotfeet.ch 2006-06-07 11:00:44 MST ---- MS.NET reports the class name as well. For the code above it quotes `X.Y(System.Text.StringBuilder)'. Therefore just getting rid of the cut-away lines seems to be the right thing to do. ---- Additional Comments From juraj@hotfeet.ch 2006-06-07 11:01:37 MST ---- Created an attachment (id=169965) Proposed patch ---- Additional Comments From rharinath@novell.com 2006-06-08 05:38:03 MST ---- Hmm, while this fixes the problem for this case, we need to handle the short-name case for other callsites. A grep shows the callsites as ------------------------------------ ecore.cs:2050: Error_ObjectRefRequired (ec, loc, me.GetSignatureForError ()); ecore.cs:2446: SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ()); ecore.cs:2472: SimpleName.Error_ObjectRefRequired (ec, loc, Name); ecore.cs:2780: SimpleName.Error_ObjectRefRequired (ec, loc, FieldInfo.Name); ecore.cs:3268: SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name); ecore.cs:3601: SimpleName.Error_ObjectRefRequired (ec, loc, EventInfo.Name); expression.cs:4740: SimpleName.Error_ObjectRefRequired (ec, loc, TypeManager.CSharpSignature (mi)); expression.cs:5016: SimpleName.Error_ObjectRefRequired (ec, loc, TypeManager.CSharpSignature (method)); ----------------------------------- Note the cases where .Name is used. I think those cases have to be fixed to pass short names. If you're interested in fixing the issue, please assign the bug to yourself (Cc:ing me). ---- Additional Comments From juraj@hotfeet.ch 2006-06-15 05:25:14 MST ---- Sorry, didn't see your comment until now. I'll look at the other callsites. ---- Additional Comments From juraj@hotfeet.ch 2006-06-15 06:06:55 MST ---- Hmm, I've only found one single test case which reaches one of the other callsites : =========== using System.Collections; public class Test { ArrayList ArrayList; public static void Main () { ArrayList.Capacity = 5; } } ============ Output from mcs: ---------------- error CS0120: `ArrayList': An object reference is required for the nonstatic field, method or property Output from csc: ---------------- error CS0120: An object reference is required for the nonstatic field, method, or property 'System.Collections.ArrayList.Capacity' I would except the compiler to either tell me that "System.Collections.ArrayList.Capacity" (as csc does) or that "Test.ArrayList" is the field needing an object reference. So my questions now are: - What do we want mcs to report for the test case above? - How do I reach the other callsites? - When and why do we want to report the short name? ---- Additional Comments From rharinath@novell.com 2006-06-16 07:38:12 MST ---- Ok. Looks good. You may want to use GetSignatureForError on Property names too. Please apply your patch. ---- Additional Comments From juraj@hotfeet.ch 2006-06-16 08:18:31 MST ---- The fix is in svn. Additionally, I've made the code more consistent by always letting GetSignatureForError () construct the name. ---- Additional Comments From rharinath@novell.com 2006-06-16 08:37:47 MST ---- Thanks. There were several undocumented steps that I forgot to mention :-) * merge into gmcs/ * add a regression testcase * handle any regressions I've taken care of that. Imported an attachment (id=169965) Unknown bug field "cf_op_sys_details" encountered while moving bug <cf_op_sys_details>FC5</cf_op_sys_details> Unknown operating system unknown. Setting to default OS "Other".