Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unintuitive behavior when omitting type specifier: APPROX x = 1; #34

Open
billzorn opened this issue Feb 10, 2015 · 7 comments
Open

Unintuitive behavior when omitting type specifier: APPROX x = 1; #34

billzorn opened this issue Feb 10, 2015 · 7 comments
Labels

Comments

@billzorn
Copy link

Explanation in comments.

/*
 * So this clearly shouldn't be allowed. What we observe is that the program
 * returns 0, not 10000 as we might expect it to, even on precise runs.
 */

#include <enerc.h>

int main() {

  int x = 0;
  for(int i = 0; i < 1000; ++i) {
    // huh?
    APPROX x = x + 10;
    /*
     * I *think* it's declaring a new variable.
     *
     * We do get a compiler warning about this:
     *
     * main.c:14:12: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
     *     APPROX x = x + 10;
     *     ~~~~~~ ^
     *
     * Writing "APPROX int x = x + 10;" instead produces the same behavior
     * but no warning.
     *
     * I suppose this is actually working as intended, but it has some very
     * surprising consequences for cetain typos. I was not aware that you
     * were allowed to redefine variables like this inside a loop, but accept
     * doesn't complain about it even without any annotations. Nor does gcc.
     */    
  }

  return x;
}
@andreolb
Copy link
Contributor

I don't understand what "clearly shouldn't be allowed".

Declaring another variable inside the loop scope is allowed in C++.

And the "x" the return statement refers to is the one outside the loop. The
loop itself touches the redefined "x" which doesn't exist after the loop
anymore. So I expect this program to return 0.

On Monday, February 9, 2015, Bill [email protected] wrote:

Explanation in comments.

/* * So this clearly shouldn't be allowed. What we observe is that the program * returns 0, not 10000 as we might expect it to, even on precise runs. */

#include <enerc.h>
int main() {

int x = 0;
for(int i = 0; i < 1000; ++i) {
// huh?
APPROX x = x + 10;
/* * I think it's declaring a new variable. * * We do get a compiler warning about this: * * main.c:14:12: warning: type specifier missing, defaults to 'int' [-Wimplicit-int] * APPROX x = x + 10; * ~~~~~~ ^ * * Writing "APPROX int x = x + 10;" instead produces the same behavior * but no warning. * * I suppose this is actually working as intended, but it has some very * surprising consequences for cetain typos. I was not aware that you * were allowed to redefine variables like this inside a loop, but accept * doesn't complain about it even without any annotations. Nor does gcc. */
}

return x;
}


Reply to this email directly or view it on GitHub
#34.

André Oliveira

@sampsyo
Copy link
Member

sampsyo commented Feb 10, 2015

@andreolb It's the APPROX x = ... that should be illegal. That's an assignment, not a declaration. APPROX int x = ... is fine, but without the type it should be an error.

@andreolb
Copy link
Contributor

Right, I understand now. Thanks!

On Mon, Feb 9, 2015 at 10:44 PM, Adrian Sampson [email protected]
wrote:

@andreolb https://github.com/andreolb It's the APPROX x = ... that
should be illegal. That's an assignment, not a declaration. APPROX int x
= ... is fine, but without the type it should be an error.


Reply to this email directly or view it on GitHub
#34 (comment).

André Oliveira

@andreolb
Copy link
Contributor

Actually, thinking more carefully about it, maybe it's just fine (tell me
if you think I'm trying to turn a bug into a feature :) :

ACCEPT just recognizes statements that starts with "APPROX" as a
declaration, which is correct. And in this case, because the type is
missing in a declaration, the C compiler assumes it's an implicit "int".

On Mon, Feb 9, 2015 at 10:47 PM, Andre Oliveira [email protected] wrote:

Right, I understand now. Thanks!

On Mon, Feb 9, 2015 at 10:44 PM, Adrian Sampson [email protected]
wrote:

@andreolb https://github.com/andreolb It's the APPROX x = ... that
should be illegal. That's an assignment, not a declaration. APPROX int x
= ... is fine, but without the type it should be an error.


Reply to this email directly or view it on GitHub
#34 (comment).

André Oliveira

André Oliveira

@sampsyo
Copy link
Member

sampsyo commented Feb 10, 2015

Yeah, I guess it does—the error message does say that. I mostly just wish the error said "did you mean to leave off APPROX?" instead of "did you mean to insert int?" which seems less likely.

@andreolb
Copy link
Contributor

I agree, but maybe the misunderstanding is due to the C compiler, not to
ACCEPT. I say this because when I see code like

foo(int a) {
....
}

I think it's much more likely that the programmer forgot to specify the
return type than that he wanted an int.

On Mon, Feb 9, 2015 at 10:59 PM, Adrian Sampson [email protected]
wrote:

Yeah, I guess it does—the error message does say that. I mostly just wish
the error said "did you mean to leave off APPROX?" instead of "did you mean
to insert int?" which seems less likely.


Reply to this email directly or view it on GitHub
#34 (comment).

André Oliveira

@billzorn
Copy link
Author

Based on my experience, this wouldn't be a problem if the warning were more visible to the user.

In practice, it's highly likely that whatever warning is generated will blend in with all the other warnings that the APPROX notation tends to cause. I first hit this feature while annotating the perfect benchmarks, and had no idea it even produced a warning until I tried to reproduce it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants