Skip to content

Commit f3996f6

Browse files
committed
review
1 parent bc0156b commit f3996f6

File tree

21 files changed

+62
-40
lines changed

21 files changed

+62
-40
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
499499
ty::Adt(def, ..) if !def.is_box() => {
500500
// Again, only create type information if full debuginfo is enabled
501501
if cx.sess().opts.debuginfo == DebugInfo::Full
502-
&& !impl_self_ty.needs_subst(cx.tcx)
502+
&& !impl_self_ty.definitely_needs_subst(cx.tcx)
503503
{
504504
Some(type_metadata(cx, impl_self_ty, rustc_span::DUMMY_SP))
505505
} else {

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
470470
{
471471
let needs_canonical_flags = if canonicalize_region_mode.any() {
472472
TypeFlags::NEEDS_INFER |
473-
TypeFlags::HAS_POTENTIAL_FREE_REGIONS | // `HAS_RE_PLACEHOLDER` implies `HAS_xxx_FREE_REGIONS`
473+
TypeFlags::HAS_POTENTIAL_FREE_REGIONS | // `HAS_RE_PLACEHOLDER` implies `HAS_POTENTIAL_FREE_REGIONS`
474474
TypeFlags::HAS_TY_PLACEHOLDER |
475475
TypeFlags::HAS_CT_PLACEHOLDER
476476
} else {

compiler/rustc_lint/src/noop_method_call.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
6262
_ => return,
6363
};
6464
let substs = cx.typeck_results().node_substs(expr.hir_id);
65-
if substs.needs_subst(cx.tcx) {
65+
if substs.definitely_needs_subst(cx.tcx) {
6666
// We can't resolve on types that require monomorphization, so we don't handle them if
6767
// we need to perfom substitution.
6868
return;

compiler/rustc_middle/src/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl<'tcx> Body<'tcx> {
285285
predecessor_cache: PredecessorCache::new(),
286286
is_cyclic: GraphIsCyclicCache::new(),
287287
};
288-
body.is_polymorphic = body.has_param_types_or_consts(tcx);
288+
body.is_polymorphic = body.definitely_has_param_types_or_consts(tcx);
289289
body
290290
}
291291

compiler/rustc_middle/src/ty/flags.rs

+11
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,20 @@ impl FlagComputation {
311311
}
312312

313313
fn add_unevaluated_const<P>(&mut self, ct: ty::Unevaluated<'tcx, P>) {
314+
// The generic arguments of unevaluated consts are a bit special,
315+
// see the `rustc-dev-guide` for more information.
316+
//
317+
// FIXME(@lcnr): Actually add a link here.
314318
if let Some(substs) = ct.substs_ {
319+
// If they are available, we treat them as ordinary generic arguments.
315320
self.add_substs(substs);
316321
} else {
322+
// Otherwise, we add `HAS_UNKNOWN_DEFAULT_CONST_SUBSTS` to signify
323+
// that our const may potentially refer to generic parameters.
324+
//
325+
// Note that depending on which generic parameters are actually
326+
// used in this constant, we may not actually refer to any generic
327+
// parameters at all.
317328
self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
318329
self.add_flags(TypeFlags::HAS_UNKNOWN_DEFAULT_CONST_SUBSTS);
319330
}

compiler/rustc_middle/src/ty/fold.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
9292
fn references_error(&self) -> bool {
9393
self.has_type_flags(TypeFlags::HAS_ERROR)
9494
}
95-
fn has_potential_param_types_or_consts(&self) -> bool {
95+
fn potentially_has_param_types_or_consts(&self) -> bool {
9696
self.has_type_flags(
9797
TypeFlags::HAS_KNOWN_TY_PARAM
9898
| TypeFlags::HAS_KNOWN_CT_PARAM
9999
| TypeFlags::HAS_UNKNOWN_DEFAULT_CONST_SUBSTS,
100100
)
101101
}
102-
fn has_param_types_or_consts(&self, tcx: TyCtxt<'tcx>) -> bool {
102+
fn definitely_has_param_types_or_consts(&self, tcx: TyCtxt<'tcx>) -> bool {
103103
self.definitely_has_type_flags(
104104
tcx,
105105
TypeFlags::HAS_KNOWN_TY_PARAM | TypeFlags::HAS_KNOWN_CT_PARAM,
@@ -129,7 +129,7 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
129129
TypeFlags::KNOWN_NEEDS_SUBST | TypeFlags::HAS_UNKNOWN_DEFAULT_CONST_SUBSTS,
130130
)
131131
}
132-
fn needs_subst(&self, tcx: TyCtxt<'tcx>) -> bool {
132+
fn definitely_needs_subst(&self, tcx: TyCtxt<'tcx>) -> bool {
133133
self.definitely_has_type_flags(tcx, TypeFlags::KNOWN_NEEDS_SUBST)
134134
}
135135
/// "Free" regions in this context means that it has any region
@@ -227,10 +227,13 @@ pub trait TypeVisitor<'tcx>: Sized {
227227
/// Supplies the `tcx` for an unevaluated anonymous constant in case its default substs
228228
/// are not yet supplied.
229229
///
230-
/// Visitors which do not look into these substs may return `None` here, in which case
231-
/// `super_visit_with` completely skips the default substs. Incorrectly returning
232-
/// `None` can very quickly lead to ICE or other critical bugs, so be careful and
233-
/// try to return an actual `tcx` if at all possible.
230+
/// Returning `None` for this method is only recommended if the `TypeVisitor`
231+
/// does not care about default anon const substs, as it ignores generic parameters,
232+
/// and fetching the default substs would cause a query cycle.
233+
///
234+
/// For visitors which return `None` we completely skip the default substs in `ty::Unevaluated::super_visit_with`.
235+
/// This means that incorrectly returning `None` can very quickly lead to ICE or other critical bugs, so be careful and
236+
/// try to return an actual `tcx` if possible.
234237
fn tcx_for_anon_const_substs(&self) -> Option<TyCtxt<'tcx>>;
235238

236239
fn visit_binder<T: TypeFoldable<'tcx>>(

compiler/rustc_middle/src/ty/layout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
17271727
// Ignore layouts that are done with non-empty environments or
17281728
// non-monomorphic layouts, as the user only wants to see the stuff
17291729
// resulting from the final codegen session.
1730-
if layout.ty.has_param_types_or_consts(self.tcx)
1730+
if layout.ty.definitely_has_param_types_or_consts(self.tcx)
17311731
|| !self.param_env.caller_bounds().is_empty()
17321732
{
17331733
return;
@@ -1896,7 +1896,7 @@ impl<'tcx> SizeSkeleton<'tcx> {
18961896
let tail = tcx.struct_tail_erasing_lifetimes(pointee, param_env);
18971897
match tail.kind() {
18981898
ty::Param(_) | ty::Projection(_) => {
1899-
debug_assert!(tail.has_param_types_or_consts(tcx));
1899+
debug_assert!(tail.definitely_has_param_types_or_consts(tcx));
19001900
Ok(SizeSkeleton::Pointer { non_zero, tail: tcx.erase_regions(tail) })
19011901
}
19021902
_ => bug!(

compiler/rustc_middle/src/ty/print/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ pub trait PrettyPrinter<'tcx>:
11931193

11941194
// Aggregates, printed as array/tuple/struct/variant construction syntax.
11951195
//
1196-
// NB: the `has_param_types_or_consts` check ensures that we can use
1196+
// NB: the `potentially_has_param_types_or_consts` check ensures that we can use
11971197
// the `destructure_const` query with an empty `ty::ParamEnv` without
11981198
// introducing ICEs (e.g. via `layout_of`) from missing bounds.
11991199
// E.g. `transmute([0usize; 2]): (u8, *mut T)` needs to know `T: Sized`
@@ -1202,7 +1202,7 @@ pub trait PrettyPrinter<'tcx>:
12021202
// FIXME(eddyb) for `--emit=mir`/`-Z dump-mir`, we should provide the
12031203
// correct `ty::ParamEnv` to allow printing *all* constant values.
12041204
(_, ty::Array(..) | ty::Tuple(..) | ty::Adt(..))
1205-
if !ty.has_potential_param_types_or_consts() =>
1205+
if !ty.potentially_has_param_types_or_consts() =>
12061206
{
12071207
let contents = self.tcx().destructure_const(
12081208
ty::ParamEnv::reveal_all()

compiler/rustc_mir/src/interpret/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ where
4343
let is_used = unused_params.contains(index).map_or(true, |unused| !unused);
4444
// Only recurse when generic parameters in fns, closures and generators
4545
// are used and require substitution.
46-
match (is_used, subst.needs_subst(self.tcx)) {
46+
match (is_used, subst.definitely_needs_subst(self.tcx)) {
4747
// Just in case there are closures or generators within this subst,
4848
// recurse.
4949
(true, true) => return subst.super_visit_with(self),

compiler/rustc_mir/src/monomorphize/polymorphize.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
288288
}
289289
#[instrument(skip(self))]
290290
fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> ControlFlow<Self::BreakTy> {
291-
if !c.has_potential_param_types_or_consts() {
291+
if !c.potentially_has_param_types_or_consts() {
292292
return ControlFlow::CONTINUE;
293293
}
294294

@@ -321,7 +321,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
321321

322322
#[instrument(skip(self))]
323323
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
324-
if !ty.has_potential_param_types_or_consts() {
324+
if !ty.potentially_has_param_types_or_consts() {
325325
return ControlFlow::CONTINUE;
326326
}
327327

@@ -363,7 +363,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for HasUsedGenericParams<'a, 'tcx> {
363363

364364
#[instrument(skip(self))]
365365
fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> ControlFlow<Self::BreakTy> {
366-
if !c.has_potential_param_types_or_consts() {
366+
if !c.potentially_has_param_types_or_consts() {
367367
return ControlFlow::CONTINUE;
368368
}
369369

@@ -381,7 +381,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for HasUsedGenericParams<'a, 'tcx> {
381381

382382
#[instrument(skip(self))]
383383
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
384-
if !ty.has_potential_param_types_or_consts() {
384+
if !ty.potentially_has_param_types_or_consts() {
385385
return ControlFlow::CONTINUE;
386386
}
387387

compiler/rustc_mir/src/transform/const_prop.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
469469
/// Returns the value, if any, of evaluating `c`.
470470
fn eval_constant(&mut self, c: &Constant<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
471471
// FIXME we need to revisit this for #67176
472-
if c.needs_subst(self.tcx) {
472+
if c.definitely_needs_subst(self.tcx) {
473473
return None;
474474
}
475475

@@ -489,9 +489,9 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
489489
}) => true,
490490
// Out of backwards compatibility we cannot report hard errors in unused
491491
// generic functions using associated constants of the generic parameters.
492-
_ => c.literal.needs_subst(*tcx),
492+
_ => c.literal.definitely_needs_subst(*tcx),
493493
},
494-
ConstantKind::Val(_, ty) => ty.needs_subst(*tcx),
494+
ConstantKind::Val(_, ty) => ty.definitely_needs_subst(*tcx),
495495
};
496496
if lint_only {
497497
// Out of backwards compatibility we cannot report hard errors in unused
@@ -721,7 +721,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
721721
}
722722

723723
// FIXME we need to revisit this for #67176
724-
if rvalue.needs_subst(self.tcx) {
724+
if rvalue.definitely_needs_subst(self.tcx) {
725725
return None;
726726
}
727727

compiler/rustc_mir/src/transform/inline/cycle.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ crate fn mir_callgraph_reachable(
8989
// FIXME: A not fully substituted drop shim can cause ICEs if one attempts to
9090
// have its MIR built. Likely oli-obk just screwed up the `ParamEnv`s, so this
9191
// needs some more analysis.
92-
if callee.needs_subst(tcx) {
92+
if callee.definitely_needs_subst(tcx) {
9393
continue;
9494
}
9595
}

compiler/rustc_symbol_mangling/src/legacy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn get_symbol_hash<'tcx>(
107107
tcx.def_path_hash(def_id).hash_stable(&mut hcx, &mut hasher);
108108

109109
// Include the main item-type. Note that, in this case, the
110-
// assertions about `needs_subst` may not hold, but this item-type
110+
// assertions about `definitely_needs_subst` may not hold, but this item-type
111111
// ought to be the same for every reference anyway.
112112
assert!(!item_type.has_erasable_regions(tcx));
113113
hcx.while_hashing_spans(false, |hcx| {

compiler/rustc_symbol_mangling/src/v0.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ impl Printer<'tcx> for &mut SymbolMangler<'tcx> {
277277

278278
// Encode impl generic params if the substitutions contain parameters (implying
279279
// polymorphization is enabled) and this isn't an inherent impl.
280-
if impl_trait_ref.is_some() && substs.iter().any(|a| a.has_param_types_or_consts(self.tcx))
280+
if impl_trait_ref.is_some()
281+
&& substs.iter().any(|a| a.definitely_has_param_types_or_consts(self.tcx))
281282
{
282283
self = self.path_generic_args(
283284
|this| {

compiler/rustc_trait_selection/src/traits/coherence.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ fn orphan_check_trait_ref<'tcx>(
391391
) -> Result<(), OrphanCheckErr<'tcx>> {
392392
debug!("orphan_check_trait_ref(trait_ref={:?}, in_crate={:?})", trait_ref, in_crate);
393393

394-
if trait_ref.needs_infer() && trait_ref.needs_subst(tcx) {
394+
if trait_ref.needs_infer() && trait_ref.definitely_needs_subst(tcx) {
395395
bug!(
396396
"can't orphan check a trait ref with both params and inference variables {:?}",
397397
trait_ref

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
8585
let leaf = leaf.subst(tcx, ct.substs);
8686
if leaf.has_infer_types_or_consts() {
8787
failure_kind = FailureKind::MentionsInfer;
88-
} else if leaf.has_param_types_or_consts(tcx) {
88+
} else if leaf.definitely_has_param_types_or_consts(tcx) {
8989
failure_kind = cmp::min(failure_kind, FailureKind::MentionsParam);
9090
}
9191

@@ -95,7 +95,7 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
9595
let ty = ty.subst(tcx, ct.substs);
9696
if ty.has_infer_types_or_consts() {
9797
failure_kind = FailureKind::MentionsInfer;
98-
} else if ty.has_param_types_or_consts(tcx) {
98+
} else if ty.definitely_has_param_types_or_consts(tcx) {
9999
failure_kind = cmp::min(failure_kind, FailureKind::MentionsParam);
100100
}
101101

@@ -151,7 +151,7 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
151151
// See #74595 for more details about this.
152152
let concrete = infcx.const_eval_resolve(param_env, uv.expand(), Some(span));
153153

154-
if concrete.is_ok() && uv.substs(infcx.tcx).has_param_types_or_consts(infcx.tcx) {
154+
if concrete.is_ok() && uv.substs(infcx.tcx).definitely_has_param_types_or_consts(infcx.tcx) {
155155
match infcx.tcx.def_kind(uv.def.did) {
156156
DefKind::AnonConst => {
157157
let mir_body = infcx.tcx.mir_for_ctfe_opt_const_arg(uv.def);

compiler/rustc_trait_selection/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ fn subst_and_check_impossible_predicates<'tcx>(
450450
debug!("subst_and_check_impossible_predicates(key={:?})", key);
451451

452452
let mut predicates = tcx.predicates_of(key.0).instantiate(tcx, key.1).predicates;
453-
predicates.retain(|predicate| !predicate.needs_subst(tcx));
453+
predicates.retain(|predicate| !predicate.definitely_needs_subst(tcx));
454454
let result = impossible_predicates(tcx, predicates);
455455

456456
debug!("subst_and_check_impossible_predicates(key={:?}) = {:?}", key, result);

compiler/rustc_trait_selection/src/traits/select/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
696696
.param_env
697697
.caller_bounds()
698698
.iter()
699-
.all(|bound| bound.needs_subst(self.tcx()))
699+
.all(|bound| bound.definitely_needs_subst(self.tcx()))
700700
{
701701
// If a param env has no global bounds, global obligations do not
702702
// depend on its particular value in order to work, so we can clear

compiler/rustc_typeck/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2204,7 +2204,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
22042204
self.prohibit_generics(path.segments);
22052205
// Try to evaluate any array length constants.
22062206
let normalized_ty = self.normalize_ty(span, tcx.at(span).type_of(def_id));
2207-
if forbid_generic && normalized_ty.needs_subst(tcx) {
2207+
if forbid_generic && normalized_ty.definitely_needs_subst(tcx) {
22082208
let mut err = tcx.sess.struct_span_err(
22092209
path.span,
22102210
"generic `Self` types are currently not permitted in anonymous constants",

compiler/rustc_typeck/src/check/wfcheck.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ fn check_where_clauses<'tcx, 'fcx>(
746746
// Ignore dependent defaults -- that is, where the default of one type
747747
// parameter includes another (e.g., `<T, U = T>`). In those cases, we can't
748748
// be sure if it will error or not as user might always specify the other.
749-
if !ty.needs_subst(tcx) {
749+
if !ty.definitely_needs_subst(tcx) {
750750
fcx.register_wf_obligation(
751751
ty.into(),
752752
tcx.def_span(param.def_id),
@@ -762,7 +762,7 @@ fn check_where_clauses<'tcx, 'fcx>(
762762
// for `struct Foo<const N: usize, const M: usize = { 1 - 2 }>`
763763
// we should eagerly error.
764764
let default_ct = tcx.const_param_default(param.def_id);
765-
if !default_ct.needs_subst(tcx) {
765+
if !default_ct.definitely_needs_subst(tcx) {
766766
fcx.register_wf_obligation(
767767
default_ct.into(),
768768
tcx.def_span(param.def_id),
@@ -796,7 +796,7 @@ fn check_where_clauses<'tcx, 'fcx>(
796796
if is_our_default(param) {
797797
let default_ty = tcx.type_of(param.def_id);
798798
// ... and it's not a dependent default, ...
799-
if !default_ty.needs_subst(tcx) {
799+
if !default_ty.definitely_needs_subst(tcx) {
800800
// ... then substitute it with the default.
801801
return default_ty.into();
802802
}
@@ -809,7 +809,7 @@ fn check_where_clauses<'tcx, 'fcx>(
809809
if is_our_default(param) {
810810
let default_ct = tcx.const_param_default(param.def_id);
811811
// ... and it's not a dependent default, ...
812-
if !default_ct.needs_subst(tcx) {
812+
if !default_ct.definitely_needs_subst(tcx) {
813813
// ... then substitute it with the default.
814814
return default_ct.into();
815815
}
@@ -858,7 +858,7 @@ fn check_where_clauses<'tcx, 'fcx>(
858858
let substituted_pred = pred.subst(tcx, substs);
859859
// Don't check non-defaulted params, dependent defaults (including lifetimes)
860860
// or preds with multiple params.
861-
if substituted_pred.has_param_types_or_consts(tcx)
861+
if substituted_pred.definitely_has_param_types_or_consts(tcx)
862862
|| param_count.params.len() > 1
863863
|| has_region
864864
{

compiler/rustc_typeck/src/collect/type_of.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,21 @@ fn get_path_containing_arg_in_pat<'hir>(
277277
pub(super) fn default_anon_const_substs(tcx: TyCtxt<'_>, def_id: DefId) -> SubstsRef<'_> {
278278
let generics = tcx.generics_of(def_id);
279279
if let Some(parent) = generics.parent {
280+
// This is the reason we bother with having optional anon const substs.
281+
//
282+
// In the future the substs of an anon const will depend on its parents predicates
283+
// at which point eagerly looking at them will cause a query cycle.
284+
//
285+
// So for now this is only an assurance that this approach won't cause cycle errors in
286+
// the future.
280287
let _cycle_check = tcx.predicates_of(parent);
281288
}
282289

283290
let substs = InternalSubsts::identity_for_item(tcx, def_id);
284291
// We only expect substs with the following type flags as default substs.
285292
//
286293
// Getting this wrong can lead to ICE and unsoundness, so we assert it here.
287-
for arg in substs.iter().flat_map(|s| s.walk(tcx)) {
294+
for arg in substs.iter() {
288295
let allowed_flags = ty::TypeFlags::MAY_NEED_DEFAULT_CONST_SUBSTS
289296
| ty::TypeFlags::STILL_FURTHER_SPECIALIZABLE;
290297
assert!(!arg.has_type_flags(!allowed_flags));

0 commit comments

Comments
 (0)