Skip to content

Commit 569a98e

Browse files
anforowiczcopybara-github
authored andcommitted
Recognize Iterator trait via DefId, not def path.
In Chromium `tcx.def_path_str` may return `core::iter::Iterator` and fail the old checks. The `DefId`-based checks should be more robust. PiperOrigin-RevId: 916314962
1 parent 14e96ba commit 569a98e

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

  • cc_bindings_from_rs/generate_bindings

cc_bindings_from_rs/generate_bindings/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,7 @@ fn generate_const<'tcx>(db: &BindingsGenerator<'tcx>, def_id: DefId) -> Result<A
10171017
// Implementation of `BindingsGenerator::supported_traits`.
10181018
fn supported_traits(db: &BindingsGenerator<'_>) -> Rc<[DefId]> {
10191019
let tcx = db.tcx();
1020+
let iterator_trait_id = tcx.get_diagnostic_item(sym::Iterator);
10201021

10211022
let traits = tcx
10221023
.visible_traits()
@@ -1038,11 +1039,14 @@ fn supported_traits(db: &BindingsGenerator<'_>) -> Rc<[DefId]> {
10381039
// At least for _most_ other traits - we probably still want to exclude traits that
10391040
// get idiomatic C++ bindings elsewhere, such as `Clone`, `Default`, `Drop`, `From`,
10401041
// `Index`, and `Into`.
1041-
let crate_name = tcx.crate_name(trait_id.krate);
1042-
let not_in_stdlib = crate_name.as_str() != "std"
1043-
&& crate_name.as_str() != "core"
1044-
&& crate_name.as_str() != "alloc";
1045-
not_in_stdlib || tcx.def_path_str(*trait_id) == "std::iter::Iterator"
1042+
let not_in_stdlib = {
1043+
let crate_name = tcx.crate_name(trait_id.krate);
1044+
crate_name.as_str() != "std"
1045+
&& crate_name.as_str() != "core"
1046+
&& crate_name.as_str() != "alloc"
1047+
};
1048+
let is_iterator_trait = iterator_trait_id == Some(*trait_id);
1049+
not_in_stdlib || is_iterator_trait
10461050
})
10471051
.collect::<Vec<DefId>>()
10481052
.into_boxed_slice();

0 commit comments

Comments
 (0)