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

compiler bug : string parameter misbehaves within for loop #396

Closed
gopherbot opened this issue Dec 8, 2009 · 5 comments
Closed

compiler bug : string parameter misbehaves within for loop #396

gopherbot opened this issue Dec 8, 2009 · 5 comments

Comments

@gopherbot
Copy link
Contributor

by kaushansky:

The following program doesn't compile, complains about unknown type 

func iterateFast(s string) int { 
  acc:=0; 
  for i:=0;i<10;i++ { 
    acc+=len(s); 
  } 
  return acc;  // fatal error: unknown etype 0/STRING 
} 

However, this variant compiles with no errors:

func iterateFast(s string) int { 
  s1:=s; 
  acc:=0; 
  for i:=0;i<10;i++ { 
    acc+=len(s1); 
  } 
  return acc;  // no problem
} 

I tried to localize the bug by removing for loop, but the following compiles just fine:
func iterateFast(s string) int { 
  acc:=0; 
  
  acc+=len(s); 
   
  return acc;  // no problem
}
@gopherbot
Copy link
Contributor Author

Comment 1 by pshah.foss:

src/cmd/8g/reg.c line 1047

@gopherbot
Copy link
Contributor Author

Comment 2 by kenliz@cruzio.com:

How is iterateFast being called? 
The zero value for string is supposed to be the empty string. But this looks like a real
0. Is there a pointer 
dereference involved? or a strange type cast?

@gopherbot
Copy link
Contributor Author

Comment 3 by kaushansky:

But this is compiler error, not runtime error. It shows up even if no
one calls the function.
To eliminate a chance that inlining gets somehow involved, I compiled
this function by itself,  with the same result. Do you have problem reproducing it?

@robpike
Copy link
Contributor

robpike commented Dec 9, 2009

Comment 4:

Labels changed: added compilerbug.

Owner changed to k...@golang.org.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Dec 14, 2009

Comment 5:

This was fixed by 683ed10f78 (8g) and d9cbc65f0f (6g).

Status changed to Fixed.

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants