public IErrorReporter ErrorUsingMutablesOnNeutral() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Language.Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(TypeBuilder.Create("Elka") .SetModifier(EntityModifier.Mutable) .With(PropertyBuilder.CreateAutoFull(env.Options, "numi", NameFactory.Int64NameReference(), null)) .With(FunctionBuilder.Create("mutator", NameFactory.UnitNameReference(), Block.CreateStatement()) .SetModifier(EntityModifier.Mutable))); FunctionCall call = FunctionCall.Create(NameReference.Create("x", NameFactory.MutableName("mutator"))); IExpression assignment = Assignment.CreateStatement(NameReference.Create("x", "numi"), Int64Literal.Create("5")); root_ns.AddBuilder(FunctionBuilder.Create("foo", NameFactory.UnitNameReference(), Block.CreateStatement( // we can assign both mutable and immutable to neutral VariableDeclaration.CreateStatement("x", NameFactory.PointerNameReference(NameReference.Create(TypeMutability.ReadOnly, "Elka")), ExpressionFactory.HeapConstructor(NameReference.Create("Elka"))), call, assignment ))); resolver = NameResolver.Create(env); Assert.AreEqual(2, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AlteringNonMutableInstance, call)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AlteringNonMutableInstance, assignment)); } return(resolver); }
public IErrorReporter ErrorInOutVarianceProperties() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Language.Environment.Create(new Options() { DiscardingAnyExpressionDuringTests = true } .SetMutability(mutability)); var root_ns = env.Root; NameReference prop_a_typename = NameReference.Create("TA"); NameReference prop_b_typename = NameReference.Create("TB"); root_ns.AddBuilder(TypeBuilder.Create( NameDefinition.Create(NameFactory.TupleTypeName, TemplateParametersBuffer.Create().Add("TA", VarianceMode.In).Add("TB", VarianceMode.Out).Values)) .SetModifier(EntityModifier.Mutable) .With(ExpressionFactory.BasicConstructor(new[] { "adata", "bdata" }, new[] { NameReference.Create("TA"), NameReference.Create("TB") })) .With(PropertyBuilder.CreateAutoFull(env.Options, "adata", prop_a_typename, Undef.Create())) .With(PropertyBuilder.CreateAutoFull(env.Options, "bdata", prop_b_typename, Undef.Create()))); resolver = NameResolver.Create(env); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.VarianceForbiddenPosition, prop_a_typename)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.VarianceForbiddenPosition, prop_b_typename)); Assert.AreEqual(2, resolver.ErrorManager.Errors.Count); } return(resolver); }