Bug 1698 – foreach auto type inference doesnt work properly
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2007-11-29T07:03:00Z
Last change time
2015-06-09T01:14:22Z
Keywords
rejects-valid
Assigned to
bugzilla
Creator
spam
Comments
Comment #0 by spam — 2007-11-29T07:03:12Z
since the const/invariant changes in dmd2.008 the following valid code wont compile anymore, cause the compile is not able to infer the type for c correctly i think.
[CODE]
void bar(ref char c) {
}
void main() {
string text;
foreach(ref c; text) { // adding type char makes it work
bar(c);
}
}
[\CODE]
compiler message:
Error: cast(char)c is not an lvalue
Comment #1 by ludwig — 2007-12-01T11:25:17Z
[email protected] wrote:
> http://d.puremagic.com/issues/show_bug.cgi?id=1698
>
> Summary: foreach auto type inference doesnt work properly
> Product: D
> Version: 2.008
> Platform: PC
> OS/Version: Windows
> Status: NEW
> Keywords: rejects-valid
> Severity: normal
> Priority: P2
> Component: DMD
> AssignedTo: [email protected]
> ReportedBy: [email protected]
>
>
> since the const/invariant changes in dmd2.008 the following valid code wont
> compile anymore, cause the compile is not able to infer the type for c
> correctly i think.
>
> [CODE]
> void bar(ref char c) {
>
> }
>
> void main() {
>
> string text;
>
> foreach(ref c; text) { // adding type char makes it work
>
> bar(c);
> }
> }
> [\CODE]
>
> compiler message:
> Error: cast(char)c is not an lvalue
>
>
Since c inside of the foreach-loop is actually a "ref invariant(char)"
(string = invariant(char)[]), it is correct that it cannot be passed to
bar, taking a mutable "ref char".
Changing bar to "void bar( ref invariant(char) c ){}" makes it compile
again.