D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5251 - Const C file
Summary: Const C file
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2010-11-21 16:31 UTC by bearophile_hugs
Modified: 2010-11-22 10:30 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-11-21 16:31:32 UTC
I think this program is supposed to work, because fprintf() and fclose() don't modify 'fout':


import std.c.stdio: fopen, fclose, fprintf;
void main() {
    const fout = fopen("test.txt", "w");
    fprintf(fout, "%d", 10); // ERR
    fclose(fout); // ERR
}



But DMD 2.050 shows the errors:

test.d(4): Error: function core.stdc.stdio.fprintf (shared(_iobuf)* stream, in const(char*) format,...) is not callable using argument types (const(shared(const(_iobuf))*),string,int)
test.d(4): Error: cannot implicitly convert expression (fout) of type const(shared(const(_iobuf))*) to shared(_iobuf)*
test.d(5): Error: function core.stdc.stdio.fclose (shared(_iobuf)* stream) is not callable using argument types (const(shared(const(_iobuf))*))
test.d(5): Error: cannot implicitly convert expression (fout) of type const(shared(const(_iobuf))*) to shared(_iobuf)*
Comment 1 Steven Schveighoffer 2010-11-22 08:58:52 UTC
(In reply to comment #0)
> I think this program is supposed to work, because fprintf() and fclose() don't
> modify 'fout':

Yes they do.  fprintf and fclose deal with a memory-allocated buffer that will be used to optimize I/O throughput.
Comment 2 bearophile_hugs 2010-11-22 09:34:42 UTC
(In reply to comment #1)

> Yes they do.  fprintf and fclose deal with a memory-allocated buffer that will
> be used to optimize I/O throughput.

If this bug report is invalid then thank you for closing it. I am not expert enough on this.

But I don't understand what you have said. Even if fprintf and fclose deal with a memory-allocated buffer, do they modify the value of 'fout'?
Comment 3 Steven Schveighoffer 2010-11-22 10:11:55 UTC
const is transitive, so if you imagine a FILE having this structure:

struct FILE
{
   int fd;
   ubyte[] buffer;
}

Now, if FILE is const, then buffer is const(ubyte[]), so how does fprintf write to that buffer?

What you are looking for is head-const (the variable cannot change, but everything it points to can), which has some good uses, but does not exist in D.
Comment 4 bearophile_hugs 2010-11-22 10:16:12 UTC
(In reply to comment #3)
> const is transitive, so if you imagine a FILE having this structure:
> 
> struct FILE
> {
>    int fd;
>    ubyte[] buffer;
> }
> 
> Now, if FILE is const, then buffer is const(ubyte[]), so how does fprintf write
> to that buffer?
> 
> What you are looking for is head-const (the variable cannot change, but
> everything it points to can), which has some good uses, but does not exist in
> D.

I understand, you are right, thank you.
Comment 5 nfxjfg 2010-11-22 10:30:04 UTC
(In reply to comment #3)
> What you are looking for is head-const (the variable cannot change, but
> everything it points to can), which has some good uses, but does not exist in
> D.

It exists to some degree in D1:

final int[] a = [1,2];
a[0] = 1;  //works
a = [5,6]; //Error: cannot modify final variable 'a'

Apparently this was disabled in D2.