Footguns on stdlib renamings (`containsAtLeastScalar`)

I just found a small footgun while working on updating my codebase to 0.17.0, and I thought I should make a post about it to save others from any trouble.
Luckily, while doing the migration I decided to use search across all files to change it everywhere quickly, which allowed me to find it before it caused any issues.

Consider the following code:

const std = @import("std");

pub fn main() void {
	const data: []const u8 = ":";
	std.log.info("{}", .{std.mem.containsAtLeastScalar(u8, data, 1, ':')});
}

In compiles in both 0.16.0 and 0.17.0, yet in 0.16.0 it prints true, and in 0.17.0 it prints false.
This is because the signature was changed, the last two parameters were swapped, and since both values here are numbers that fit into a u8, the compiler just silently accepts it.

I wonder if it’s worth adding an additional step to such renamings where the change wouldn’t cause compilation errors in all cases.

6 Likes

In 0.16.0 containsAtLeastScalar was already deprecated in favor of containsAtLeastScalar2. In the 0.17.0 release, the deprecated containsAtLeastScalar was removed and containsAtLeastScalar2 was renamed to containsAtLeastScalar (33d2e571c7 in #35253).

1 Like

I didn’t even know it was deprecated (and I also just double-checked: it wasn’t even mentioned in the 0.16.0 release notes).
It could have helped to have an additional step in-between where containsAtLeastScalar was removed but containsAtLeastScalar2 was still named the same. (or a toggleable error for deprecated things)

I agree. It would be nice if something like #22822/36703 would be implemented in a way, such that for changes like this you can choose between deprecated behavior, new behavior and a compiler error to see that something changed.

1 Like

Someone already made an issue about this: #35563

1 Like

Added a warning to the release notes: https://codeberg.org/ziglang/ziglang.org/commit/fb214309b3dbb3a491c07b31bd19f88ae3879f15

2 Likes