Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9591942

Browse files
committed
Deduce readonly in generic parameters
1 parent ba0ec68 commit 9591942

File tree

3 files changed

+10
-21
lines changed

3 files changed

+10
-21
lines changed

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3300,6 +3300,7 @@ pub struct DeducedParamAttrs {
33003300
/// The parameter is marked immutable in the function and contains no `UnsafeCell` (i.e. its
33013301
/// type is freeze).
33023302
pub read_only: bool,
3303+
pub read_only_when_freeze: bool,
33033304
}
33043305

33053306
pub fn provide(providers: &mut Providers) {

compiler/rustc_mir_transform/src/deduce_param_attrs.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -169,26 +169,11 @@ pub(super) fn deduced_param_attrs<'tcx>(
169169

170170
// Set the `readonly` attribute for every argument that we concluded is immutable and that
171171
// contains no UnsafeCells.
172-
//
173-
// FIXME: This is overly conservative around generic parameters: `is_freeze()` will always
174-
// return false for them. For a description of alternatives that could do a better job here,
175-
// see [1].
176-
//
177-
// [1]: https://github.com/rust-lang/rust/pull/103172#discussion_r999139997
178-
let typing_env = body.typing_env(tcx);
179-
let mut deduced_param_attrs = tcx.arena.alloc_from_iter(
180-
body.local_decls.iter().skip(1).take(body.arg_count).enumerate().map(
181-
|(arg_index, local_decl)| DeducedParamAttrs {
182-
read_only: deduce.read_only.contains(arg_index) || (
183-
deduce.read_only_when_freeze.contains(arg_index)
184-
// We must normalize here to reveal opaques and normalize
185-
// their generic parameters, otherwise we'll see exponential
186-
// blow-up in compile times: #113372
187-
&& tcx.normalize_erasing_regions(typing_env, local_decl.ty).is_freeze(tcx, typing_env))
188-
189-
}
190-
),
191-
);
172+
let mut deduced_param_attrs =
173+
tcx.arena.alloc_from_iter((0..body.arg_count).map(|arg_index| DeducedParamAttrs {
174+
read_only: deduce.read_only.contains(arg_index),
175+
read_only_when_freeze: deduce.read_only_when_freeze.contains(arg_index),
176+
}));
192177

193178
// Trailing parameters past the size of the `deduced_param_attrs` array are assumed to have the
194179
// default set of attributes, so we don't have to store them explicitly. Pop them off to save a

compiler/rustc_ty_utils/src/abi.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,10 @@ fn fn_abi_adjust_for_abi<'tcx>(
712712
// we can't deduce any parameters for, so make sure the argument index is in
713713
// bounds.
714714
if let Some(deduced_param_attrs) = deduced_param_attrs.get(arg_idx) {
715-
if deduced_param_attrs.read_only {
715+
if deduced_param_attrs.read_only
716+
|| (deduced_param_attrs.read_only_when_freeze
717+
&& arg.layout.ty.is_freeze(tcx, cx.typing_env))
718+
{
716719
attrs.regular.insert(ArgAttribute::ReadOnly);
717720
debug!("added deduced read-only attribute");
718721
}

0 commit comments

Comments
 (0)