Better check for instance equality of generic types. Fixes #15100
[mono.git] / mcs / mcs / complete.cs
index 191c8cd5ae2ba15e44da46e5661a027b3e730541..6eb42cddada0a51de2f96f54fe9c35b0b6cd294e 100644 (file)
@@ -7,16 +7,14 @@
 //
 // Copyright 2001, 2002, 2003 Ximian, Inc.
 // Copyright 2003-2009 Novell, Inc.
+// Copyright 2011 Xamarin Inc
 //
 // Completion* classes derive from ExpressionStatement as this allows
 // them to pass through the parser in many conditions that require
 // statements even when the expression is incomplete (for example
 // completing inside a lambda
 //
-using System;
 using System.Collections.Generic;
-using System.Text;
-using Mono.CSharp.Linq;
 using System.Linq;
 
 namespace Mono.CSharp {
@@ -46,19 +44,24 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override void EmitStatement (EmitContext ec)
+               public override bool ContainsEmitWithAwait ()
                {
-                       // Do nothing
+                       return false;
                }
 
-               public override void Emit (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
+               {
+                       return null;
+               }
+
+               public override void EmitStatement (EmitContext ec)
                {
                        // Do nothing
                }
 
-               public override Expression CreateExpressionTree (ResolveContext ec)
+               public override void Emit (EmitContext ec)
                {
-                       return null;
+                       // Do nothing
                }
        }
        
@@ -75,11 +78,9 @@ namespace Mono.CSharp {
                {
                        var results = new List<string> ();
 
-                       AppendResults (results, Prefix, Evaluator.GetVarNames ());
-                       AppendResults (results, Prefix, ec.CurrentMemberDefinition.Parent.NamespaceEntry.CompletionGetTypesStartingWith (Prefix));
-                       AppendResults (results, Prefix, Evaluator.GetUsingList ());
-                       
-                       throw new CompletionResult (Prefix, results.ToArray ());
+                       ec.CurrentMemberDefinition.GetCompletionStartingWith (Prefix, results);
+
+                       throw new CompletionResult (Prefix, results.Distinct ().Select (l => l.Substring (Prefix.Length)).ToArray ());
                }
 
                protected override void CloneTo (CloneContext clonectx, Expression t)
@@ -108,28 +109,56 @@ namespace Mono.CSharp {
                        this.targs = targs;
                }
                
-               protected override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext rc)
                {
-                       Expression expr_resolved = expr.Resolve (ec,
-                               ResolveFlags.VariableOrValue | ResolveFlags.Type);
+                       var sn = expr as SimpleName;
+                       const ResolveFlags flags = ResolveFlags.VariableOrValue | ResolveFlags.Type;
+
+                       //
+                       // Resolve the expression with flow analysis turned off, we'll do the definite
+                       // assignment checks later.  This is because we don't know yet what the expression
+                       // will resolve to - it may resolve to a FieldExpr and in this case we must do the
+                       // definite assignment check on the actual field and not on the whole struct.
+                       //
+                       using (rc.Set (ResolveContext.Options.OmitStructFlowAnalysis)) {
+                               if (sn != null) {
+                                       expr = sn.LookupNameExpression (rc, MemberLookupRestrictions.ReadAccess | MemberLookupRestrictions.ExactArity);
+
+                                       //
+                                       // Resolve expression which does have type set as we need expression type
+                                       // with disable flow analysis as we don't know whether left side expression
+                                       // is used as variable or type
+                                       //
+                                       if (expr is VariableReference || expr is ConstantExpr || expr is Linq.TransparentMemberAccess) {
+                                               using (rc.With (ResolveContext.Options.DoFlowAnalysis, false)) {
+                                                       expr = expr.Resolve (rc);
+                                               }
+                                       } else if (expr is TypeParameterExpr) {
+                                               expr.Error_UnexpectedKind (rc, flags, sn.Location);
+                                               expr = null;
+                                       }
+                               } else {
+                                       expr = expr.Resolve (rc, flags);
+                               }
+                       }
 
-                       if (expr_resolved == null)
+                       if (expr == null)
                                return null;
 
-                       TypeSpec expr_type = expr_resolved.Type;
-                       if (expr_type.IsPointer || expr_type == TypeManager.void_type || expr_type == InternalType.Null || expr_type == InternalType.AnonymousMethod) {
-                               Unary.Error_OperatorCannotBeApplied (ec, loc, ".", expr_type);
+                       TypeSpec expr_type = expr.Type;
+                       if (expr_type.IsPointer || expr_type.Kind == MemberKind.Void || expr_type == InternalType.NullLiteral || expr_type == InternalType.AnonymousMethod) {
+                               expr.Error_OperatorCannotBeApplied (rc, loc, ".", expr_type);
                                return null;
                        }
 
                        if (targs != null) {
-                               if (!targs.Resolve (ec))
+                               if (!targs.Resolve (rc))
                                        return null;
                        }
 
                        var results = new List<string> ();
-                       if (expr_resolved is Namespace){
-                               Namespace nexpr = expr_resolved as Namespace;
+                       if (expr is Namespace) {
+                               Namespace nexpr = expr as Namespace;
                                string namespaced_partial;
 
                                if (partial_name == null)
@@ -137,19 +166,11 @@ namespace Mono.CSharp {
                                else
                                        namespaced_partial = nexpr.Name + "." + partial_name;
 
-#if false
-                               Console.WriteLine ("Workign with: namespaced partial {0}", namespaced_partial);
-                               foreach (var x in ec.TypeContainer.NamespaceEntry.CompletionGetTypesStartingWith (ec.TypeContainer, namespaced_partial)){
-                                       Console.WriteLine ("    {0}", x);
-                               }
-#endif
-
-                               CompletionSimpleName.AppendResults (
-                                       results,
-                                       partial_name, 
-                                       ec.CurrentMemberDefinition.Parent.NamespaceEntry.CompletionGetTypesStartingWith (namespaced_partial));
+                               rc.CurrentMemberDefinition.GetCompletionStartingWith (namespaced_partial, results);
+                               if (partial_name != null)
+                                       results = results.Select (l => l.Substring (partial_name.Length)).ToList ();
                        } else {
-                               var r = MemberCache.GetCompletitionMembers (expr_type, partial_name).Select (l => l.Name);
+                               var r = MemberCache.GetCompletitionMembers (rc, expr_type, partial_name).Select (l => l.Name);
                                AppendResults (results, partial_name, r);
                        }
 
@@ -178,7 +199,7 @@ namespace Mono.CSharp {
                
                protected override Expression DoResolve (ResolveContext ec)
                {
-                       var members = MemberCache.GetCompletitionMembers (ec.CurrentInitializerVariable.Type, partial_name);
+                       var members = MemberCache.GetCompletitionMembers (ec, ec.CurrentInitializerVariable.Type, partial_name);
 
 // TODO: Does this mean exact match only ?
 //                     if (partial_name != null && results.Count > 0 && result [0] == "")