www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - akePureMalloc cannot be interpreted at compile time

reply Adnan <relay.public.adnan outlook.com> writes:
The following program produces an error message and it is not 
clear exactly what line causes this error:

module maybe;

 nogc:
private import std.container : Array;

struct MayBe(T) {
	Array!T data;

	this(T datum) {
		data.reserve(1);
		data.insert(datum);
	}

	T unwrap() const {
		return data[0];
	}

	T unwrapOr(T alternateValue) const {
		return isSome() ? unwrap() : alternateValue;
	}

	bool isSome() const {
		return !data.empty();
	}

	bool isNone() const {
		return !isSome();
	}

	bool contains(T arg) const {
		return isSome() && unwrap() == arg;
	}

	T expect(lazy string msg) const {
		if (isNone()) {
			import core.stdc.stdio : puts;
			import core.stdc.stdlib : exit;

			puts(msg.ptr);
			exit(-1);
		}
		return unwrap();
	}

	void nullify() {
		data.clear();
	}

	void insert(T datum) {
		if (isNone())
			data.insert(datum);
		else
			data.front = datum;
	}
}

MayBe!T flatten(T)(MayBe!(MayBe!T) arg) {
	return isSome() ? arg.unwrap() : some!T();
}

MayBe!T some(T)(T arg) {
	return MayBe!T(arg);
}

MayBe!T none(T)() {
	return MayBe!T();
}

unittest {
	alias MB = MayBe;

	MB!int a;
	assert(a.isNone);

	auto b = none!int();
	assert(b.isNone);

	auto c = some!int(23);
	assert(c.contains(23));

	c.nullify();

	assert(a == c);

	a.insert(11);
	c.insert(11);
	assert(a == c);

	auto d = some!(some!int(9));
	assert(d.flatten() == some!int(9));
}

$ dub test
Generating test runner configuration 'maybe-test-library' for 
'library' (library).
Performing "unittest" build using dmd for x86_64.
maybe ~master: building configuration "maybe-test-library"...
/snap/dmd/99/bin/../import/phobos/std/internal/memory.d(31,33): 
Error: fakePureMalloc cannot be interpreted at compile time, 
because it has no available source code
dmd failed with exit code 1.

And what is the meaning of this error message?
Mar 05 2020
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 5 March 2020 at 20:13:11 UTC, Adnan wrote:
 	auto d = some!(some!int(9));
My guess would be this line is causing your trouble. That is trying to pass an instance as a value at compile time, thus triggering ctfe. Since you use the `Array` object inside and that uses malloc, this call tries to run malloc at compile time triggering the error. It kinda looks like a typo anyway...
Mar 05 2020