cznic / ql

github.com/cznic/ql has moved to modernc.org/ql
https://godoc.org/modernc.org/ql
BSD 3-Clause "New" or "Revised" License
1.31k stars 75 forks source link

storage_test: compilation failure with go 1.11 #203

Closed decathorpe closed 6 years ago

decathorpe commented 6 years ago
$GOPATH/src/github.com/cznic/ql/storage_test.go:423: dbg call has arguments but no formatting directives
cznic commented 6 years ago

The dbg call does exist at the line you reported. I can easily remove it, but I believe the compilation failure in go1.11 is most probably a vet bug. dbg is not a well know function taking formatting string in its first arument, like fmt.Printf, for example.

I don't see any ql bug here. Please report this problem directly at the Go issue tracker. Thank you.

cryptix commented 6 years ago

I think this is unfortunate false-positive but I fear a lot of people might come here for this.

Would dbg("Error %s", err) work as well?

cznic commented 6 years ago

Would dbg("Error %s", err) work as well?

Sure it will. But the language specification does not allow the Go compiler to reject a valid program, AFAICT. At least I recall I've been told that while discussing some other issue I forgot already about.

Anyway, if I forget a valid format directive in a call to fmt.Printf, log.Printf etc, I'm actually quite happy vet catches the problem. The problem is that there's no problem in a call to dbg with no formatting arguments. The function is this:

func dbg(s string, va ...interface{}) {
    if s == "" {
        s = strings.Repeat("%v ", len(va))
    }
    _, fn, fl, _ := runtime.Caller(1)
    fmt.Printf("dbg %s:%d: ", path.Base(fn), fl)
    fmt.Printf(s, va...)
    fmt.Println()
}

src

vet has no bussines in this case. Also, not every function with signature func(string, ...interface{}) is a printf-like function. That's why I believe this should be fixed in vet.

Well, and if the compiler guys reject that, I will be sad, but I will remove the offending line, of course. But please let's first try to fix vet.

decathorpe commented 6 years ago

Thanks for looking into this - I reported this as a probable error in go vet here: https://github.com/golang/go/issues/26486

ianlancetaylor commented 6 years ago

@cznic I came here from the Go project issue. The dbg function does indeed pass its arguments to fmt.Printf, so the call to dbg("", err) does look wrong to me. Why does it seem right to you?

cznic commented 6 years ago

@ianlancetaylor The code seen in https://github.com/cznic/ql/issues/203#issuecomment-406381754 handles specially the first argument of dbg when it's equal to "" in such a way that it becomes a valid format string of fmt.Printf.

It's not nice or idiomatic code, but I use the dbg a) for years b) as a quick and dirty debuging aid. In those years, I've typed the call probably some thousands of times. One will soon realize that automatically creating the right "default" format string is just making debugging a bit more comfortable.

tl;dr: The format string finally passed to fmt.Printf when the first argument to dbg is "" is a valid format string. I don't think vet is right when making such code fail the compilation and thus disabling go test to execute. Maybe I'm missing something, please let me know in that case.

It still holds that I'll fix it on my side if the decision is to keep vet "deep" inspecting user code w/o understanding that the "bad" case is actually handled correctly.

ianlancetaylor commented 6 years ago

Ah, I see, sorry for missing that.

I think that on balance vet is making the right choice here but let's discuss that over on the Go project issue.

cznic commented 6 years ago

This should be now fixed at tip: https://go-review.googlesource.com/c/go/+/125039

decathorpe commented 6 years ago

I can confirm that go 1.11beta3 contains the fix. Thanks for your help!