From patchwork Tue Jan 20 21:29:54 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 431260 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 172961401F6 for ; Wed, 21 Jan 2015 08:30:21 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:date:message-id:subject:from:to:content-type; q= dns; s=default; b=YqlVR+z0zf55gcttpRhAJ2Jsnq3nrFlFB5JVVO6AePcayE AhufX4EpQevhRd2mTbYBio4OhtR1PWtRXEIX8wYUBh50S+g3oYKqXFzZCVUMl+Oh 4b7Vjukz05yJqdv9PpGVza/kdJg0+QBAfPMNHU/u1ZmqaeTAPY5oqOZFVlVok= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:date:message-id:subject:from:to:content-type; s= default; bh=yv4UXxUQY+PSVkKjJ0/yC2XSY6I=; b=uBPrInQnhFcUq+Sj0unz 4wOLCExmm5Ra+068yW69xJwg38mcM4JJ5vtq6ceNZNS6XpWAJ9M8S7PVvf4zAOqC n2cyut9TRddQn7snRgaakgaoq2XxSCYUpSLTd2kcvomIF2Pt310PD1iXgY4gSW9M S3+m7kJGLjYr8FAs0l5QqdU= Received: (qmail 24536 invoked by alias); 20 Jan 2015 21:30:03 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 24311 invoked by uid 89); 20 Jan 2015 21:29:59 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-yh0-f54.google.com Received: from mail-yh0-f54.google.com (HELO mail-yh0-f54.google.com) (209.85.213.54) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Tue, 20 Jan 2015 21:29:56 +0000 Received: by mail-yh0-f54.google.com with SMTP id 29so2716714yhl.13 for ; Tue, 20 Jan 2015 13:29:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:date:message-id:subject:from:to :content-type; bh=tpmz4cX8sgFMbdMuUhcgFB1LyQZ5sD1eMx7LBkKrU60=; b=Fs6dzLY6wzV/lwIncKUaLjjzJrgkPm7ovsf7Sydd3Z0bNyOCp2B7aJbgDUP6GIb7pX PwiRFdaQ1wYAUbL/TqkFO+ieRcaPR9e2ZpnjakFDd/4S2k7BCx2kkCJNIqtBG/UQ/R0d PHCow0aAkbtq5Jhbw1yJDjKcBa3aaRjo0+/AJ9hdTwiD/0wSGSxrIOLRhAtnI/09WlUE DXVN+ABmMHSmImuX1IZgiRnQ1gk8TfUsPmUrTgMoQwdbZ5P7Hu1rcaKJuChOtpjIbQ8k vd7CeXB6AaF0aS2ccY2XiqN4gARn8c+GiMAQbE0y6atDJg2IR2m8OPGOccOPjNhjfEnR ugHw== X-Gm-Message-State: ALoCoQleAMZW33qfpv/h4cLFvIS+ElxVCnlPqeKmDdnjisaZIYLqm/v9AgRJAHHJdYBDmTYGNBlg MIME-Version: 1.0 X-Received: by 10.236.231.49 with SMTP id k47mr21783053yhq.86.1421789394607; Tue, 20 Jan 2015 13:29:54 -0800 (PST) Received: by 10.170.57.196 with HTTP; Tue, 20 Jan 2015 13:29:54 -0800 (PST) Date: Tue, 20 Jan 2015 13:29:54 -0800 Message-ID: Subject: Go patch committed: Do not mark unused variables as used in closures From: Ian Lance Taylor To: gcc-patches , "gofrontend-dev@googlegroups.com" This patch from Chris Manghane fixes the Go frontend to not always mark variables in closures as used. This is http://golang.org/issue/6415. Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu. Committed to mainline. Ian diff -r cbdec4465fa8 go/parse.cc --- a/go/parse.cc Tue Jan 20 08:11:07 2015 -0800 +++ b/go/parse.cc Tue Jan 20 13:24:35 2015 -0800 @@ -2450,7 +2450,7 @@ && (named_object->is_variable() || named_object->is_result_variable())) return this->enclosing_var_reference(in_function, named_object, - location); + may_be_sink, location); switch (named_object->classification()) { @@ -2591,11 +2591,14 @@ Expression* Parse::enclosing_var_reference(Named_object* in_function, Named_object* var, - Location location) + bool may_be_sink, Location location) { go_assert(var->is_variable() || var->is_result_variable()); - this->mark_var_used(var); + // Any left-hand-side can be a sink, so if this can not be + // a sink, then it must be a use of the variable. + if (!may_be_sink) + this->mark_var_used(var); Named_object* this_function = this->gogo_->current_function(); Named_object* closure = this_function->func_value()->closure_var(); @@ -2912,7 +2915,7 @@ ref = Expression::make_var_reference(var, location); else ref = this->enclosing_var_reference(ev[i].in_function(), var, - location); + true, location); Expression* refaddr = Expression::make_unary(OPERATOR_AND, ref, location); initializer->push_back(refaddr); @@ -3215,7 +3218,7 @@ if (in_function != NULL && in_function != this->gogo_->current_function() && (named_object->is_variable() || named_object->is_result_variable())) - return this->enclosing_var_reference(in_function, named_object, + return this->enclosing_var_reference(in_function, named_object, is_lhs, location); switch (named_object->classification()) @@ -5722,6 +5725,20 @@ Var_expression* ve = expr->var_expression(); if (ve != NULL) this->mark_var_used(ve->named_object()); + else if (expr->deref()->field_reference_expression() != NULL + && this->gogo_->current_function() != NULL) + { + // We could be looking at a variable referenced from a closure. + // If so, we need to get the enclosed variable and mark it as used. + Function* this_function = this->gogo_->current_function()->func_value(); + Named_object* closure = this_function->closure_var(); + if (closure != NULL) + { + unsigned int var_index = + expr->deref()->field_reference_expression()->field_index(); + this->mark_var_used(this_function->enclosing_var(var_index - 1)); + } + } return expr; } diff -r cbdec4465fa8 go/parse.h --- a/go/parse.h Tue Jan 20 08:11:07 2015 -0800 +++ b/go/parse.h Tue Jan 20 13:24:35 2015 -0800 @@ -218,7 +218,7 @@ Typed_identifier* receiver(); Expression* operand(bool may_be_sink, bool *is_parenthesized); Expression* enclosing_var_reference(Named_object*, Named_object*, - Location); + bool may_be_sink, Location); Expression* composite_lit(Type*, int depth, Location); Expression* function_lit(); Expression* create_closure(Named_object* function, Enclosing_vars*,