Skip to content

Commit 58c105a

Browse files
committed
include host_effect_index in Generics
1 parent 30b21b7 commit 58c105a

File tree

5 files changed

+34
-21
lines changed

5 files changed

+34
-21
lines changed

compiler/rustc_ast_lowering/src/item.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -417,14 +417,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
417417
}
418418
ItemKind::Trait(box Trait { is_auto, unsafety, generics, bounds, items }) => {
419419
// FIXME(const_trait_impl, effects, fee1-dead) this should be simplified if possible
420-
let constness = if let Some(attrs) = attrs {
421-
attrs
422-
.iter()
423-
.find(|x| x.has_name(sym::const_trait))
424-
.map_or(Const::No, |x| Const::Yes(x.span))
425-
} else {
426-
Const::No
427-
};
420+
let constness = attrs
421+
.unwrap_or(&[])
422+
.iter()
423+
.find(|x| x.has_name(sym::const_trait))
424+
.map_or(Const::No, |x| Const::Yes(x.span));
428425
let (generics, (unsafety, items, bounds)) = self.lower_generics(
429426
generics,
430427
constness,

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
101101
param_def_id_to_index,
102102
has_self: generics.has_self,
103103
has_late_bound_regions: generics.has_late_bound_regions,
104+
host_effect_index: None,
104105
};
105106
} else {
106107
// HACK(eddyb) this provides the correct generics when
@@ -226,10 +227,12 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
226227
let has_self = opt_self.is_some();
227228
let mut parent_has_self = false;
228229
let mut own_start = has_self as u32;
230+
let mut host_effect_index = None;
229231
let parent_count = parent_def_id.map_or(0, |def_id| {
230232
let generics = tcx.generics_of(def_id);
231233
assert!(!has_self);
232234
parent_has_self = generics.has_self;
235+
host_effect_index = generics.host_effect_index;
233236
own_start = generics.count() as u32;
234237
generics.parent_count + generics.params.len()
235238
});
@@ -251,11 +254,11 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
251254

252255
// Now create the real type and const parameters.
253256
let type_start = own_start - has_self as u32 + params.len() as u32;
254-
let mut i = 0;
257+
let mut i: u32 = 0;
255258
let mut next_index = || {
256259
let prev = i;
257260
i += 1;
258-
prev as u32 + type_start
261+
prev + type_start
259262
};
260263

261264
const TYPE_DEFAULT_NOT_ALLOWED: &'static str = "defaults for type parameters are only allowed in \
@@ -295,10 +298,12 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
295298
})
296299
}
297300
GenericParamKind::Const { default, .. } => {
298-
// `rustc_host` effect params are allowed to have defaults.
301+
let is_host_param = tcx.has_attr(param.def_id, sym::rustc_host);
302+
299303
if !matches!(allow_defaults, Defaults::Allowed)
300304
&& default.is_some()
301-
&& !tcx.has_attr(param.def_id, sym::rustc_host)
305+
// `rustc_host` effect params are allowed to have defaults.
306+
&& !is_host_param
302307
{
303308
tcx.sess.span_err(
304309
param.span,
@@ -307,8 +312,18 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
307312
);
308313
}
309314

315+
let index = next_index();
316+
317+
if is_host_param {
318+
if let Some(idx) = host_effect_index {
319+
bug!("parent also has host effect param? index: {idx}, def: {def_id:?}");
320+
}
321+
322+
host_effect_index = Some(parent_count + index as usize);
323+
}
324+
310325
Some(ty::GenericParamDef {
311-
index: next_index(),
326+
index,
312327
name: param.name.ident().name,
313328
def_id: param.def_id.to_def_id(),
314329
pure_wrt_drop: param.pure_wrt_drop,
@@ -360,6 +375,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
360375
param_def_id_to_index,
361376
has_self: has_self || parent_has_self,
362377
has_late_bound_regions: has_late_bound_regions(tcx, node),
378+
host_effect_index,
363379
}
364380
}
365381

compiler/rustc_hir_typeck/src/callee.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -777,16 +777,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
777777
None => tcx.consts.true_,
778778
};
779779

780-
let identity_substs = ty::InternalSubsts::identity_for_item(tcx, callee_did);
780+
let generics = tcx.generics_of(callee_did);
781781

782-
trace!(?effect, ?identity_substs, ?callee_substs);
782+
trace!(?effect, ?generics, ?callee_substs);
783783

784-
// FIXME this should be made more efficient
785-
let host_effect_param_index = identity_substs.iter().position(|x| {
786-
matches!(x.unpack(), ty::GenericArgKind::Const(const_) if matches!(const_.kind(), ty::ConstKind::Param(param) if param.name == sym::host))
787-
});
788-
789-
if let Some(idx) = host_effect_param_index {
784+
if let Some(idx) = generics.host_effect_index {
790785
let param = callee_substs.const_at(idx);
791786
let cause = self.misc(span);
792787
match self.at(&cause, self.param_env).eq(infer::DefineOpaqueTypes::No, effect, param) {

compiler/rustc_middle/src/ty/generics.rs

+3
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ pub struct Generics {
133133

134134
pub has_self: bool,
135135
pub has_late_bound_regions: Option<Span>,
136+
137+
// The index of the host effect when substituted. (i.e. might be index to parent substs)
138+
pub host_effect_index: Option<usize>,
136139
}
137140

138141
impl<'tcx> Generics {

compiler/rustc_ty_utils/src/assoc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ fn associated_type_for_impl_trait_in_trait(
337337
param_def_id_to_index,
338338
has_self: opaque_ty_generics.has_self,
339339
has_late_bound_regions: opaque_ty_generics.has_late_bound_regions,
340+
host_effect_index: parent_generics.host_effect_index,
340341
}
341342
});
342343

@@ -415,6 +416,7 @@ fn associated_type_for_impl_trait_in_impl(
415416
param_def_id_to_index,
416417
has_self: false,
417418
has_late_bound_regions: trait_assoc_generics.has_late_bound_regions,
419+
host_effect_index: parent_generics.host_effect_index,
418420
}
419421
});
420422

0 commit comments

Comments
 (0)