Skip to content

Commit d629824

Browse files
committed
Remove #[rustc_host], use internal desugaring
1 parent cf8d812 commit d629824

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
@@ -562,11 +562,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
562562
.params
563563
.iter()
564564
.find(|param| {
565-
parent_hir
566-
.attrs
567-
.get(param.hir_id.local_id)
568-
.iter()
569-
.any(|attr| attr.has_name(sym::rustc_host))
565+
matches!(
566+
param.kind,
567+
hir::GenericParamKind::Const { is_host_effect: true, .. }
568+
)
570569
})
571570
.map(|param| param.def_id);
572571
}
@@ -1353,27 +1352,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
13531352
let host_param_parts = if let Const::Yes(span) = constness
13541353
&& self.tcx.features().effects
13551354
{
1356-
if let Some(param) =
1357-
generics.params.iter().find(|x| x.attrs.iter().any(|x| x.has_name(sym::rustc_host)))
1358-
{
1359-
// user has manually specified a `rustc_host` param, in this case, we set
1360-
// the param id so that lowering logic can use that. But we don't create
1361-
// another host param, so this gives `None`.
1362-
self.host_param_id = Some(self.local_def_id(param.id));
1363-
None
1364-
} else {
1365-
let param_node_id = self.next_node_id();
1366-
let hir_id = self.next_id();
1367-
let def_id = self.create_def(
1368-
self.local_def_id(parent_node_id),
1369-
param_node_id,
1370-
DefPathData::TypeNs(sym::host),
1371-
DefKind::ConstParam,
1372-
span,
1373-
);
1374-
self.host_param_id = Some(def_id);
1375-
Some((span, hir_id, def_id))
1376-
}
1355+
let param_node_id = self.next_node_id();
1356+
let hir_id = self.next_id();
1357+
let def_id = self.create_def(
1358+
self.local_def_id(parent_node_id),
1359+
param_node_id,
1360+
DefPathData::TypeNs(sym::host),
1361+
DefKind::ConstParam,
1362+
span,
1363+
);
1364+
self.host_param_id = Some(def_id);
1365+
Some((span, hir_id, def_id))
13771366
} else {
13781367
None
13791368
};
@@ -1442,19 +1431,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
14421431
self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
14431432
self.children.push((anon_const, hir::MaybeOwner::NonOwner(const_id)));
14441433

1445-
let attr_id = self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id();
1446-
1447-
let attrs = self.arena.alloc_from_iter([Attribute {
1448-
kind: AttrKind::Normal(P(NormalAttr::from_ident(Ident::new(
1449-
sym::rustc_host,
1450-
span,
1451-
)))),
1452-
span,
1453-
id: attr_id,
1454-
style: AttrStyle::Outer,
1455-
}]);
1456-
self.attrs.insert(hir_id.local_id, attrs);
1457-
14581434
let const_body = self.lower_body(|this| {
14591435
(
14601436
&[],
@@ -1496,6 +1472,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14961472
hir_id: const_id,
14971473
body: const_body,
14981474
}),
1475+
is_host_effect: true,
14991476
},
15001477
colon_span: None,
15011478
pure_wrt_drop: false,

compiler/rustc_ast_lowering/src/lib.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -2109,7 +2109,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21092109
let default = default.as_ref().map(|def| self.lower_anon_const(def));
21102110
(
21112111
hir::ParamName::Plain(self.lower_ident(param.ident)),
2112-
hir::GenericParamKind::Const { ty, default },
2112+
hir::GenericParamKind::Const { ty, default, is_host_effect: false },
21132113
)
21142114
}
21152115
}
@@ -2537,22 +2537,14 @@ impl<'hir> GenericArgsCtor<'hir> {
25372537
})
25382538
});
25392539

2540-
let attr_id = lcx.tcx.sess.parse_sess.attr_id_generator.mk_attr_id();
2541-
let attr = lcx.arena.alloc(Attribute {
2542-
kind: AttrKind::Normal(P(NormalAttr::from_ident(Ident::new(sym::rustc_host, span)))),
2543-
span,
2544-
id: attr_id,
2545-
style: AttrStyle::Outer,
2546-
});
2547-
lcx.attrs.insert(hir_id.local_id, std::slice::from_ref(attr));
2548-
25492540
let def_id = lcx.create_def(
25502541
lcx.current_hir_id_owner.def_id,
25512542
id,
25522543
DefPathData::AnonConst,
25532544
DefKind::AnonConst,
25542545
span,
25552546
);
2547+
25562548
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
25572549
self.args.push(hir::GenericArg::Const(hir::ConstArg {
25582550
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
@@ -2185,7 +2185,7 @@ impl<'tcx> TyCtxt<'tcx> {
21852185
hir::Node::Item(hir::Item {
21862186
kind: hir::ItemKind::Impl(hir::Impl { generics, .. }),
21872187
..
2188-
}) if generics.params.iter().any(|p| self.has_attr(p.def_id, sym::rustc_host))
2188+
}) if generics.params.iter().any(|p| matches!(p.kind, hir::GenericParamKind::Const { is_host_effect: true, .. }))
21892189
)
21902190
}
21912191

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)