Skip to content

Commit 0c0f27a

Browse files
committed
Auto merge of rust-lang#147695 - cjgillot:deduce-param-freeze, r=tmiasko
deduced_param_attrs: check Freeze on monomorphic types. `deduced_param_attrs` currently checks `Freeze` bound on polymorphic MIR. This pessimizes the deduction, as generic types are not `Freeze` by default. This moves the check to the ABI adjustment.
2 parents c8a31b7 + 0b02102 commit 0c0f27a

File tree

13 files changed

+157
-92
lines changed

13 files changed

+157
-92
lines changed

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ use rustc_macros::{
2424
use rustc_middle::metadata::ModChild;
2525
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
2626
use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
27+
use rustc_middle::middle::deduced_param_attrs::DeducedParamAttrs;
2728
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
2829
use rustc_middle::middle::lib_features::FeatureStability;
2930
use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
3031
use rustc_middle::mir;
3132
use rustc_middle::ty::fast_reject::SimplifiedType;
32-
use rustc_middle::ty::{self, DeducedParamAttrs, Ty, TyCtxt, UnusedGenericParams};
33+
use rustc_middle::ty::{self, Ty, TyCtxt, UnusedGenericParams};
3334
use rustc_middle::util::Providers;
3435
use rustc_serialize::opaque::FileEncoder;
3536
use rustc_session::config::{SymbolManglingVersion, TargetModifier};

compiler/rustc_metadata/src/rmeta/parameterized.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ trivially_parameterized_over_tcx! {
9797
rustc_middle::metadata::ModChild,
9898
rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs,
9999
rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile,
100+
rustc_middle::middle::deduced_param_attrs::DeducedParamAttrs,
100101
rustc_middle::middle::exported_symbols::SymbolExportInfo,
101102
rustc_middle::middle::lib_features::FeatureStability,
102103
rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault,
@@ -105,7 +106,6 @@ trivially_parameterized_over_tcx! {
105106
rustc_middle::ty::AssocContainer,
106107
rustc_middle::ty::AsyncDestructor,
107108
rustc_middle::ty::Asyncness,
108-
rustc_middle::ty::DeducedParamAttrs,
109109
rustc_middle::ty::Destructor,
110110
rustc_middle::ty::Generics,
111111
rustc_middle::ty::ImplTraitInTraitData,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use rustc_macros::{Decodable, Encodable, HashStable};
2+
3+
use crate::ty::{Ty, TyCtxt, TypingEnv};
4+
5+
/// Flags that dictate how a parameter is mutated. If the flags are empty, the param is
6+
/// read-only. If non-empty, it is read-only with conditions.
7+
#[derive(Clone, Copy, PartialEq, Debug, Decodable, Encodable, HashStable)]
8+
pub struct DeducedReadOnlyParam(u8);
9+
10+
bitflags::bitflags! {
11+
impl DeducedReadOnlyParam: u8 {
12+
/// This parameter is dropped. It is read-only if `!needs_drop`.
13+
const IF_NO_DROP = 1 << 0;
14+
/// This parameter is borrowed. It is read-only if `Freeze`.
15+
const IF_FREEZE = 1 << 1;
16+
/// This parameter is mutated. It is never read-only.
17+
const MUTATED = 1 << 2;
18+
}
19+
}
20+
21+
/// Parameter attributes that can only be determined by examining the body of a function instead
22+
/// of just its signature.
23+
///
24+
/// These can be useful for optimization purposes when a function is directly called. We compute
25+
/// them and store them into the crate metadata so that downstream crates can make use of them.
26+
///
27+
/// Right now, we only have `read_only`, but `no_capture` and `no_alias` might be useful in the
28+
/// future.
29+
#[derive(Clone, Copy, PartialEq, Debug, Decodable, Encodable, HashStable)]
30+
pub struct DeducedParamAttrs {
31+
/// The parameter is marked immutable in the function.
32+
pub read_only: DeducedReadOnlyParam,
33+
}
34+
35+
// By default, consider the parameters to be mutated.
36+
impl Default for DeducedParamAttrs {
37+
#[inline]
38+
fn default() -> DeducedParamAttrs {
39+
DeducedParamAttrs { read_only: DeducedReadOnlyParam::MUTATED }
40+
}
41+
}
42+
43+
impl DeducedParamAttrs {
44+
#[inline]
45+
pub fn is_default(self) -> bool {
46+
self.read_only.contains(DeducedReadOnlyParam::MUTATED)
47+
}
48+
49+
pub fn read_only<'tcx>(
50+
&self,
51+
tcx: TyCtxt<'tcx>,
52+
typing_env: TypingEnv<'tcx>,
53+
ty: Ty<'tcx>,
54+
) -> bool {
55+
let read_only = self.read_only;
56+
if read_only.contains(DeducedReadOnlyParam::MUTATED) {
57+
return false;
58+
}
59+
if read_only.contains(DeducedReadOnlyParam::IF_NO_DROP) && ty.needs_drop(tcx, typing_env) {
60+
return false;
61+
}
62+
if read_only.contains(DeducedReadOnlyParam::IF_FREEZE) && !ty.is_freeze(tcx, typing_env) {
63+
return false;
64+
}
65+
true
66+
}
67+
}

compiler/rustc_middle/src/middle/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod codegen_fn_attrs;
22
pub mod debugger_visualizer;
3+
pub mod deduced_param_attrs;
34
pub mod dependency_format;
45
pub mod exported_symbols;
56
pub mod lang_items;

compiler/rustc_middle/src/query/erase.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ trivial! {
313313
rustc_hir::Stability,
314314
rustc_hir::Upvar,
315315
rustc_index::bit_set::FiniteBitSet<u32>,
316+
rustc_middle::middle::deduced_param_attrs::DeducedParamAttrs,
316317
rustc_middle::middle::dependency_format::Linkage,
317318
rustc_middle::middle::exported_symbols::SymbolExportInfo,
318319
rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault,
@@ -336,7 +337,6 @@ trivial! {
336337
rustc_middle::ty::AsyncDestructor,
337338
rustc_middle::ty::BoundVariableKind,
338339
rustc_middle::ty::AnonConstKind,
339-
rustc_middle::ty::DeducedParamAttrs,
340340
rustc_middle::ty::Destructor,
341341
rustc_middle::ty::fast_reject::SimplifiedType,
342342
rustc_middle::ty::ImplPolarity,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ use crate::lint::LintExpectation;
107107
use crate::metadata::ModChild;
108108
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
109109
use crate::middle::debugger_visualizer::DebuggerVisualizerFile;
110+
use crate::middle::deduced_param_attrs::DeducedParamAttrs;
110111
use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
111112
use crate::middle::lib_features::LibFeatures;
112113
use crate::middle::privacy::EffectiveVisibilities;
@@ -2656,7 +2657,7 @@ rustc_queries! {
26562657
return_result_from_ensure_ok
26572658
}
26582659

2659-
query deduced_param_attrs(def_id: DefId) -> &'tcx [ty::DeducedParamAttrs] {
2660+
query deduced_param_attrs(def_id: DefId) -> &'tcx [DeducedParamAttrs] {
26602661
desc { |tcx| "deducing parameter attributes for {}", tcx.def_path_str(def_id) }
26612662
separate_provide_extern
26622663
}

compiler/rustc_middle/src/query/on_disk_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ impl_ref_decoder! {<'tcx>
799799
rustc_span::def_id::DefId,
800800
rustc_span::def_id::LocalDefId,
801801
(rustc_middle::middle::exported_symbols::ExportedSymbol<'tcx>, rustc_middle::middle::exported_symbols::SymbolExportInfo),
802-
ty::DeducedParamAttrs,
802+
rustc_middle::middle::deduced_param_attrs::DeducedParamAttrs,
803803
}
804804

805805
//- ENCODING -------------------------------------------------------------------

compiler/rustc_middle/src/ty/codec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ impl_arena_copy_decoder! {<'tcx>
577577
rustc_span::def_id::DefId,
578578
rustc_span::def_id::LocalDefId,
579579
(rustc_middle::middle::exported_symbols::ExportedSymbol<'tcx>, rustc_middle::middle::exported_symbols::SymbolExportInfo),
580-
ty::DeducedParamAttrs,
580+
rustc_middle::middle::deduced_param_attrs::DeducedParamAttrs,
581581
}
582582

583583
#[macro_export]

compiler/rustc_middle/src/ty/context.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ use rustc_hir::lang_items::LangItem;
4141
use rustc_hir::limit::Limit;
4242
use rustc_hir::{self as hir, Attribute, HirId, Node, TraitCandidate, find_attr};
4343
use rustc_index::IndexVec;
44-
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
4544
use rustc_query_system::cache::WithDepNode;
4645
use rustc_query_system::dep_graph::DepNodeIndex;
4746
use rustc_query_system::ich::StableHashingContext;
@@ -3562,21 +3561,6 @@ impl<'tcx> TyCtxt<'tcx> {
35623561
}
35633562
}
35643563

3565-
/// Parameter attributes that can only be determined by examining the body of a function instead
3566-
/// of just its signature.
3567-
///
3568-
/// These can be useful for optimization purposes when a function is directly called. We compute
3569-
/// them and store them into the crate metadata so that downstream crates can make use of them.
3570-
///
3571-
/// Right now, we only have `read_only`, but `no_capture` and `no_alias` might be useful in the
3572-
/// future.
3573-
#[derive(Clone, Copy, PartialEq, Debug, Default, TyDecodable, TyEncodable, HashStable)]
3574-
pub struct DeducedParamAttrs {
3575-
/// The parameter is marked immutable in the function and contains no `UnsafeCell` (i.e. its
3576-
/// type is freeze).
3577-
pub read_only: bool,
3578-
}
3579-
35803564
pub fn provide(providers: &mut Providers) {
35813565
providers.is_panic_runtime =
35823566
|tcx, LocalCrate| contains_name(tcx.hir_krate_attrs(), sym::panic_runtime);

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ pub use self::consts::{
7878
ExprKind, ScalarInt, UnevaluatedConst, ValTree, ValTreeKind, Value,
7979
};
8080
pub use self::context::{
81-
CtxtInterners, CurrentGcx, DeducedParamAttrs, Feed, FreeRegionInfo, GlobalCtxt, Lift, TyCtxt,
82-
TyCtxtFeed, tls,
81+
CtxtInterners, CurrentGcx, Feed, FreeRegionInfo, GlobalCtxt, Lift, TyCtxt, TyCtxtFeed, tls,
8382
};
8483
pub use self::fold::*;
8584
pub use self::instance::{Instance, InstanceKind, ReifyReason, UnusedGenericParams};

0 commit comments

Comments
 (0)