checker: require `params` attribute to use struct as keyword arguments in function (#11135)

pull/11489/head
Daniel Däschle 2021-09-13 23:05:30 +02:00 committed by GitHub
parent ae41d1d3c6
commit b63ec8fbcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 27 additions and 8 deletions

View File

@ -1,11 +1,12 @@
module gx
// TODO: remove these and uae the enum everywhere
// TODO: remove these, and use the enum everywhere
pub const (
align_left = HorizontalAlign.left
align_right = HorizontalAlign.right
)
[params]
pub struct TextCfg {
pub:
color Color = black

View File

@ -8,6 +8,7 @@ const (
retries = 10000
)
[params]
pub struct TempFileOptions {
path string = os.temp_dir()
pattern string
@ -44,6 +45,7 @@ pub fn temp_file(tfo TempFileOptions) ?(os.File, string) {
' could not create temporary file in "$d". Retry limit ($util.retries) exhausted. Please ensure write permissions.')
}
[params]
pub struct TempDirOptions {
path string = os.temp_dir()
pattern string

View File

@ -97,6 +97,7 @@ pub fn integer_from_u64(value u64) Integer {
}
}
[params]
pub struct IntegerConfig {
signum int = 1
}

View File

@ -533,6 +533,7 @@ pub fn (h Header) keys() []string {
return h.data.keys()
}
[params]
pub struct HeaderRenderConfig {
version Version
coerce bool

View File

@ -7,6 +7,7 @@ import rand.seed
// generator WyRand used 64 bits, ie. 2 u32s so that is the default. In case your desired generator
// uses a different number of u32s, use the `seed.time_seed_array()` method with the correct
// number of u32s.
[params]
pub struct PRNGConfigStruct {
pub:
seed_ []u32 = seed.time_seed_array(2)

View File

@ -3,6 +3,7 @@
// that can be found in the LICENSE file.
module time
[params]
pub struct StopWatchOptions {
auto_start bool = true
}

View File

@ -352,6 +352,7 @@ pub fn (t &Table) type_find_method(s &TypeSymbol, name string) ?Fn {
return none
}
[params]
pub struct GetEmbedsOptions {
preceding []Type
}

View File

@ -2088,15 +2088,17 @@ pub fn (mut c Checker) check_expected_arg_count(mut node ast.CallExpr, f &ast.Fn
if min_required_params == nr_args + 1 {
last_typ := f.params.last().typ
last_sym := c.table.get_type_symbol(last_typ)
if last_sym.kind == .struct_ {
// allow empty trailing struct syntax arg (`f()` where `f` is `fn(ConfigStruct)`)
node.args << ast.CallArg{
expr: ast.StructInit{
typ: last_typ
if last_sym.info is ast.Struct {
is_params := last_sym.info.attrs.filter(it.name == 'params' && !it.has_arg).len > 0
if is_params {
// allow empty trailing struct syntax arg (`f()` where `f` is `fn(ConfigStruct)`)
node.args << ast.CallArg{
expr: ast.StructInit{
typ: last_typ
}
}
typ: last_typ
return
}
return
}
}
c.error('expected $min_required_params arguments, but got $nr_args', node.pos)

View File

@ -14,6 +14,7 @@ mut:
max_type_len int
}
[params]
struct AddInfoConfig {
use_threshold bool
}

View File

@ -18,6 +18,7 @@ pub fn (mut f Fmt) attrs(attrs []ast.Attr) {
}
}
[params]
pub struct AttrsOptions {
inline bool
}

View File

@ -16,6 +16,7 @@ pub enum CommentsLevel {
// - level: either .keep (don't indent), or .indent (increment indentation)
// - iembed: a /* ... */ block comment used inside expressions; // comments the whole line
// - prev_line: the line number of the previous token to save linebreaks
[params]
pub struct CommentsOptions {
has_nl bool = true
inline bool

View File

@ -133,6 +133,7 @@ pub fn (mut f Fmt) wrap_long_line(penalty_idx int, add_indent bool) bool {
return true
}
[params]
pub struct RemoveNewLineConfig {
imports_buffer bool // Work on f.out_imports instead of f.out
}

View File

@ -642,6 +642,7 @@ pub fn (mut p Parser) comment_stmt() ast.ExprStmt {
}
}
[params]
struct EatCommentsConfig {
same_line bool // Only eat comments on the same line as the previous token
follow_up bool // Comments directly below the previous token as long as there is no empty line

View File

@ -1,3 +1,4 @@
[params]
struct Data {
array []int
}

View File

@ -1,3 +1,4 @@
[params]
struct Foo {
x int
}
@ -6,6 +7,7 @@ fn foo(f Foo) int {
return f.x
}
[params]
struct Bar {
x int
y int = 1234

View File

@ -247,6 +247,7 @@ fn test_fixed_field() {
//}
}
*/
[params]
struct Config {
mut:
n int