Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changeset 1541

Show
Ignore:
Timestamp:
05/23/10 01:07:22 (15 years ago)
Author:
rsinfu
Message:

Fixed covariance check (function safety).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/phobos/std/traits.d

    r1540 r1541  
    15291529    template checkSTC() 
    15301530    { 
    15311531        // Note the order of arguments.  The convertion order Lwr -> Upr is 
    15321532        // correct since Upr should be semantically 'narrower' than Lwr. 
    15331533        enum ok = isStorageClassImplicitlyConvertible!(Lwr, Upr); 
    15341534    } 
    15351535    /* 
    15361536     * Check for function attributes: 
    15371537     *  - require exact match for ref and @property 
    15381538     *  - overrider can add pure and nothrow, but can't remove them 
    1539      *  - trust: ? 
     1539     *  - @safe and @trusted are covariant with each other, unremovable 
    15401540     */ 
    15411541    template checkAttributes() 
    15421542    { 
    15431543        alias FunctionAttribute FA; 
    15441544        enum uprAtts = functionAttributes!(Upr); 
    15451545        enum lwrAtts = functionAttributes!(Lwr); 
    15461546        // 
    15471547        enum WANT_EXACT = FA.REF | FA.PROPERTY; 
    1548         enum TRUST = FA.SAFE | FA.TRUSTED; 
     1548        enum SAFETY = FA.SAFE | FA.TRUSTED; 
    15491549        enum ok = 
    1550             ((uprAtts & WANT_EXACT) == (lwrAtts & WANT_EXACT)) && 
    1551             ((uprAtts & FA.PURE   ) >= (lwrAtts & FA.PURE   )) && 
    1552             ((uprAtts & FA.NOTHROW) >= (lwrAtts & FA.NOTHROW)) && 
    1553             ((uprAtts & TRUST     ) >= (lwrAtts & TRUST     )) ;  // XXX 
     1550            (  (uprAtts & WANT_EXACT) ==  (lwrAtts & WANT_EXACT)) && 
     1551            (  (uprAtts & FA.PURE   ) >=  (lwrAtts & FA.PURE   )) && 
     1552            (  (uprAtts & FA.NOTHROW) >=  (lwrAtts & FA.NOTHROW)) && 
     1553            (!!(uprAtts & SAFETY    ) >= !!(lwrAtts & SAFETY    )) ; 
    15541554    } 
    15551555    /* 
    15561556     * Check for return type: usual implicit convertion. 
    15571557     */ 
    15581558    template checkReturnType() 
    15591559    { 
    15601560        enum ok = is(ReturnType!(Upr) : ReturnType!(Lwr)); 
    15611561    } 
    15621562    /* 
    15631563     * Check for parameters: 
     
    16361636    static assert(! isCovariantWith!(BaseB.test, DerivB_1.test)); 
    16371637    static assert(! isCovariantWith!(BaseB.test, DerivB_2.test)); 
    16381638    static assert(! isCovariantWith!(BaseB.test, DerivB_3.test)); 
    16391639 
    16401640    // function storage class 
    16411641    interface BaseC            {          void test()      ; } 
    16421642    interface DerivC_1 : BaseC { override void test() const; } 
    16431643    static assert(isCovariantWith!(DerivC_1.test, BaseC.test)); 
    16441644    static assert(! isCovariantWith!(BaseC.test, DerivC_1.test)); 
    16451645 
    1646     // trust 
     1646    // increasing safety 
    16471647    interface BaseE            {          void test()         ; } 
    16481648    interface DerivE_1 : BaseE { override void test() @safe   ; } 
    16491649    interface DerivE_2 : BaseE { override void test() @trusted; } 
    16501650    static assert(isCovariantWith!(DerivE_1.test, BaseE.test)); 
    16511651    static assert(isCovariantWith!(DerivE_2.test, BaseE.test)); 
    16521652    static assert(! isCovariantWith!(BaseE.test, DerivE_1.test)); 
    16531653    static assert(! isCovariantWith!(BaseE.test, DerivE_2.test)); 
     1654 
     1655    // @safe and @trusted 
     1656    interface BaseF 
     1657    { 
     1658        void test1() @safe; 
     1659        void test2() @trusted; 
     1660    } 
     1661    interface DerivF : BaseF 
     1662    { 
     1663        override void test1() @trusted; 
     1664        override void test2() @safe; 
     1665    } 
     1666    static assert(isCovariantWith!(DerivF.test1, BaseF.test1)); 
     1667    static assert(isCovariantWith!(DerivF.test2, BaseF.test2)); 
    16541668} 
    16551669 
    16561670 
    16571671//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::// 
    16581672// isSomething 
    16591673//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::// 
    16601674 
    16611675/** 
    16621676 * Detect whether T is a built-in integral type. Types $(D bool), $(D 
    16631677 * char), $(D wchar), and $(D dchar) are not considered integral.