Skip to content

Commit f32d298

Browse files
committed
Auto merge of rust-lang#118605 - fee1-dead-contrib:rm-rustc_host, r=compiler-errors
Remove `#[rustc_host]`, use internal desugaring Also removed a way for users to explicitly specify the host param since that isn't particularly useful. This should eliminate any pain with encoding attributes across crates and etc. r? `@compiler-errors`
2 parents 6316ac8 + 65212a0 commit f32d298

File tree

16 files changed

+56
-88
lines changed

16 files changed

+56
-88
lines changed

compiler/rustc_ast_lowering/src/item.rs

+16-39
Original file line numberDiff line numberDiff line change
@@ -565,11 +565,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
565565
.params
566566
.iter()
567567
.find(|param| {
568-
parent_hir
569-
.attrs
570-
.get(param.hir_id.local_id)
571-
.iter()
572-
.any(|attr| attr.has_name(sym::rustc_host))
568+
matches!(
569+
param.kind,
570+
hir::GenericParamKind::Const { is_host_effect: true, .. }
571+
)
573572
})
574573
.map(|param| param.def_id);
575574
}
@@ -1372,27 +1371,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
13721371
let host_param_parts = if let Const::Yes(span) = constness
13731372
&& self.tcx.features().effects
13741373
{
1375-
if let Some(param) =
1376-
generics.params.iter().find(|x| x.attrs.iter().any(|x| x.has_name(sym::rustc_host)))
1377-
{
1378-
// user has manually specified a `rustc_host` param, in this case, we set
1379-
// the param id so that lowering logic can use that. But we don't create
1380-
// another host param, so this gives `None`.
1381-
self.host_param_id = Some(self.local_def_id(param.id));
1382-
None
1383-
} else {
1384-
let param_node_id = self.next_node_id();
1385-
let hir_id = self.next_id();
1386-
let def_id = self.create_def(
1387-
self.local_def_id(parent_node_id),
1388-
param_node_id,
1389-
sym::host,
1390-
DefKind::ConstParam,
1391-
span,
1392-
);
1393-
self.host_param_id = Some(def_id);
1394-
Some((span, hir_id, def_id))
1395-
}
1374+
let param_node_id = self.next_node_id();
1375+
let hir_id = self.next_id();
1376+
let def_id = self.create_def(
1377+
self.local_def_id(parent_node_id),
1378+
param_node_id,
1379+
sym::host,
1380+
DefKind::ConstParam,
1381+
span,
1382+
);
1383+
self.host_param_id = Some(def_id);
1384+
Some((span, hir_id, def_id))
13961385
} else {
13971386
None
13981387
};
@@ -1456,19 +1445,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
14561445
self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
14571446
self.children.push((anon_const, hir::MaybeOwner::NonOwner(const_id)));
14581447

1459-
let attr_id = self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id();
1460-
1461-
let attrs = self.arena.alloc_from_iter([Attribute {
1462-
kind: AttrKind::Normal(P(NormalAttr::from_ident(Ident::new(
1463-
sym::rustc_host,
1464-
span,
1465-
)))),
1466-
span,
1467-
id: attr_id,
1468-
style: AttrStyle::Outer,
1469-
}]);
1470-
self.attrs.insert(hir_id.local_id, attrs);
1471-
14721448
let const_body = self.lower_body(|this| {
14731449
(
14741450
&[],
@@ -1510,6 +1486,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15101486
hir_id: const_id,
15111487
body: const_body,
15121488
}),
1489+
is_host_effect: true,
15131490
},
15141491
colon_span: None,
15151492
pure_wrt_drop: false,

compiler/rustc_ast_lowering/src/lib.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -2122,7 +2122,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21222122
let default = default.as_ref().map(|def| self.lower_anon_const(def));
21232123
(
21242124
hir::ParamName::Plain(self.lower_ident(param.ident)),
2125-
hir::GenericParamKind::Const { ty, default },
2125+
hir::GenericParamKind::Const { ty, default, is_host_effect: false },
21262126
)
21272127
}
21282128
}
@@ -2550,22 +2550,14 @@ impl<'hir> GenericArgsCtor<'hir> {
25502550
})
25512551
});
25522552

2553-
let attr_id = lcx.tcx.sess.parse_sess.attr_id_generator.mk_attr_id();
2554-
let attr = lcx.arena.alloc(Attribute {
2555-
kind: AttrKind::Normal(P(NormalAttr::from_ident(Ident::new(sym::rustc_host, span)))),
2556-
span,
2557-
id: attr_id,
2558-
style: AttrStyle::Outer,
2559-
});
2560-
lcx.attrs.insert(hir_id.local_id, std::slice::from_ref(attr));
2561-
25622553
let def_id = lcx.create_def(
25632554
lcx.current_hir_id_owner.def_id,
25642555
id,
25652556
kw::Empty,
25662557
DefKind::AnonConst,
25672558
span,
25682559
);
2560+
25692561
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
25702562
self.args.push(hir::GenericArg::Const(hir::ConstArg {
25712563
value: hir::AnonConst { def_id, hir_id, body },

compiler/rustc_feature/src/builtin_attrs.rs

-6
Original file line numberDiff line numberDiff line change
@@ -719,12 +719,6 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
719719
and it is only intended to be used in `alloc`."
720720
),
721721

722-
rustc_attr!(
723-
rustc_host, AttributeType::Normal, template!(Word), ErrorFollowing,
724-
"#[rustc_host] annotates const generic parameters as the `host` effect param, \
725-
and it is only intended for internal use and as a desugaring."
726-
),
727-
728722
BuiltinAttribute {
729723
name: sym::rustc_diagnostic_item,
730724
// FIXME: This can be `true` once we always use `tcx.is_diagnostic_item`.

compiler/rustc_hir/src/hir.rs

+1
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ pub enum GenericParamKind<'hir> {
487487
ty: &'hir Ty<'hir>,
488488
/// Optional default value for the const generic param
489489
default: Option<AnonConst>,
490+
is_host_effect: bool,
490491
},
491492
}
492493

compiler/rustc_hir/src/intravisit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Generi
874874
match param.kind {
875875
GenericParamKind::Lifetime { .. } => {}
876876
GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
877-
GenericParamKind::Const { ref ty, ref default } => {
877+
GenericParamKind::Const { ref ty, ref default, is_host_effect: _ } => {
878878
visitor.visit_ty(ty);
879879
if let Some(ref default) = default {
880880
visitor.visit_const_param_default(param.hir_id, default);

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
859859
hir::GenericParamKind::Lifetime { .. } | hir::GenericParamKind::Type { .. } => Ok(()),
860860

861861
// Const parameters are well formed if their type is structural match.
862-
hir::GenericParamKind::Const { ty: hir_ty, default: _ } => {
862+
hir::GenericParamKind::Const { ty: hir_ty, default: _, is_host_effect: _ } => {
863863
let ty = tcx.type_of(param.def_id).instantiate_identity();
864864

865865
if tcx.features().adt_const_params {

compiler/rustc_hir_analysis/src/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,7 @@ fn impl_trait_ref(
13831383
let last_segment = path_segments.len() - 1;
13841384
let mut args = *path_segments[last_segment].args();
13851385
let last_arg = args.args.len() - 1;
1386-
assert!(matches!(args.args[last_arg], hir::GenericArg::Const(anon_const) if tcx.has_attr(anon_const.value.def_id, sym::rustc_host)));
1386+
assert!(matches!(args.args[last_arg], hir::GenericArg::Const(anon_const) if anon_const.is_desugared_from_effects));
13871387
args.args = &args.args[..args.args.len() - 1];
13881388
path_segments[last_segment].args = Some(&args);
13891389
let path = hir::Path {

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir::def_id::LocalDefId;
99
use rustc_middle::ty::{self, TyCtxt};
1010
use rustc_session::lint;
1111
use rustc_span::symbol::{kw, Symbol};
12-
use rustc_span::{sym, Span};
12+
use rustc_span::Span;
1313

1414
pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
1515
use rustc_hir::*;
@@ -298,13 +298,11 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
298298
kind,
299299
})
300300
}
301-
GenericParamKind::Const { default, .. } => {
302-
let is_host_param = tcx.has_attr(param.def_id, sym::rustc_host);
303-
301+
GenericParamKind::Const { ty: _, default, is_host_effect } => {
304302
if !matches!(allow_defaults, Defaults::Allowed)
305303
&& default.is_some()
306-
// `rustc_host` effect params are allowed to have defaults.
307-
&& !is_host_param
304+
// `host` effect params are allowed to have defaults.
305+
&& !is_host_effect
308306
{
309307
tcx.sess.span_err(
310308
param.span,
@@ -315,7 +313,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
315313

316314
let index = next_index();
317315

318-
if is_host_param {
316+
if is_host_effect {
319317
if let Some(idx) = host_effect_index {
320318
bug!("parent also has host effect param? index: {idx}, def: {def_id:?}");
321319
}
@@ -330,7 +328,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
330328
pure_wrt_drop: param.pure_wrt_drop,
331329
kind: ty::GenericParamDefKind::Const {
332330
has_default: default.is_some(),
333-
is_host_effect: is_host_param,
331+
is_host_effect,
334332
},
335333
})
336334
}
@@ -489,7 +487,7 @@ struct AnonConstInParamTyDetector {
489487

490488
impl<'v> Visitor<'v> for AnonConstInParamTyDetector {
491489
fn visit_generic_param(&mut self, p: &'v hir::GenericParam<'v>) {
492-
if let GenericParamKind::Const { ty, default: _ } = p.kind {
490+
if let GenericParamKind::Const { ty, default: _, is_host_effect: _ } = p.kind {
493491
let prev = self.in_param_ty;
494492
self.in_param_ty = true;
495493
self.visit_ty(ty);

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
992992
self.visit_ty(ty);
993993
}
994994
}
995-
GenericParamKind::Const { ty, default } => {
995+
GenericParamKind::Const { ty, default, is_host_effect: _ } => {
996996
self.visit_ty(ty);
997997
if let Some(default) = default {
998998
self.visit_body(self.tcx.hir().body(default.body));

compiler/rustc_hir_pretty/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,7 @@ impl<'a> State<'a> {
21262126
self.print_type(default);
21272127
}
21282128
}
2129-
GenericParamKind::Const { ty, ref default } => {
2129+
GenericParamKind::Const { ty, ref default, is_host_effect: _ } => {
21302130
self.word_space(":");
21312131
self.print_type(ty);
21322132
if let Some(default) = default {

compiler/rustc_lint/src/nonstandard_style.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,9 @@ impl<'tcx> LateLintPass<'tcx> for NonUpperCaseGlobals {
534534
}
535535

536536
fn check_generic_param(&mut self, cx: &LateContext<'_>, param: &hir::GenericParam<'_>) {
537-
if let GenericParamKind::Const { .. } = param.kind {
538-
// `rustc_host` params are explicitly allowed to be lowercase.
539-
if cx.tcx.has_attr(param.def_id, sym::rustc_host) {
537+
if let GenericParamKind::Const { is_host_effect, .. } = param.kind {
538+
// `host` params are explicitly allowed to be lowercase.
539+
if is_host_effect {
540540
return;
541541
}
542542
NonUpperCaseGlobals::check_upper_case(cx, "const parameter", &param.name.ident());

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2186,7 +2186,7 @@ impl<'tcx> TyCtxt<'tcx> {
21862186
hir::Node::Item(hir::Item {
21872187
kind: hir::ItemKind::Impl(hir::Impl { generics, .. }),
21882188
..
2189-
}) if generics.params.iter().any(|p| self.has_attr(p.def_id, sym::rustc_host))
2189+
}) if generics.params.iter().any(|p| matches!(p.kind, hir::GenericParamKind::Const { is_host_effect: true, .. }))
21902190
)
21912191
}
21922192

compiler/rustc_passes/src/lang_items.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_hir::{LangItem, LanguageItems, Target};
2121
use rustc_middle::ty::TyCtxt;
2222
use rustc_session::cstore::ExternCrate;
2323
use rustc_span::symbol::kw::Empty;
24-
use rustc_span::{sym, Span};
24+
use rustc_span::Span;
2525

2626
use rustc_middle::query::Providers;
2727

@@ -162,7 +162,12 @@ impl<'tcx> LanguageItemCollector<'tcx> {
162162
generics
163163
.params
164164
.iter()
165-
.filter(|p| !self.tcx.has_attr(p.def_id, sym::rustc_host))
165+
.filter(|p| {
166+
!matches!(
167+
p.kind,
168+
hir::GenericParamKind::Const { is_host_effect: true, .. }
169+
)
170+
})
166171
.count(),
167172
generics.span,
168173
),

compiler/rustc_span/src/symbol.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,6 @@ symbols! {
13931393
rustc_expected_cgu_reuse,
13941394
rustc_has_incoherent_inherent_impls,
13951395
rustc_hidden_type_of_opaques,
1396-
rustc_host,
13971396
rustc_if_this_changed,
13981397
rustc_inherit_overflow_checks,
13991398
rustc_insignificant_dtor,

src/librustdoc/clean/mod.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -595,13 +595,13 @@ fn clean_generic_param<'tcx>(
595595
},
596596
)
597597
}
598-
hir::GenericParamKind::Const { ty, default } => (
598+
hir::GenericParamKind::Const { ty, default, is_host_effect } => (
599599
param.name.ident().name,
600600
GenericParamDefKind::Const {
601601
ty: Box::new(clean_ty(ty, cx)),
602602
default: default
603603
.map(|ct| Box::new(ty::Const::from_anon_const(cx.tcx, ct.def_id).to_string())),
604-
is_host_effect: cx.tcx.has_attr(param.def_id, sym::rustc_host),
604+
is_host_effect,
605605
},
606606
),
607607
};
@@ -2536,11 +2536,12 @@ fn clean_generic_args<'tcx>(
25362536
}
25372537
hir::GenericArg::Lifetime(_) => GenericArg::Lifetime(Lifetime::elided()),
25382538
hir::GenericArg::Type(ty) => GenericArg::Type(clean_ty(ty, cx)),
2539-
// Checking for `#[rustc_host]` on the `AnonConst` not only accounts for the case
2539+
// Checking for `is_desugared_from_effects` on the `AnonConst` not only accounts for the case
25402540
// where the argument is `host` but for all possible cases (e.g., `true`, `false`).
2541-
hir::GenericArg::Const(ct)
2542-
if cx.tcx.has_attr(ct.value.def_id, sym::rustc_host) =>
2543-
{
2541+
hir::GenericArg::Const(hir::ConstArg {
2542+
is_desugared_from_effects: true,
2543+
..
2544+
}) => {
25442545
return None;
25452546
}
25462547
hir::GenericArg::Const(ct) => GenericArg::Const(Box::new(clean_const(ct, cx))),

tests/ui/rfcs/rfc-2632-const-trait-impl/effects/helloworld.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
// gate-test-effects
44
// ^ effects doesn't have a gate so we will trick tidy into thinking this is a gate test
55

6-
#![feature(const_trait_impl, effects, rustc_attrs)]
6+
#![feature(const_trait_impl, effects, core_intrinsics, const_eval_select)]
77

88
// ensure we are passing in the correct host effect in always const contexts.
99

10-
pub const fn hmm<T, #[rustc_host] const host: bool = true>() -> usize {
11-
if host {
12-
1
13-
} else {
14-
0
10+
pub const fn hmm<T>() -> usize {
11+
// FIXME(const_trait_impl): maybe we should have a way to refer to the (hidden) effect param
12+
fn one() -> usize { 1 }
13+
const fn zero() -> usize { 0 }
14+
unsafe {
15+
std::intrinsics::const_eval_select((), zero, one)
1516
}
1617
}
1718

0 commit comments

Comments
 (0)