From a6446c53fe8be6692b07e121b831c3db770ff94a Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Tue, 7 Mar 2023 10:06:42 -0700 Subject: [PATCH 1/2] rustdoc: fix type search index for `fn() -> &T where T: Trait` --- src/librustdoc/html/render/search_index.rs | 7 ++++++- tests/rustdoc-js/where-clause.js | 7 ++++++- tests/rustdoc-js/where-clause.rs | 6 ++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index e22ac6ec19b00..483137f4c3561 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -465,7 +465,7 @@ fn add_generics_and_bounds_as_types<'tcx, 'a>( } // First, check if it's "Self". - let arg = if let Some(self_) = self_ { + let mut arg = if let Some(self_) = self_ { match &*arg { Type::BorrowedRef { type_, .. } if type_.is_self_type() => self_, type_ if type_.is_self_type() => self_, @@ -475,6 +475,11 @@ fn add_generics_and_bounds_as_types<'tcx, 'a>( arg }; + // strip references from the argument type + while let Type::BorrowedRef { type_, .. } = &*arg { + arg = &*type_; + } + // If this argument is a type parameter and not a trait bound or a type, we need to look // for its bounds. if let Type::Generic(arg_s) = *arg { diff --git a/tests/rustdoc-js/where-clause.js b/tests/rustdoc-js/where-clause.js index 6cb42a455a369..4112f08fb0a5f 100644 --- a/tests/rustdoc-js/where-clause.js +++ b/tests/rustdoc-js/where-clause.js @@ -1,4 +1,4 @@ -const QUERY = ['trait', '-> trait', 't1, t2']; +const QUERY = ['trait', '-> trait', 't1, t2', '-> shazam']; const EXPECTED = [ { @@ -16,4 +16,9 @@ const EXPECTED = [ { 'path': 'where_clause', 'name': 'presto' }, ], }, + { + 'others': [ + { 'path': 'where_clause', 'name': 'bippety' }, + ], + }, ]; diff --git a/tests/rustdoc-js/where-clause.rs b/tests/rustdoc-js/where-clause.rs index 808561feee227..f8bdc072216e6 100644 --- a/tests/rustdoc-js/where-clause.rs +++ b/tests/rustdoc-js/where-clause.rs @@ -14,3 +14,9 @@ pub trait T2<'a, T> { } pub fn presto(_: A, _: B) where A: T1, B: for <'b> T2<'b, Nested> {} + +pub trait Shazam {} + +pub fn bippety() -> &'static X where X: Shazam { + panic!() +} From 44813e038c62ac975f4efaf8fb588949d24528af Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Tue, 7 Mar 2023 11:21:12 -0700 Subject: [PATCH 2/2] rustdoc: fix type search when more than one `where` clause applies --- src/librustdoc/html/render/search_index.rs | 2 +- .../rustdoc-js-std/option-type-signatures.js | 23 ++++++++++++++----- tests/rustdoc-js/where-clause.js | 8 ++++++- tests/rustdoc-js/where-clause.rs | 8 +++++++ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index 483137f4c3561..84213182d0ac1 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -484,7 +484,7 @@ fn add_generics_and_bounds_as_types<'tcx, 'a>( // for its bounds. if let Type::Generic(arg_s) = *arg { // First we check if the bounds are in a `where` predicate... - if let Some(where_pred) = generics.where_predicates.iter().find(|g| match g { + for where_pred in generics.where_predicates.iter().filter(|g| match g { WherePredicate::BoundPredicate { ty: Type::Generic(ty_s), .. } => *ty_s == arg_s, _ => false, }) { diff --git a/tests/rustdoc-js-std/option-type-signatures.js b/tests/rustdoc-js-std/option-type-signatures.js index dee4819e81a9f..6bf421a213560 100644 --- a/tests/rustdoc-js-std/option-type-signatures.js +++ b/tests/rustdoc-js-std/option-type-signatures.js @@ -1,7 +1,18 @@ -const QUERY = 'option, fnonce -> option'; +const QUERY = [ + 'option, fnonce -> option', + 'option -> default', +]; -const EXPECTED = { - 'others': [ - { 'path': 'std::option::Option', 'name': 'map' }, - ], -}; +const EXPECTED = [ + { + 'others': [ + { 'path': 'std::option::Option', 'name': 'map' }, + ], + }, + { + 'others': [ + { 'path': 'std::option::Option', 'name': 'unwrap_or_default' }, + { 'path': 'std::option::Option', 'name': 'get_or_insert_default' }, + ], + }, +]; diff --git a/tests/rustdoc-js/where-clause.js b/tests/rustdoc-js/where-clause.js index 4112f08fb0a5f..86254a80e20f3 100644 --- a/tests/rustdoc-js/where-clause.js +++ b/tests/rustdoc-js/where-clause.js @@ -1,4 +1,4 @@ -const QUERY = ['trait', '-> trait', 't1, t2', '-> shazam']; +const QUERY = ['trait', '-> trait', 't1, t2', '-> shazam', 'drizzel -> shazam']; const EXPECTED = [ { @@ -19,6 +19,12 @@ const EXPECTED = [ { 'others': [ { 'path': 'where_clause', 'name': 'bippety' }, + { 'path': 'where_clause::Drizzel', 'name': 'boppety' }, + ], + }, + { + 'others': [ + { 'path': 'where_clause::Drizzel', 'name': 'boppety' }, ], }, ]; diff --git a/tests/rustdoc-js/where-clause.rs b/tests/rustdoc-js/where-clause.rs index f8bdc072216e6..56c01019fb69f 100644 --- a/tests/rustdoc-js/where-clause.rs +++ b/tests/rustdoc-js/where-clause.rs @@ -20,3 +20,11 @@ pub trait Shazam {} pub fn bippety() -> &'static X where X: Shazam { panic!() } + +pub struct Drizzel(T); + +impl Drizzel { + pub fn boppety(&self) -> &T where T: Shazam { + panic!(); + } +}