Skip to content

Commit 009d72b

Browse files
Rollup merge of rust-lang#112853 - GuillaumeGomez:type_alias_type, r=oli-obk
Add `lazy_type_alias` feature gate Add the `type_alias_type` to be able to have the weak alias used without restrictions. Part of rust-lang#112792. cc `@compiler-errors` r? `@oli-obk`
2 parents e100df9 + 53761e1 commit 009d72b

File tree

8 files changed

+68
-5
lines changed

8 files changed

+68
-5
lines changed

compiler/rustc_feature/src/active.rs

+2
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ declare_features! (
442442
(active, intra_doc_pointers, "1.51.0", Some(80896), None),
443443
// Allows setting the threshold for the `large_assignments` lint.
444444
(active, large_assignments, "1.52.0", Some(83518), None),
445+
/// Allow to have type alias types for inter-crate use.
446+
(active, lazy_type_alias, "CURRENT_RUSTC_VERSION", Some(112792), None),
445447
/// Allows `if/while p && let q = r && ...` chains.
446448
(active, let_chains, "1.37.0", Some(53667), None),
447449
/// Allows using `reason` in lint attributes and the `#[expect(lint)]` lint check.

compiler/rustc_hir_analysis/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
896896
let ty = self.tcx().at(span).type_of(did);
897897

898898
if matches!(self.tcx().def_kind(did), DefKind::TyAlias)
899-
&& ty.skip_binder().has_opaque_types()
899+
&& (ty.skip_binder().has_opaque_types() || self.tcx().features().lazy_type_alias)
900900
{
901901
// Type aliases referring to types that contain opaque types (but aren't just directly
902902
// referencing a single opaque type) get encoded as a type alias that normalization will

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,7 @@ symbols! {
871871
large_assignments,
872872
lateout,
873873
lazy_normalization_consts,
874+
lazy_type_alias,
874875
le,
875876
len,
876877
let_chains,

src/librustdoc/clean/mod.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -2023,8 +2023,8 @@ pub(crate) fn clean_middle_ty<'tcx>(
20232023
Tuple(t.iter().map(|t| clean_middle_ty(bound_ty.rebind(t), cx, None, None)).collect())
20242024
}
20252025

2026-
ty::Alias(ty::Projection, ref data) => {
2027-
clean_projection(bound_ty.rebind(*data), cx, parent_def_id)
2026+
ty::Alias(ty::Projection, data) => {
2027+
clean_projection(bound_ty.rebind(data), cx, parent_def_id)
20282028
}
20292029

20302030
ty::Alias(ty::Inherent, alias_ty) => {
@@ -2052,8 +2052,21 @@ pub(crate) fn clean_middle_ty<'tcx>(
20522052
}
20532053

20542054
ty::Alias(ty::Weak, data) => {
2055-
let ty = cx.tcx.type_of(data.def_id).subst(cx.tcx, data.substs);
2056-
clean_middle_ty(bound_ty.rebind(ty), cx, None, None)
2055+
if cx.tcx.features().lazy_type_alias {
2056+
// Weak type alias `data` represents the `type X` in `type X = Y`. If we need `Y`,
2057+
// we need to use `type_of`.
2058+
let path = external_path(
2059+
cx,
2060+
data.def_id,
2061+
false,
2062+
ThinVec::new(),
2063+
bound_ty.rebind(data.substs),
2064+
);
2065+
Type::Path { path }
2066+
} else {
2067+
let ty = cx.tcx.type_of(data.def_id).subst(cx.tcx, data.substs);
2068+
clean_middle_ty(bound_ty.rebind(ty), cx, None, None)
2069+
}
20572070
}
20582071

20592072
ty::Param(ref p) => {

tests/rustdoc/alias-reexport.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// aux-build:alias-reexport.rs
2+
// aux-build:alias-reexport2.rs
3+
4+
#![crate_name = "foo"]
5+
#![feature(lazy_type_alias)]
6+
7+
extern crate alias_reexport2;
8+
9+
// @has 'foo/reexport/fn.foo.html'
10+
// @has - '//*[@class="rust item-decl"]' 'pub fn foo() -> Reexported'
11+
// @has 'foo/reexport/fn.foo2.html'
12+
// @has - '//*[@class="rust item-decl"]' 'pub fn foo2() -> Result<Reexported, ()>'
13+
// @has 'foo/reexport/type.Reexported.html'
14+
// @has - '//*[@class="rust item-decl"]' 'pub type Reexported = u8;'
15+
#[doc(inline)]
16+
pub use alias_reexport2 as reexport;

tests/rustdoc/alias-reexport2.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// gate-test-lazy_type_alias
2+
// aux-build:alias-reexport.rs
3+
4+
#![crate_name = "foo"]
5+
#![feature(lazy_type_alias)]
6+
7+
extern crate alias_reexport;
8+
9+
use alias_reexport::Reexported;
10+
11+
// @has 'foo/fn.foo.html'
12+
// @has - '//*[@class="rust item-decl"]' 'pub fn foo() -> Reexported'
13+
pub fn foo() -> Reexported { 0 }
14+
// @has 'foo/fn.foo2.html'
15+
// @has - '//*[@class="rust item-decl"]' 'pub fn foo2() -> Result<Reexported, ()>'
16+
pub fn foo2() -> Result<Reexported, ()> { Ok(0) }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![feature(lazy_type_alias)]
2+
3+
pub type Reexported = u8;
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(lazy_type_alias)]
2+
3+
extern crate alias_reexport;
4+
5+
pub use alias_reexport::Reexported;
6+
7+
// @has 'foo/fn.foo.html'
8+
// @has - '//*[@class="docblock item-decl"]' 'pub fn foo() -> Reexported'
9+
pub fn foo() -> Reexported { 0 }
10+
// @has 'foo/fn.foo2.html'
11+
// @has - '//*[@class="docblock item-decl"]' 'pub fn foo2() -> Result<Reexported, ()>'
12+
pub fn foo2() -> Result<Reexported, ()> { Ok(0) }

0 commit comments

Comments
 (0)