checker: fix @[deprecated] attribute for consts (fix #20523) (#20550)

This commit is contained in:
shove 2024-01-16 14:00:47 +08:00 committed by GitHub
parent e30182c628
commit 072d65b28a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 1 deletions

View File

@ -39,24 +39,70 @@ pub const max_f64 = 1.797693134862315708145274237317043567981e+308 // 2**1023 *
pub const smallest_non_zero_f64 = 4.940656458412465441765687928682213723651e-324
@[deprecated: 'use built-in constants instead (e.g. min_i8 instead of math.min_i8)']
@[deprecated: 'use built-in constant `min_i8` instead']
@[deprecated_after: '2023-12-31']
pub const min_i8 = i8(-128)
@[deprecated: 'use built-in constant `max_i8` instead']
@[deprecated_after: '2023-12-31']
pub const max_i8 = i8(127)
@[deprecated: 'use built-in constant `min_i16` instead']
@[deprecated_after: '2023-12-31']
pub const min_i16 = i16(-32768)
@[deprecated: 'use built-in constant `max_i16` instead']
@[deprecated_after: '2023-12-31']
pub const max_i16 = i16(32767)
@[deprecated: 'use built-in constant `min_i32` instead']
@[deprecated_after: '2023-12-31']
pub const min_i32 = i32(-2147483648)
@[deprecated: 'use built-in constant `max_i32` instead']
@[deprecated_after: '2023-12-31']
pub const max_i32 = i32(2147483647)
// -9223372036854775808 is wrong, because C compilers parse literal values
// without sign first, and 9223372036854775808 overflows i64, hence the
// consecutive subtraction by 1
@[deprecated: 'use built-in constant `min_i64` instead']
@[deprecated_after: '2023-12-31']
pub const min_i64 = i64(-9223372036854775807 - 1)
@[deprecated: 'use built-in constant `max_i64` instead']
@[deprecated_after: '2023-12-31']
pub const max_i64 = i64(9223372036854775807)
@[deprecated: 'use built-in constant `min_u8` instead']
@[deprecated_after: '2023-12-31']
pub const min_u8 = u8(0)
@[deprecated: 'use built-in constant `max_u8` instead']
@[deprecated_after: '2023-12-31']
pub const max_u8 = u8(255)
@[deprecated: 'use built-in constant `min_u16` instead']
@[deprecated_after: '2023-12-31']
pub const min_u16 = u16(0)
@[deprecated: 'use built-in constant `max_u16` instead']
@[deprecated_after: '2023-12-31']
pub const max_u16 = u16(65535)
@[deprecated: 'use built-in constant `min_u32` instead']
@[deprecated_after: '2023-12-31']
pub const min_u32 = u32(0)
@[deprecated: 'use built-in constant `max_u32` instead']
@[deprecated_after: '2023-12-31']
pub const max_u32 = u32(4294967295)
@[deprecated: 'use built-in constant `min_u64` instead']
@[deprecated_after: '2023-12-31']
pub const min_u64 = u64(0)
@[deprecated: 'use built-in constant `max_u64` instead']
@[deprecated_after: '2023-12-31']
pub const max_u64 = u64(18446744073709551615)

View File

@ -363,6 +363,7 @@ pub:
is_pub bool
is_markused bool // an explicit `[markused]` tag; the const will NOT be removed by `-skip-unused`, no matter what
pos token.Pos
attrs []Attr // same value as `attrs` of the ConstDecl to which it belongs
pub mut:
expr Expr // the value expr of field; everything after `=`
typ Type // the type of the const field, it can be any type in V

View File

@ -3685,6 +3685,10 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
obj.typ = typ
node.obj = obj
if obj.attrs.contains('deprecated') && obj.mod != c.mod {
c.deprecate('const', '${obj.name}', obj.attrs, node.pos)
}
if node.or_expr.kind != .absent {
unwrapped_typ := typ.clear_option_and_result()
c.expected_or_type = unwrapped_typ
@ -3736,6 +3740,12 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
saved_mod := node.mod
node.mod = 'builtin'
builtin_type := c.ident(mut node)
if node.obj is ast.ConstField {
field := node.obj as ast.ConstField
if field.attrs.contains('deprecated') && field.mod != c.mod {
c.deprecate('const', '${field.name}', field.attrs, node.pos)
}
}
if builtin_type != ast.void_type {
return builtin_type
}

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/deprecations_consts.vv:3:10: warning: const `math.max_i8` has been deprecated since 2023-12-31, it will be an error after 2024-06-28; use built-in constant `max_i8` instead
1 | import math
2 |
3 | _ = math.max_i8
| ~~~~~~

View File

@ -0,0 +1,3 @@
import math
_ = math.max_i8

View File

@ -3875,6 +3875,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
is_pub: is_pub
expr: expr
pos: pos.extend(expr.pos())
attrs: attrs
comments: comments
end_comments: end_comments
is_markused: is_markused