Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.
This repository was archived by the owner on Jul 15, 2023. It is now read-only.

Contract.Require(x != null) gets translated in "!(x <= null)" in the documentation rewritten by CC #307

@ndepend

Description

@ndepend

We have a lot of require not null contracts and I noticed that the contract formatting in the XML doc that used to be csharp="XYZ != null" is now csharp="!(XYZ <= null)". If I look in the xml generated I get:

// XML Generated Now     csharp="!(initialSeq <= null)"
<requires description="initialSeq must not be null" csharp="!(initialSeq &lt;= null)" vb="!(initialSeq &lt;= Nothing)">!(initialSeq &lt;= null)</requires>

// XML Generated Before  csharp="initialSeq != null"
<requires description="initialSeq must not be null" csharp="initialSeq != null" vb="initialSeq &lt;&gt; Nothing">initialSeq != null</requires>

Here is the contract declaration in the C# code:

// Contract declaration in the C# code
Contract.Requires(initialSeq != null, "initialSeq must not be null");

Here is the IL emitted from VS2015 (Update 1 RC) after being rewritten with 1.9.1:

IL_0000: ldarg.0
IL_0001: ldnull
IL_0002: cgt.un
IL_0004: ldstr "initialSeq must not be null"
IL_0009: ldstr "initialSeq != null"
IL_000e: call void System.Diagnostics.Contracts.__ContractsRuntime::Requires(bool, string, string)
IL_0013: nop
IL_0014: nop
IL_0015: br IL_001a

Here is the IL when compiled with VS2013 + Code Contract 1.7 if it can help:

L_0000: ldarg.0 
L_0001: ldnull 
L_0002: ceq 
L_0004: ldc.i4.0 
L_0005: ceq 
L_0007: ldstr "initialSeq must not be null"
L_000c: ldstr "initialSeq != null"
L_0011: call void System.Diagnostics.Contracts.__ContractsRuntime::Requires(bool, string, string)
L_0016: nop 
L_0017: ldarg.1 
L_0018: ldnull 
L_0019: ceq 
L_001b: ldc.i4.0 
L_001c: ceq

This is likely cause by Roslyn - Roslyn compiler emits x != null as x > null, whereas the old <= VS2013 compiler emitted it as (x == null) == 0 as you can see from the VS2013 IL above.

ccdocgen likely needs to be updated to understand that x > null also means x != null.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions