From 20363f40a938d05df68a775bacc9ca52c7490c7c Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 12 Oct 2023 15:55:23 +0000 Subject: [PATCH 1/6] Add regression tests --- tests/rustdoc/const-fn-effects.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/rustdoc/const-fn-effects.rs diff --git a/tests/rustdoc/const-fn-effects.rs b/tests/rustdoc/const-fn-effects.rs new file mode 100644 index 0000000000000..49b4cd874e6b0 --- /dev/null +++ b/tests/rustdoc/const-fn-effects.rs @@ -0,0 +1,19 @@ +#![crate_name = "foo"] +#![feature(effects)] + +// @has foo/fn.bar.html +// @has - '//pre[@class="rust item-decl"]' 'pub const fn bar() -> ' +/// foo +pub const fn bar() -> usize { + 2 +} + +// @has foo/struct.Foo.html +// @has - '//*[@class="method"]' 'const fn new()' +pub struct Foo(usize); + +impl Foo { + pub const fn new() -> Foo { + Foo(0) + } +} From c4e61faf2e078dc30b62488326404137600e5e11 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 12 Oct 2023 16:14:54 +0000 Subject: [PATCH 2/6] Hide host effect params from docs --- src/librustdoc/clean/mod.rs | 4 +++- src/librustdoc/clean/types.rs | 7 ++++--- src/librustdoc/html/format.rs | 3 +-- src/librustdoc/json/conversions.rs | 16 +++++++++------- tests/rustdoc/const-fn-effects.rs | 4 ++-- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index b9d7acee63c25..1f52afc002222 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -521,7 +521,7 @@ fn clean_generic_param_def<'tcx>( }, ) } - ty::GenericParamDefKind::Const { has_default, .. } => ( + ty::GenericParamDefKind::Const { has_default, is_host_effect } => ( def.name, GenericParamDefKind::Const { ty: Box::new(clean_middle_ty( @@ -541,6 +541,7 @@ fn clean_generic_param_def<'tcx>( )), false => None, }, + is_host_effect, }, ), }; @@ -597,6 +598,7 @@ fn clean_generic_param<'tcx>( ty: Box::new(clean_ty(ty, cx)), default: default .map(|ct| Box::new(ty::Const::from_anon_const(cx.tcx, ct.def_id).to_string())), + is_host_effect: cx.tcx.has_attr(param.def_id, sym::rustc_host), }, ), }; diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 48ce0a8944996..517a51867cfff 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1306,7 +1306,7 @@ impl WherePredicate { pub(crate) enum GenericParamDefKind { Lifetime { outlives: Vec }, Type { did: DefId, bounds: Vec, default: Option>, synthetic: bool }, - Const { ty: Box, default: Option> }, + Const { ty: Box, default: Option>, is_host_effect: bool }, } impl GenericParamDefKind { @@ -1326,9 +1326,10 @@ impl GenericParamDef { Self { name, kind: GenericParamDefKind::Lifetime { outlives: Vec::new() } } } - pub(crate) fn is_synthetic_type_param(&self) -> bool { + pub(crate) fn is_synthetic_param(&self) -> bool { match self.kind { - GenericParamDefKind::Lifetime { .. } | GenericParamDefKind::Const { .. } => false, + GenericParamDefKind::Lifetime { .. } => false, + GenericParamDefKind::Const { is_host_effect, .. } => is_host_effect, GenericParamDefKind::Type { synthetic, .. } => synthetic, } } diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 1983bb11e5b11..aa3f7184b4eb9 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -250,8 +250,7 @@ impl clean::Generics { cx: &'a Context<'tcx>, ) -> impl fmt::Display + 'a + Captures<'tcx> { display_fn(move |f| { - let mut real_params = - self.params.iter().filter(|p| !p.is_synthetic_type_param()).peekable(); + let mut real_params = self.params.iter().filter(|p| !p.is_synthetic_param()).peekable(); if real_params.peek().is_none() { return Ok(()); } diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index e7f782bb6a65e..1420d1086649d 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -453,7 +453,7 @@ impl FromWithTcx for GenericParamDefKind { default: default.map(|x| (*x).into_tcx(tcx)), synthetic, }, - Const { ty, default } => GenericParamDefKind::Const { + Const { ty, default, is_host_effect: _ } => GenericParamDefKind::Const { type_: (*ty).into_tcx(tcx), default: default.map(|x| *x), }, @@ -491,12 +491,14 @@ impl FromWithTcx for WherePredicate { default: default.map(|ty| (*ty).into_tcx(tcx)), synthetic, }, - clean::GenericParamDefKind::Const { ty, default } => { - GenericParamDefKind::Const { - type_: (*ty).into_tcx(tcx), - default: default.map(|d| *d), - } - } + clean::GenericParamDefKind::Const { + ty, + default, + is_host_effect: _, + } => GenericParamDefKind::Const { + type_: (*ty).into_tcx(tcx), + default: default.map(|d| *d), + }, }; GenericParamDef { name, kind } }) diff --git a/tests/rustdoc/const-fn-effects.rs b/tests/rustdoc/const-fn-effects.rs index 49b4cd874e6b0..7c19b4b2c0cf6 100644 --- a/tests/rustdoc/const-fn-effects.rs +++ b/tests/rustdoc/const-fn-effects.rs @@ -2,14 +2,14 @@ #![feature(effects)] // @has foo/fn.bar.html -// @has - '//pre[@class="rust item-decl"]' 'pub const fn bar() -> ' +// @has - '//pre[@class="rust item-decl"]' 'pub const fn bar() -> ' /// foo pub const fn bar() -> usize { 2 } // @has foo/struct.Foo.html -// @has - '//*[@class="method"]' 'const fn new()' +// @has - '//*[@class="method"]' 'const fn new()' pub struct Foo(usize); impl Foo { From 8f2af7e010945e6d57e839db2673e276e8bf6532 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 12 Oct 2023 16:44:37 +0000 Subject: [PATCH 3/6] Test cross crate --- tests/rustdoc/inline_cross/auxiliary/const-fn.rs | 5 +++++ tests/rustdoc/inline_cross/const-fn.rs | 10 ++++++++++ 2 files changed, 15 insertions(+) create mode 100644 tests/rustdoc/inline_cross/auxiliary/const-fn.rs create mode 100644 tests/rustdoc/inline_cross/const-fn.rs diff --git a/tests/rustdoc/inline_cross/auxiliary/const-fn.rs b/tests/rustdoc/inline_cross/auxiliary/const-fn.rs new file mode 100644 index 0000000000000..26332b419b6d3 --- /dev/null +++ b/tests/rustdoc/inline_cross/auxiliary/const-fn.rs @@ -0,0 +1,5 @@ +#![feature(effects)] + +pub const fn load() -> i32 { + 0 +} diff --git a/tests/rustdoc/inline_cross/const-fn.rs b/tests/rustdoc/inline_cross/const-fn.rs new file mode 100644 index 0000000000000..24934b873c25b --- /dev/null +++ b/tests/rustdoc/inline_cross/const-fn.rs @@ -0,0 +1,10 @@ +// Regression test for issue #116629. +// Check that we render the correct generic params of const fn + +// aux-crate:const_fn=const-fn.rs +// edition: 2021 +#![crate_name = "user"] + +// @has user/fn.load.html +// @has - '//pre[@class="rust item-decl"]' "pub const fn load() -> i32" +pub use const_fn::load; From cfb6afa2965a1504f366eb258193219c4a820141 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 12 Oct 2023 16:49:23 +0000 Subject: [PATCH 4/6] Add regression test for generic args showing `host` param --- tests/rustdoc/const-effect-param.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 tests/rustdoc/const-effect-param.rs diff --git a/tests/rustdoc/const-effect-param.rs b/tests/rustdoc/const-effect-param.rs new file mode 100644 index 0000000000000..b1155dd9d8f52 --- /dev/null +++ b/tests/rustdoc/const-effect-param.rs @@ -0,0 +1,12 @@ +#![crate_name = "foo"] +#![feature(effects, const_trait_impl)] + +#[const_trait] +pub trait Tr { + fn f(); +} + +// @has foo/fn.g.html +// @has - '//pre[@class="rust item-decl"]' 'pub const fn g>()' +/// foo +pub const fn g() {} From 6724f9926c9e8c2d37e3e68e44238897bd89fddf Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 12 Oct 2023 17:14:19 +0000 Subject: [PATCH 5/6] hide `host` param from generic parameter list of `~const` bounds --- src/librustdoc/clean/mod.rs | 23 +++++++++++++++-------- src/librustdoc/clean/utils.rs | 1 + src/librustdoc/lib.rs | 1 + tests/rustdoc/const-effect-param.rs | 2 +- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 1f52afc002222..0ee2232d1fe52 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2510,14 +2510,21 @@ fn clean_generic_args<'tcx>( let args = generic_args .args .iter() - .map(|arg| match arg { - hir::GenericArg::Lifetime(lt) if !lt.is_anonymous() => { - GenericArg::Lifetime(clean_lifetime(*lt, cx)) - } - hir::GenericArg::Lifetime(_) => GenericArg::Lifetime(Lifetime::elided()), - hir::GenericArg::Type(ty) => GenericArg::Type(clean_ty(ty, cx)), - hir::GenericArg::Const(ct) => GenericArg::Const(Box::new(clean_const(ct, cx))), - hir::GenericArg::Infer(_inf) => GenericArg::Infer, + .filter_map(|arg| { + Some(match arg { + hir::GenericArg::Lifetime(lt) if !lt.is_anonymous() => { + GenericArg::Lifetime(clean_lifetime(*lt, cx)) + } + hir::GenericArg::Lifetime(_) => GenericArg::Lifetime(Lifetime::elided()), + hir::GenericArg::Type(ty) => GenericArg::Type(clean_ty(ty, cx)), + hir::GenericArg::Const(ct) + if cx.tcx.has_attr(ct.value.def_id, sym::rustc_host) => + { + return None; + } + hir::GenericArg::Const(ct) => GenericArg::Const(Box::new(clean_const(ct, cx))), + hir::GenericArg::Infer(_inf) => GenericArg::Infer, + }) }) .collect::>() .into(); diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 01078504b713a..3b4f869dab4c9 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -104,6 +104,7 @@ pub(crate) fn ty_args_to_args<'tcx>( arg: index, }), ))), + GenericArgKind::Const(ct) if let ty::ConstKind::Param(p) = ct.kind() && p.name == sym::host => None, GenericArgKind::Const(ct) => { Some(GenericArg::Const(Box::new(clean_middle_const(kind.rebind(ct), cx)))) } diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index fc2acb6eaa358..67f5ea5d98b2a 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -6,6 +6,7 @@ #![feature(array_methods)] #![feature(assert_matches)] #![feature(box_patterns)] +#![feature(if_let_guard)] #![feature(impl_trait_in_assoc_type)] #![feature(iter_intersperse)] #![feature(lazy_cell)] diff --git a/tests/rustdoc/const-effect-param.rs b/tests/rustdoc/const-effect-param.rs index b1155dd9d8f52..f50a9b96d812b 100644 --- a/tests/rustdoc/const-effect-param.rs +++ b/tests/rustdoc/const-effect-param.rs @@ -7,6 +7,6 @@ pub trait Tr { } // @has foo/fn.g.html -// @has - '//pre[@class="rust item-decl"]' 'pub const fn g>()' +// @has - '//pre[@class="rust item-decl"]' 'pub const fn g()' /// foo pub const fn g() {} From 16f8396f6df6902eeef580396785ab8888c3718b Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 13 Oct 2023 11:04:01 +0000 Subject: [PATCH 6/6] Add some FIXMEs for remaining issues that we need to fix before using more const trait things in libcore --- src/librustdoc/clean/mod.rs | 1 + src/librustdoc/clean/utils.rs | 3 +++ 2 files changed, 4 insertions(+) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 0ee2232d1fe52..e08318e4f5427 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2517,6 +2517,7 @@ fn clean_generic_args<'tcx>( } hir::GenericArg::Lifetime(_) => GenericArg::Lifetime(Lifetime::elided()), hir::GenericArg::Type(ty) => GenericArg::Type(clean_ty(ty, cx)), + // FIXME(effects): This will still emit `` for non-const impls of const traits hir::GenericArg::Const(ct) if cx.tcx.has_attr(ct.value.def_id, sym::rustc_host) => { diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 3b4f869dab4c9..c5302570489d9 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -104,6 +104,9 @@ pub(crate) fn ty_args_to_args<'tcx>( arg: index, }), ))), + // FIXME(effects): this relies on the host effect being called `host`, which users could also name + // their const generics. + // FIXME(effects): this causes `host = true` and `host = false` generics to also be emitted. GenericArgKind::Const(ct) if let ty::ConstKind::Param(p) = ct.kind() && p.name == sym::host => None, GenericArgKind::Const(ct) => { Some(GenericArg::Const(Box::new(clean_middle_const(kind.rebind(ct), cx))))