Giter VIP home page Giter VIP logo

Comments (2)

nektro avatar nektro commented on May 29, 2024 2

while I wouldn't mind seeing this fixed and still write this way in my own code atm, I could also see an argument that it should be left as-is since it would better parallel with function body code that the comptime keyword is only used to elevate otherwise-runtime things to be comptime-known. in bodies we even have an error about using it redundantly.

from zig.

mlugg avatar mlugg commented on May 29, 2024

This problem is a little trickier than it seems, because we don't necessarily know if a function call is comptime until after we evaluate its parameters. Consider:

fn f(T: type, x: anytype) @TypeOf(x) {
    _ = T;
    return x;
}

If f is called in a runtime context, the call itself might still be comptime depending on whether x has a comptime-only type. That means we require a comptime annotation on T only if x has a runtime type. This might be non-trivial - I forget how the Sema logic is structured.

If that's hard, it'd be easiest to instead tighten the rule a bit and say that the comptime is only optional if the call site is already being comptime-evaluated, i.e. it's not just because the function is comptime-only. I'm not sure if this would regress use cases however, and it feels like a more complex rule.

By the way, a good separate enhancement might be to tighten the rules further by requiring the annotation on any argument which is not generic. We could check in AstGen that no identifier is a prior parameter, or a generic capture, or a decl derived from a generic capture. In that case, we could completely change the rule to the following, which would be nicer and also more generally helpful:

"A function parameter with a comptime-only type must be marked comptime if its type is comptime-only or the return type is comptime-only, and the parameter type is not generic."

from zig.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.