Skip to content

Commit

Permalink
std::mem: disallow void and function types for the SizeOf and AlignOf…
Browse files Browse the repository at this point in the history
… functions
  • Loading branch information
mertcandav committed Jun 22, 2024
1 parent 0e91fbf commit 7712a87
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions std/jule/build/log.jule
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ enum LogMsg: str {
ArraySizeOverflow: "array with size @ overflows limit (@) of the system",
InvalidTypeForTypeOf: "comptime::TypeOf is not supports type @",
ComptimeAsExpr: "compile-time evaluations cannot used as expression",
InvalidTypeForFn: `type @ is invalid for function @`,

// Suggestions.
ExpectedIdentifier: `write an identifier because identifier expected`,
Expand Down
6 changes: 6 additions & 0 deletions std/jule/sema/builtin.jule
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,9 @@ fn builtinCallerStdMemSizeOf(mut &e: &Eval, mut &fc: &FnCallExpr, mut &_: &Data)
if d == nil {
ret result
}
if !validTypeForXof(d.Kind) {
e.pushErr(fc.Args[0].Token, LogMsg.InvalidTypeForFn, d.Kind.Str(), "SizeOf")
}

result.Model = &SizeofExprModel{Expr: d.Model}
ret result
Expand All @@ -874,6 +877,9 @@ fn builtinCallerStdMemAlignOf(mut &e: &Eval, mut &fc: &FnCallExpr, mut &_: &Data
if d == nil {
ret result
}
if !validTypeForXof(d.Kind) {
e.pushErr(fc.Args[0].Token, LogMsg.InvalidTypeForFn, d.Kind.Str(), "AlignOf")
}

result.Model = &AlignofExprModel{Expr: d.Model}
ret result
Expand Down
4 changes: 4 additions & 0 deletions std/jule/sema/type.jule
Original file line number Diff line number Diff line change
Expand Up @@ -1739,4 +1739,8 @@ fn applyImplicitCast(mut &dest: &TypeKind, mut &d: &Data) {
applyCastKindModel(d, dest)
ret
}
}

fn validTypeForXof(mut &t: &TypeKind): bool {
ret !t.Void() && t.Fn() == nil
}
2 changes: 2 additions & 0 deletions std/mem/builtin.jule
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

// Returns the size of the type in bytes.
// If given expression, uses type of expression.
// Void and function types are not supported.
// fn SizeOf(TYPE || EXPRESSION): uint

// Returns the alignment, in bytes, required for any instance of the type
// indicated by type-id, which is either complete object type.
// If given expression, uses type of expression.
// Void and function types are not supported.
// fn AlignOf(TYPE || EXPRESSION): uint

// Frees memory.
Expand Down

0 comments on commit 7712a87

Please sign in to comment.