From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by sourceware.org (Postfix) with ESMTPS id E4E463858D1E for ; Mon, 13 Feb 2023 22:02:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E4E463858D1E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=jguk.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=jguk.org Received: by mail-wm1-x32d.google.com with SMTP id n33so3655226wms.0 for ; Mon, 13 Feb 2023 14:02:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jguk.org; s=google; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:subject:from:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=awCrMXG4MWNvsNB7CbfgKNBIlLRCQPUtYESGllm4Qoc=; b=lrZ3HdgQahVTlxtn0VFb9e7fH8TyWLDy5f0p6PTByIgHEymq6VS3PBvC/OrJEwcmG8 /93Uz1y6sPiepEYdT5WLCa9WK+QLjoDHfWS+Kt+AoeAmW8bb2zzDDlwBSQwWtiTL5odX +arMgZDpyQsKd3TnnLEffHFo59fOHPUFghSI0mFh/eOwyG7Wohsm6HJ8mITDiHEBRuQa 1lJ7r/GPKYvaSKYfrwJ3vHAaogFrhSRps/tyNTs3a2x833bincrHa6Ypjzs60sGic53/ fXM1TMRlZeUVqlbufi+kOrS8VR49e4F2OLO1fxBOTLkxEpi3dgBjIsSUHcbD4DnvUumH yplA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:subject:from:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=awCrMXG4MWNvsNB7CbfgKNBIlLRCQPUtYESGllm4Qoc=; b=0BcETziQWEsUn9Ap/IWYlz36BBsQkEhZDEaXOgIiNGvyyquV/VTWCJ7yFM4pPip1Fv Dd0aj2lj+Fbd8K7urMw8KpcCnNS7OzzUU5jPwvIRjq5uh0jc5paj8fJfw+6LopiTD9fQ WY3SKmAW7m6tnNPFqELfCoejoKk0wDYGJsP32s7w7ttSh7sF8TNYvr2zcfp9NNjjfPyG rMP2SL1cxdvDIdXjesQUc+2snUObvfxSh0wTSt8tHjnTAE/hTwaJlpTej9BDVjyI0ChA CoWwbTFJN0npnB5QUn/kA9Ob4pc+wdnj/3u8Y5nn+KfYRSyfRuKTVzkqY8zh66Pdc0Ka YJdw== X-Gm-Message-State: AO0yUKUeFxhWdLCAZawuThzRiXwxq8yvXyIsayBtN71yPFVCCuS/S1HB hrVc3cxKi7i7GIfbZkneVFNsBQ== X-Google-Smtp-Source: AK7set9nIruAfmOGGLokpbqDf4WxMiBr/9N/LGv4lhmi3wghgddT4ebD/8DaK+t1xz24+uoQHNUsTA== X-Received: by 2002:a05:600c:1d22:b0:3df:d431:cf64 with SMTP id l34-20020a05600c1d2200b003dfd431cf64mr61891wms.39.1676325759554; Mon, 13 Feb 2023 14:02:39 -0800 (PST) Received: from [192.168.0.12] (cpc87345-slou4-2-0-cust172.17-4.cable.virginm.net. [81.101.252.173]) by smtp.gmail.com with ESMTPSA id g20-20020a7bc4d4000000b003a3442f1229sm18380477wmk.29.2023.02.13.14.02.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 13 Feb 2023 14:02:39 -0800 (PST) Message-ID: <09fc5748-53f2-5e81-80df-c617800a2b05@jguk.org> Date: Mon, 13 Feb 2023 22:02:38 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 From: Jonny Grant Subject: Re: std::string add nullptr attribute To: Jonathan Wakely Cc: Xi Ruoyao , gcc-help References: <7e6e3bbf-0dac-0632-0e8f-372bd32a6923@jguk.org> <6e30ed8e6c6f08407a5b8259e73fd18a492376b5.camel@xry111.site> <8cfbab8b-07e8-7dab-c829-6de77cc8cf39@jguk.org> <6b530d67-723a-a0c9-15bc-12b7341653a7@jguk.org> Content-Language: en-GB In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,KAM_SHORT,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 11/02/2023 00:32, Jonathan Wakely wrote: > On Fri, 10 Feb 2023 at 22:38, Jonny Grant wrote: >> >> >> >> On 10/02/2023 22:03, Jonathan Wakely wrote: >>> On Fri, 10 Feb 2023 at 21:30, Jonny Grant wrote: >>>> >>>> >>>> >>>> On 09/02/2023 17:52, Jonathan Wakely wrote: >>>>> On Thu, 9 Feb 2023 at 16:30, Xi Ruoyao wrote: >>>>>> >>>>>> On Thu, 2023-02-09 at 14:56 +0000, Jonathan Wakely via Gcc-help wrote: >>>>>>>> Note, my code isn't like this, it is just an example to suggest >>>>>>>> adding the nullptr attribute, as its clearly already rejected at >>>>>>>> runtime. >>>>>>> >>>>>>> I assume you mean the nonnull attribute. That was added in 2020 and >>>>>>> then reverted because it broke some things: >>>>>> >>>>>> I remember I'd once made the same mistake when I suggested to add >>>>>> nonnull for ostream::operator<<(const string &) and I was lectured: >>>>>> nonnull is not only a diagnostic attribute, it also allows the compiler >>>>>> to assume the parameter is never null and rendering std::string(nullptr) >>>>>> an undefined behavior. >>>>> >>>>> Yes, I think that's what might have happened with the std::string change. >>>> >>>> My apologies, Jonathan, Xi, yes it is the __attribute__((nonnull)); I was mistaken to type as nullptr. >>>> >>>> I re-read, and it does seem nonnull is really an optimization that as a side effect may give some warnings. So I'm going to stop using it. >>>> https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes >>>> >>>> (there is a typo in that manual section saying "nonnul" - I don't know if you have a moment to make a change in git? I didn't get replies on gcc-patches to my patches...) >>>> >>>> I searched and see like someone investigated this problem and saw it removed NULL checks http://www.rkoucha.fr/tech_corner/nonnull_gcc_attribute.html >>>> >>>> I saw wget2 removed the nonnull attribute due to the optimizer removing checks against NULL too >>>> https://gitlab.com/gnuwget/wget2/-/issues/200 >>>> >>>>>> Then the example may just silently continue to run, instead of throwing >>>>>> an exception. It would be an ironic example: an attempt to improve >>>>>> diagnostic finally made diagnostic more difficult. >>>>> >>>>> Indeed. >>>>> >>>>> Maybe we can add __attribute__((access(read, 1))) instead, which says >>>>> that we will read from the pointer, which also implies it must be >>>>> non-null. >>>> >>>> I tried this with gcc 12, as read_only, but it didn't stop when compiling. Maybe you have an example that demonstrates please? >>>> >>>> void f(const char * p) __attribute__((access(read_only, 1))); >>>> >>>>> >>>>> N.B. in C++23 string(nullptr) produces an error, although >>>>> string((const char*)nullptr) doesn't, so in practice it only prevents >>>>> the dumbest calls with a literal 'nullptr' token, and not the more >>>>> realistic problems where you have a pointer that happens to be null. >>>> >>>> That's good it stops compiling, the error is not that clear "use of deleted function" for me though. >>>> >>>> string.cpp: In function ‘int main()’: >>>> string.cpp:13:26: error: use of deleted function ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::nullptr_t) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::nullptr_t = std::nullptr_t]’ >>>> 13 | std::string c(nullptr); >>>> >>>> >>>> >>>> >>>> I made my own test class str_string which stops the build a different way. It only works if the dumbest calls with 'nullptr' as you found in your test. >>>> >>>> void nullptr_compile_abort() __attribute__((error("nullptr compile error"))); >>>> >>>> str_string(nullptr_t) { nullptr_compile_abort(); } >>> >>> This doesn't work because std::is_constructible_v>> std::nullptr_t> would be true, and we want it to be false. >> >> Hmm, for me, this output is 0. >> std::cout << std::is_constructible_v << "\n"; > > For C++23, yes, but if you add a constructor like your > str_string(nullptr_t) it would become 1. > > Using a deleted function is observably different to using a > constructor that then produces an error when called. Indeed. May I ask if you found a way to get the read_only attribute to trigger a build warning for nullptr? It seems like only runtime checks can catch most of them for the moment. Jonny