From patchwork Mon Oct 9 19:59:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Marie de Rodat X-Patchwork-Id: 823466 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-463808-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="FlNxc+KM"; dkim-atps=neutral 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 3y9rgH6vpTz9rxl for ; Tue, 10 Oct 2017 06:59:39 +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:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=k6jyucxnNXSk/B8hr7DVjBRKi6OyozieYTYhvCbVH+6bPTIOFG 6Wjp49J+dFoXZL+Utdiywb+kmXakQuYvVpgZvwet0QJoA7CJ2h4XUn4FBUBVyjLp 0XHDJbpLGTAWZ6xE0MbSHRPaNiQdOeZbfLkhg0VWkZYUCk5X8rJ+a4Kto= 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:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=+z22H7TH/NsA+X/3SFDM6z+CHx4=; b=FlNxc+KM/hQ2XZ8/DO/7 VAXHdE6Ex27AnlJuZfx2eWiHtMVJVUNGNJbP7sxpeETIPkezJm8LugBaUR//J70S Dh9MhQlhOcJKJN9LdwnoZUFTSSQXzyLFtPmB0M57Ug5RxfSCEWeI5qP8jq2+j70N /fPRida2NTRUs1vn4/OttU4= Received: (qmail 12294 invoked by alias); 9 Oct 2017 19:59:28 -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 12278 invoked by uid 89); 9 Oct 2017 19:59:28 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=Precision X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 09 Oct 2017 19:59:26 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 4A4255631C; Mon, 9 Oct 2017 15:59:25 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id AGRlXI2NNFhM; Mon, 9 Oct 2017 15:59:25 -0400 (EDT) Received: from tron.gnat.com (tron.gnat.com [205.232.38.10]) by rock.gnat.com (Postfix) with ESMTP id 359CC561D5; Mon, 9 Oct 2017 15:59:25 -0400 (EDT) Received: by tron.gnat.com (Postfix, from userid 4862) id 31F7843A; Mon, 9 Oct 2017 15:59:25 -0400 (EDT) Date: Mon, 9 Oct 2017 15:59:25 -0400 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Cc: Ed Schonberg Subject: [Ada] Spurious error with predicate and class-wide object Message-ID: <20171009195925.GA43460@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-IsSubscribed: yes This patch removes a spurious error on a call to a function that applies to a predicated tagged type, when the actual in the call is class-wide. The argument must be converted to be type-specific, given that the predicate function is not dispatching and cannot accept a class-wide actual. Executing: gnatmake -q -gnata main main must yield: Predicate checked Predicate checked Predicate checked Predicate checked Predicate checked Predicate checked --- with Predicate_Ints; use Predicate_Ints; procedure Main is Thing1 : Int := (0, 100, 50); Thing2 : Approx_Int := (0, 100, 50, 13); begin Call_Bump (Thing1); Call_Bump (Thing2); end; --- package Predicate_Ints is type Int is tagged record Min, Max, Value : Integer; end record with Predicate => Value in Min .. Max and then Checked; procedure Bump (Arg : in out Int); procedure Call_Bump (Arg : in out Int'Class); function Checked return Boolean; type Approx_Int is new Int with record Precision : Natural; end record; end Predicate_Ints; --- with Text_IO; use Text_IO; package body Predicate_Ints is function Checked return Boolean is begin Put_Line ("Predicate checked"); return True; end; procedure Bump (Arg : in out Int) is begin Arg.Value := Arg.Value + 1; end Bump; procedure Call_Bump (Arg : in out Int'Class) is begin Arg.Bump; end Call_Bump; end Predicate_Ints; Tested on x86_64-pc-linux-gnu, committed on trunk 2017-10-09 Ed Schonberg * exp_util.adb (Make_Predicate_Call): If the type of the expression to which the predicate check applies is tagged, convert the expression to that type. This is in most cases a no-op, but is relevant if the expression is clas-swide, because the predicate function being invoked is not a primitive of the type and cannot take a class-wide actual. Index: exp_util.adb =================================================================== --- exp_util.adb (revision 253559) +++ exp_util.adb (working copy) @@ -9305,11 +9305,23 @@ -- Case of calling normal predicate function - Call := - Make_Function_Call (Loc, - Name => New_Occurrence_Of (Func_Id, Loc), - Parameter_Associations => New_List (Relocate_Node (Expr))); + -- If the type is tagged, the expression may be class-wide, in which + -- case it has to be converted to its root type, given that the + -- generated predicate function is not dispatching. + if Is_Tagged_Type (Typ) then + Call := + Make_Function_Call (Loc, + Name => New_Occurrence_Of (Func_Id, Loc), + Parameter_Associations => + New_List (Convert_To (Typ, Relocate_Node (Expr)))); + else + Call := + Make_Function_Call (Loc, + Name => New_Occurrence_Of (Func_Id, Loc), + Parameter_Associations => New_List (Relocate_Node (Expr))); + end if; + Restore_Ghost_Mode (Saved_GM); return Call;