From d7c99cdf9fa5548db179758ac9dd267f5f1c9e88 Mon Sep 17 00:00:00 2001 From: =?utf8?q?R=C3=A9my=20Oudompheng?= Date: Thu, 25 Jul 2013 09:42:05 -0400 Subject: [PATCH] cmd/gc: avoid passing unevaluated constant expressions to backends. Backends do not exactly expect receiving binary operators with constant operands or use workarounds to move them to register/stack in order to handle them. Fixes #5841. R=golang-dev, daniel.morsing, rsc CC=golang-dev https://golang.org/cl/11107044 --- src/cmd/gc/walk.c | 7 +++++++ test/fixedbugs/issue5841.go | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 test/fixedbugs/issue5841.go diff --git a/src/cmd/gc/walk.c b/src/cmd/gc/walk.c index c5a5874a19..892d73bc6f 100644 --- a/src/cmd/gc/walk.c +++ b/src/cmd/gc/walk.c @@ -1379,6 +1379,13 @@ walkexpr(Node **np, NodeList **init) fatal("missing switch %O", n->op); ret: + // Expressions that are constant at run time but not + // considered const by the language spec are not turned into + // constants until walk. For example, if n is y%1 == 0, the + // walk of y%1 may have replaced it by 0. + // Check whether n with its updated args is itself now a constant. + evconst(n); + ullmancalc(n); if(debug['w'] && n != N) diff --git a/test/fixedbugs/issue5841.go b/test/fixedbugs/issue5841.go new file mode 100644 index 0000000000..cfc4a504c5 --- /dev/null +++ b/test/fixedbugs/issue5841.go @@ -0,0 +1,16 @@ +// build + +// Copyright 2013 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Issue 5841: 8g produces invalid CMPL $0, $0. +// Similar to issue 5002, used to fail at link time. + +package main + +func main() { + var y int + if y%1 == 0 { + } +} -- 2.48.1