PSA: absl::optional is now std::optional

1,647 views
Skip to first unread message

David Benjamin

unread,
Nov 6, 2023, 10:53:29 AM11/6/23
to Chromium-dev, cxx
Hi all,

As of https://crrev.com/1204351, absl::optional is now a type alias for std::optional. Likewise, absl::nullopt is now an alias for std::nullopt. Please feel free to start writing std spellings instead in new code.

A handy cheat sheet:

#include "third_party/abseil-cpp/absl/types/optional.h" => #include <optional>

absl::optional => std::optional
absl::nullopt => std::nullopt
absl::in_place => std::in_place
absl::in_place_t => std::in_place_t

David

Anton Bikineev

unread,
Nov 6, 2023, 11:12:06 AM11/6/23
to David Benjamin, Chromium-dev, cxx
Shall we consider a rewrite? I guess a simple sed-like rewrite would do the trick.

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAF8qwaAe28FmirLG6%3D3HRg%2BzaqHdp9WiMt2A%3Dcn5OyYNwcbfPg%40mail.gmail.com.

David Benjamin

unread,
Nov 6, 2023, 11:32:59 AM11/6/23
to Anton Bikineev, Chromium-dev, cxx
Probably! I don't think I'll have the cycles to drive a giant rewrite, but I'd definitely be in support of it.

Peter Boström

unread,
Nov 6, 2023, 12:34:40 PM11/6/23
to David Benjamin, Anton Bikineev, Chromium-dev, cxx
I think IWYU is the non-trivial thing, iirc no tool fixes that for us automatically.

Peter Boström

unread,
Nov 6, 2023, 12:35:25 PM11/6/23
to David Benjamin, Anton Bikineev, Chromium-dev, cxx
(unless our tools are better than when I left on parental leave)

Anton Bikineev

unread,
Nov 6, 2023, 12:41:51 PM11/6/23
to Peter Boström, David Benjamin, Chromium-dev, cxx
I think IWYU is the non-trivial thing, iirc no tool fixes that for us automatically.
I converted base::Optional to absl::optional a while ago using a custom clang-tidy rewriter which also fixed the includes. I can share the rewriter sources if somebody is willing to tackle this (I myself lack the cycles unfortunately).

Daniel Cheng

unread,
Nov 6, 2023, 12:44:55 PM11/6/23
to Peter Boström, David Benjamin, Anton Bikineev, Chromium-dev, cxx
We don't have a functioning IWYU, but generally, these sorts of things can be (relatively) easily fixed up by script if there's enough of them. We already have an add_header.py script that automates the work of inserting the header, so it's just a matter of mapping compile errors to the corresponding header (or proactively fixing them).

Daniel

Arthur Sonzogni

unread,
Nov 6, 2023, 1:20:52 PM11/6/23
to Daniel Cheng, Peter Boström, David Benjamin, Anton Bikineev, Chromium-dev, cxx
Would the rewrite be more complex than a search and replace?

Suggested script:
#!/bin/bash
 
function replace {
  echo "Replacing $1 by $2"
  git grep -l "$1" | cut -f1 -d: | grep "\.h$" | xargs sed -i "s/$1/$2/g"
  git grep -l "$1" | cut -f1 -d: | grep "\.cc$" | xargs sed -i "s/$1/$2/g"
}
 
 
replace "absl::optional" "std::optional"
replace "absl::nullopt" "std::nullopt"
replace "absl::in_place" "std::in_place"
replace "absl::in_place_t" "std::in_place_t"
replace "\"third_party\/abseil-cpp\/absl\/types\/optional.h\"" "<optional>"
git cl format
This creates a 20k file change, modifying 103k lines:
https://chromium-review.googlesource.com/c/chromium/src/+/5003622

If this had been a large change associated with "//base", I would have sent the patch to a top-level base OWNER with Owners-override.
Here, this is an "abseil" vs "system" header, so I would probably first ask for a LSC change approval.
If you are interested and there are no particular difficulties, I can take a look tomorrow and ask to execute this, probably split into smaller patches to avoid constant merge conflicts.

Arthur @arthursonzogni


Alexei Svitkine

unread,
Nov 6, 2023, 1:23:34 PM11/6/23
to Arthur Sonzogni, Daniel Cheng, Peter Boström, David Benjamin, Anton Bikineev, Chromium-dev, cxx
For splitting, you can use `git cl split` after making the changes to all files locally and it will automatically break it up into separate CLs and send to appropriate owners.

Dana Jansens

unread,
Nov 6, 2023, 1:52:31 PM11/6/23
to Arthur Sonzogni, Daniel Cheng, Peter Boström, David Benjamin, Anton Bikineev, Chromium-dev, cxx
👍

On Mon, Nov 6, 2023 at 1:20 PM 'Arthur Sonzogni' via cxx <c...@chromium.org> wrote:

Peter Boström

unread,
Nov 6, 2023, 1:56:10 PM11/6/23
to Dana Jansens, Arthur Sonzogni, Daniel Cheng, David Benjamin, Anton Bikineev, Chromium-dev, cxx
Thank you Arthur! I wouldn't think that a LSC is required here, just splitting this into reasonable OWNERS coverage would make sense though (and yield fewer merge conflicts).

Sylvain Defresne

unread,
Nov 7, 2023, 4:31:56 AM11/7/23
to Dana Jansens, Arthur Sonzogni, Daniel Cheng, Peter Boström, David Benjamin, Anton Bikineev, Chromium-dev, cxx

IIRC this script will miss all .mm files. If you go this way, don't forget to also fix them.

As a followup steps, it would probably also be necessary to remove deps or public_deps on third_party/abseil-cpp:absl target that are no longer necessary (which could be more difficult than the string substitution).
-- Sylvain

Lei Zhang

unread,
Nov 7, 2023, 2:45:49 PM11/7/23
to Sylvain Defresne, Dana Jansens, Arthur Sonzogni, Daniel Cheng, Peter Boström, David Benjamin, Anton Bikineev, Chromium-dev, cxx
The add_header.py script also doesn't understand <windows.h> likes to
be first, and tries to combine it into the list of C-headers. It does
a good job overall, but one should check its work.
> To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAJauWCGA%3D%2BVo4Ak__APCcGox8btxDp8AgUVTL65X%3DMZC_BGvow%40mail.gmail.com.

Greg Thompson

unread,
Nov 8, 2023, 10:15:37 AM11/8/23
to Lei Zhang, Sylvain Defresne, Dana Jansens, Arthur Sonzogni, Daniel Cheng, Peter Boström, David Benjamin, Anton Bikineev, Chromium-dev, cxx
Hi Lei.

While we used to have a policy of always putting <windows.h> above all other headers, ISTR that the modern thought is to put it where we would normally put it, and only bump it to the top if necessary. I don't recall when this happened, but my brain is telling me that's the situation...

Arthur Sonzogni

unread,
Nov 8, 2023, 12:18:29 PM11/8/23
to Sylvain Defresne, cxx, Greg Thompson, Sylvain Defresne, Dana Jansens, Daniel Cheng, Peter Boström, David Benjamin, Anton Bikineev, Chromium-dev, Lei Zhang
FTR, I've converted all of //ios to use std::optional instead of absl::optional.
-- Sylvain

Thanks Sylvain! Also for your previous suggestions.
I opened https://crbug.com/1500249, and started renaming.
This is mostly about applying the script to some directories, and then manually fixing trivial errors due to dependencies and/or non-transitive includes missing.
 
Arthur @arthursonzogni


On Wed, Nov 8, 2023 at 6:05 PM Sylvain Defresne <sdef...@google.com> wrote:
FTR, I've converted all of //ios to use std::optional instead of absl::optional.
-- Sylvain

Sylvain Defresne

unread,
Nov 14, 2023, 1:26:46 PM11/14/23
to cxx, Greg Thompson, Sylvain Defresne, Dana Jansens, Arthur Sonzogni, Daniel Cheng, Peter Boström, David Benjamin, Anton Bikineev, Chromium-dev, cxx, Lei Zhang
FTR, I've converted all of //ios to use std::optional instead of absl::optional.
-- Sylvain

On Wednesday, 8 November 2023 at 16:15:37 UTC+1 Greg Thompson wrote:

danakj

unread,
Feb 26, 2024, 11:00:24 AMFeb 26
to Arthur Sonzogni, cxx, Sylvain Defresne, g...@chromium.org, Sylvain Defresne, Arthur Sonzogni, Daniel Cheng, Peter Boström, David Benjamin, Anton Bikineev, Chromium-dev, Lei Zhang
Thanks Arthur, you put a lot of time and effort into this migration. And thanks to all the reviewers for the many CLs to let this get done.

On Mon, Feb 26, 2024 at 10:56 AM Arthur Sonzogni <arthurs...@chromium.org> wrote:
The rewrite is now 100% completed! 🎉🎉🎉

This was about 150k lines change over 20k files. 
absl::optional is now banned via a PRESUBMIT.py rule.

From the chromium repository, the following directories have been excluded.
  • third_party/abseil-cpp/*
  • third_party/googletest/*
  • third_party/leveldatabase/*
  • third_party/libaddressinput/
  • third_party/liburlpattern/*
  • third_party/lzma_sdk/*
  • third_party/maldoca/*
  • third_party/mediapipe/*
  • third_party/shell-encryption/
  • third_party/tflite_support/*
  • third_party/webrtc_overrides/*
  • third_party/zxcvbn-cpp/*

Arthur Sonzogni

unread,
Feb 26, 2024, 10:56:28 PMFeb 26
to cxx, Sylvain Defresne, g...@chromium.org, Sylvain Defresne, danakj, Arthur Sonzogni, Daniel Cheng, Peter Boström, David Benjamin, Anton Bikineev, Chromium-dev, cxx, Lei Zhang
The rewrite is now 100% completed! 🎉🎉🎉

This was about 150k lines change over 20k files. 
absl::optional is now banned via a PRESUBMIT.py rule.

From the chromium repository, the following directories have been excluded.
  • third_party/abseil-cpp/*
  • third_party/googletest/*
  • third_party/leveldatabase/*
  • third_party/libaddressinput/
  • third_party/liburlpattern/*
  • third_party/lzma_sdk/*
  • third_party/maldoca/*
  • third_party/mediapipe/*
  • third_party/shell-encryption/
  • third_party/tflite_support/*
  • third_party/webrtc_overrides/*
  • third_party/zxcvbn-cpp/*
On Tuesday, November 14, 2023 at 7:26:46 PM UTC+1 Sylvain Defresne wrote:

K. Moon

unread,
Feb 27, 2024, 7:46:39 AMFeb 27
to danakj, Arthur Sonzogni, cxx, Sylvain Defresne, g...@chromium.org, Sylvain Defresne, Arthur Sonzogni, Daniel Cheng, Peter Boström, David Benjamin, Anton Bikineev, Chromium-dev, Lei Zhang
Congrats and thank you! Would be interested to hear more about how the experience of making this change went, and what could be improved for next time.

Arthur Sonzogni

unread,
Feb 27, 2024, 11:00:41 AMFeb 27
to K. Moon, Nico Weber, Takuto Ikuta, danakj, Arthur Sonzogni, cxx, Sylvain Defresne, g...@chromium.org, Sylvain Defresne, Daniel Cheng, Peter Boström, David Benjamin, Anton Bikineev, Chromium-dev, Lei Zhang
Congrats and thank you! Would be interested to hear more about how the experience of making this change went, and what could be improved for next time.

Good idea! Here is a a public doc about it.

Arthur @arthursonzogni


Peter Kasting

unread,
Mar 11, 2024, 10:08:53 PMMar 11
to Arthur Sonzogni, K. Moon, Nico Weber, Takuto Ikuta, danakj, Arthur Sonzogni, cxx, Sylvain Defresne, g...@chromium.org, Sylvain Defresne, Daniel Cheng, Peter Boström, David Benjamin, Anton Bikineev, Chromium-dev, Lei Zhang
On Tue, Feb 27, 2024 at 8:00 AM 'Arthur Sonzogni' via cxx <c...@chromium.org> wrote:
Congrats and thank you! Would be interested to hear more about how the experience of making this change went, and what could be improved for next time.

Good idea! Here is a a public doc about it.


Thanks for writing this. Some of my own takeaways:
* We should try to switch clang-format to regroup include blocks
* ...but when we do we should also modify the include categories to put windows.h alone
* IWYU fixes would help
* All LSCs like this should use OO+1 aggressively to get things landed immediately
* Predator is kinda dumb and harasses people with unrelated crashes when they touch stuff. No immediate fix in sight tho :(

PK 
Reply all
Reply to author
Forward
0 new messages