Skip to content

Commit fa74afd

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

File tree

16 files changed

+51
-84
lines changed

16 files changed

+51
-84
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 16 additions & 39 deletions
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

Lines changed: 2 additions & 10 deletions
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

Lines changed: 0 additions & 6 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 9 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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 {

0 commit comments

Comments
 (0)