From 60ec8405eb03bb3da43e11a5415968d8d8852120 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 19 Jun 2023 13:50:22 +0200 Subject: [PATCH 1/3] Add `lazy_type_alias` feature gate --- compiler/rustc_feature/src/active.rs | 2 ++ compiler/rustc_hir_analysis/src/astconv/mod.rs | 2 +- compiler/rustc_span/src/symbol.rs | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_feature/src/active.rs b/compiler/rustc_feature/src/active.rs index 4c53f9d8369..34c0e5009d3 100644 --- a/compiler/rustc_feature/src/active.rs +++ b/compiler/rustc_feature/src/active.rs @@ -440,6 +440,8 @@ pub fn set(&self, features: &mut Features, span: Span) { (active, intra_doc_pointers, "1.51.0", Some(80896), None), // Allows setting the threshold for the `large_assignments` lint. (active, large_assignments, "1.52.0", Some(83518), None), + /// Allow to have type alias types for inter-crate use. + (active, lazy_type_alias, "CURRENT_RUSTC_VERSION", Some(112792), None), /// Allows `if/while p && let q = r && ...` chains. (active, let_chains, "1.37.0", Some(53667), None), /// Allows using `reason` in lint attributes and the `#[expect(lint)]` lint check. diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index 621569ab321..7a83447786c 100644 --- a/compiler/rustc_hir_analysis/src/astconv/mod.rs +++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs @@ -896,7 +896,7 @@ fn ast_path_to_ty( let ty = self.tcx().at(span).type_of(did); if matches!(self.tcx().def_kind(did), DefKind::TyAlias) - && ty.skip_binder().has_opaque_types() + && (ty.skip_binder().has_opaque_types() || self.tcx().features().lazy_type_alias) { // Type aliases referring to types that contain opaque types (but aren't just directly // referencing a single opaque type) get encoded as a type alias that normalization will diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 388b904d197..d8c04f72060 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -870,6 +870,7 @@ large_assignments, lateout, lazy_normalization_consts, + lazy_type_alias, le, len, let_chains, From 1af48beed74e9ab652a05c8fe267541ae652eaf6 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 19 Jun 2023 15:05:46 +0200 Subject: [PATCH 2/3] Add rustdoc tests for `lazy_type_alias` --- tests/rustdoc/alias-reexport.rs | 18 ++++++++++++++++++ tests/rustdoc/alias-reexport2.rs | 16 ++++++++++++++++ tests/rustdoc/auxiliary/alias-reexport.rs | 3 +++ tests/rustdoc/auxiliary/alias-reexport2.rs | 12 ++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 tests/rustdoc/alias-reexport.rs create mode 100644 tests/rustdoc/alias-reexport2.rs create mode 100644 tests/rustdoc/auxiliary/alias-reexport.rs create mode 100644 tests/rustdoc/auxiliary/alias-reexport2.rs diff --git a/tests/rustdoc/alias-reexport.rs b/tests/rustdoc/alias-reexport.rs new file mode 100644 index 00000000000..029891197fc --- /dev/null +++ b/tests/rustdoc/alias-reexport.rs @@ -0,0 +1,18 @@ +// aux-build:alias-reexport.rs +// aux-build:alias-reexport2.rs + +#![crate_name = "foo"] +#![feature(lazy_type_alias)] + +extern crate alias_reexport2; + +// @has 'foo/reexport/fn.foo.html' +// FIXME: should be 'pub fn foo() -> Reexport' +// @has - '//*[@class="rust item-decl"]' 'pub fn foo() -> u8' +// @has 'foo/reexport/fn.foo2.html' +// FIXME: should be 'pub fn foo2() -> Result' +// @has - '//*[@class="rust item-decl"]' 'pub fn foo2() -> Result' +// @has 'foo/reexport/type.Reexported.html' +// @has - '//*[@class="rust item-decl"]' 'pub type Reexported = u8;' +#[doc(inline)] +pub use alias_reexport2 as reexport; diff --git a/tests/rustdoc/alias-reexport2.rs b/tests/rustdoc/alias-reexport2.rs new file mode 100644 index 00000000000..85d3cdad962 --- /dev/null +++ b/tests/rustdoc/alias-reexport2.rs @@ -0,0 +1,16 @@ +// gate-test-lazy_type_alias +// aux-build:alias-reexport.rs + +#![crate_name = "foo"] +#![feature(lazy_type_alias)] + +extern crate alias_reexport; + +use alias_reexport::Reexported; + +// @has 'foo/fn.foo.html' +// @has - '//*[@class="rust item-decl"]' 'pub fn foo() -> Reexported' +pub fn foo() -> Reexported { 0 } +// @has 'foo/fn.foo2.html' +// @has - '//*[@class="rust item-decl"]' 'pub fn foo2() -> Result' +pub fn foo2() -> Result { Ok(0) } diff --git a/tests/rustdoc/auxiliary/alias-reexport.rs b/tests/rustdoc/auxiliary/alias-reexport.rs new file mode 100644 index 00000000000..14fafc02d36 --- /dev/null +++ b/tests/rustdoc/auxiliary/alias-reexport.rs @@ -0,0 +1,3 @@ +#![feature(lazy_type_alias)] + +pub type Reexported = u8; diff --git a/tests/rustdoc/auxiliary/alias-reexport2.rs b/tests/rustdoc/auxiliary/alias-reexport2.rs new file mode 100644 index 00000000000..9f6910572ad --- /dev/null +++ b/tests/rustdoc/auxiliary/alias-reexport2.rs @@ -0,0 +1,12 @@ +#![feature(lazy_type_alias)] + +extern crate alias_reexport; + +pub use alias_reexport::Reexported; + +// @has 'foo/fn.foo.html' +// @has - '//*[@class="docblock item-decl"]' 'pub fn foo() -> Reexported' +pub fn foo() -> Reexported { 0 } +// @has 'foo/fn.foo2.html' +// @has - '//*[@class="docblock item-decl"]' 'pub fn foo2() -> Result' +pub fn foo2() -> Result { Ok(0) } From 53761e12226b03e742ae3c4b6e0d7716ad38b18f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 21 Jun 2023 13:37:17 +0200 Subject: [PATCH 3/3] Correctly handle Weak type aliases in rustdoc --- src/librustdoc/clean/mod.rs | 21 +++++++++++++++++---- tests/rustdoc/alias-reexport.rs | 6 ++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 851002d5e79..67d433dd0ad 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2024,8 +2024,8 @@ pub(crate) fn clean_middle_ty<'tcx>( Tuple(t.iter().map(|t| clean_middle_ty(bound_ty.rebind(t), cx, None, None)).collect()) } - ty::Alias(ty::Projection, ref data) => { - clean_projection(bound_ty.rebind(*data), cx, parent_def_id) + ty::Alias(ty::Projection, data) => { + clean_projection(bound_ty.rebind(data), cx, parent_def_id) } ty::Alias(ty::Inherent, alias_ty) => { @@ -2053,8 +2053,21 @@ pub(crate) fn clean_middle_ty<'tcx>( } ty::Alias(ty::Weak, data) => { - let ty = cx.tcx.type_of(data.def_id).subst(cx.tcx, data.substs); - clean_middle_ty(bound_ty.rebind(ty), cx, None, None) + if cx.tcx.features().lazy_type_alias { + // Weak type alias `data` represents the `type X` in `type X = Y`. If we need `Y`, + // we need to use `type_of`. + let path = external_path( + cx, + data.def_id, + false, + ThinVec::new(), + bound_ty.rebind(data.substs), + ); + Type::Path { path } + } else { + let ty = cx.tcx.type_of(data.def_id).subst(cx.tcx, data.substs); + clean_middle_ty(bound_ty.rebind(ty), cx, None, None) + } } ty::Param(ref p) => { diff --git a/tests/rustdoc/alias-reexport.rs b/tests/rustdoc/alias-reexport.rs index 029891197fc..a2a8e651caf 100644 --- a/tests/rustdoc/alias-reexport.rs +++ b/tests/rustdoc/alias-reexport.rs @@ -7,11 +7,9 @@ extern crate alias_reexport2; // @has 'foo/reexport/fn.foo.html' -// FIXME: should be 'pub fn foo() -> Reexport' -// @has - '//*[@class="rust item-decl"]' 'pub fn foo() -> u8' +// @has - '//*[@class="rust item-decl"]' 'pub fn foo() -> Reexported' // @has 'foo/reexport/fn.foo2.html' -// FIXME: should be 'pub fn foo2() -> Result' -// @has - '//*[@class="rust item-decl"]' 'pub fn foo2() -> Result' +// @has - '//*[@class="rust item-decl"]' 'pub fn foo2() -> Result' // @has 'foo/reexport/type.Reexported.html' // @has - '//*[@class="rust item-decl"]' 'pub type Reexported = u8;' #[doc(inline)]