Jump to page: 1 2
Thread overview
[Issue 3882] New: Unused result of pure functions
Jun 26, 2011
Walter Bright
Jan 05, 2012
Trass3r
Jan 05, 2012
Trass3r
March 05, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3882

           Summary: Unused result of pure functions
           Product: D
           Version: 2.040
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-03-05 10:31:12 PST ---
The following small program:

void main() {
    int x = 5;
    x + 3;
}

Currently produces the compile error:
Error: + has no effect in expression (x + 3)
because that's often a small bug in the program.

Equally, a pure function has no side effects, so its only purpose is to return a value. So the compiler can issue a warning or error if its return value is not used. This can help avoid some small bugs.

This predecessor() function computes the predecessor of its input argument, its result is always nonnegative:


pure int predecessor(int x) {
    if (x <= 0)
        throw new Exception("");
    return x - 1;
}
void main() {
    predecessor(5); // warning or error
}


In theory a pure function can be used just for the exceptions it throws, but I think this is a quite uncommon usage.

A "pure nothrow" function can't even have this uncommon purpose, so this can produce a compile error:

pure nothrow int predecessor(int x) {
    return x - 1;
}
void main() {
    predecessor(5); // error
}

--------------

In GCC there is a function attribute named "warn_unused_result": http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

The warn_unused_result attribute causes a warning to be emitted if a caller of the function with this attribute does not use its return value. This is useful for functions where not checking the result is either a security problem or always a bug, such as realloc.

Similar attribute can be named like @do_use_result in D2.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 28, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3882



--- Comment #1 from bearophile_hugs@eml.cc 2011-02-28 04:21:28 PST ---
See also bug 5464 , that contains and expands just the second part of this enhancement request (because putting two different requests in a bugzilla issue is bad).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 27, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3882



--- Comment #2 from bearophile_hugs@eml.cc 2011-03-27 07:29:53 PDT ---
If the druntime function to allocate a new array is a pure function, then this enhancement request catches a bug like this too:

{
    new int[10];
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 27, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3882



--- Comment #3 from bearophile_hugs@eml.cc 2011-03-27 08:39:01 PDT ---
KennyTM~ reminds that this is error message is only for "strongly pure" functions.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 27, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3882



--- Comment #4 from bearophile_hugs@eml.cc 2011-03-27 13:21:22 PDT ---
Walter seems to appreciate the idea:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=132932

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 26, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3882


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


--- Comment #5 from Walter Bright <bugzilla@digitalmars.com> 2011-06-25 20:44:39 PDT ---
https://github.com/D-Programming-Language/dmd/commit/9b50184ca4e66a4b878468b7eb0cb21839684c81

I added this as a warning because it's fairly onerous now that we have implicit purity deduction.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 26, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3882



--- Comment #6 from bearophile_hugs@eml.cc 2011-06-25 23:14:11 PDT ---
(In reply to comment #5)

> I added this as a warning because it's fairly onerous now that we have implicit purity deduction.

Thank you very much Walter. DMD 2.054 is becoming an interesting release.

In few months the warning will allow us to see _how much_ onerous a similar error is.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 27, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3882


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|FIXED                       |WONTFIX


--- Comment #7 from bearophile_hugs@eml.cc 2011-06-27 12:51:57 PDT ---
I change this to WONTFIX because practice has already shown this change is not currently practical. More thinking is needed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 05, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=3882


Trass3r <mrmocool@gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mrmocool@gmx.de


--- Comment #8 from Trass3r <mrmocool@gmx.de> 2012-01-05 14:38:57 PST ---
*** Issue 7235 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 05, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=3882


Trass3r <mrmocool@gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|WONTFIX                     |LATER


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2