Skip to content

Commit fc2daaa

Browse files
committed
Auto merge of #77302 - RalfJung:rollup-n8gg3v6, r=RalfJung
Rollup of 7 pull requests Successful merges: - #76454 (UI to unit test for those using Cell/RefCell/UnsafeCell) - #76474 (Add option to pass a custom codegen backend from a driver) - #76711 (diag: improve closure/generic parameter mismatch) - #77170 (Remove `#[rustc_allow_const_fn_ptr]` and add `#![feature(const_fn_fn_ptr_basics)]`) - #77194 (Add doc alias for iterator fold) - #77288 (fix building libstd for Miri on macOS) - #77295 (Update unstable-book: Fix ABNF in inline assembly docs) Failed merges: r? `@ghost`
2 parents d62d3f7 + d9c1192 commit fc2daaa

File tree

76 files changed

+940
-882
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+940
-882
lines changed

compiler/rustc_attr/src/builtin.rs

+7-24
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ pub struct ConstStability {
145145
pub feature: Symbol,
146146
/// whether the function has a `#[rustc_promotable]` attribute
147147
pub promotable: bool,
148-
/// whether the function has a `#[rustc_allow_const_fn_ptr]` attribute
149-
pub allow_const_fn_ptr: bool,
150148
}
151149

152150
/// The available stability levels.
@@ -190,7 +188,6 @@ where
190188
let mut stab: Option<Stability> = None;
191189
let mut const_stab: Option<ConstStability> = None;
192190
let mut promotable = false;
193-
let mut allow_const_fn_ptr = false;
194191
let diagnostic = &sess.parse_sess.span_diagnostic;
195192

196193
'outer: for attr in attrs_iter {
@@ -200,7 +197,6 @@ where
200197
sym::unstable,
201198
sym::stable,
202199
sym::rustc_promotable,
203-
sym::rustc_allow_const_fn_ptr,
204200
]
205201
.iter()
206202
.any(|&s| attr.has_name(s))
@@ -215,9 +211,6 @@ where
215211
if attr.has_name(sym::rustc_promotable) {
216212
promotable = true;
217213
}
218-
if attr.has_name(sym::rustc_allow_const_fn_ptr) {
219-
allow_const_fn_ptr = true;
220-
}
221214
// attributes with data
222215
else if let Some(MetaItem { kind: MetaItemKind::List(ref metas), .. }) = meta {
223216
let meta = meta.as_ref().unwrap();
@@ -360,12 +353,8 @@ where
360353
if sym::unstable == meta_name {
361354
stab = Some(Stability { level, feature });
362355
} else {
363-
const_stab = Some(ConstStability {
364-
level,
365-
feature,
366-
promotable: false,
367-
allow_const_fn_ptr: false,
368-
});
356+
const_stab =
357+
Some(ConstStability { level, feature, promotable: false });
369358
}
370359
}
371360
(None, _, _) => {
@@ -440,12 +429,8 @@ where
440429
if sym::stable == meta_name {
441430
stab = Some(Stability { level, feature });
442431
} else {
443-
const_stab = Some(ConstStability {
444-
level,
445-
feature,
446-
promotable: false,
447-
allow_const_fn_ptr: false,
448-
});
432+
const_stab =
433+
Some(ConstStability { level, feature, promotable: false });
449434
}
450435
}
451436
(None, _) => {
@@ -464,18 +449,16 @@ where
464449
}
465450

466451
// Merge the const-unstable info into the stability info
467-
if promotable || allow_const_fn_ptr {
452+
if promotable {
468453
if let Some(ref mut stab) = const_stab {
469454
stab.promotable = promotable;
470-
stab.allow_const_fn_ptr = allow_const_fn_ptr;
471455
} else {
472456
struct_span_err!(
473457
diagnostic,
474458
item_sp,
475459
E0717,
476-
"rustc_promotable and rustc_allow_const_fn_ptr attributes \
477-
must be paired with either a rustc_const_unstable or a rustc_const_stable \
478-
attribute"
460+
"`rustc_promotable` attribute must be paired with either a `rustc_const_unstable` \
461+
or a `rustc_const_stable` attribute"
479462
)
480463
.emit();
481464
}

compiler/rustc_driver/src/lib.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ pub fn run_compiler(
141141
callbacks: &mut (dyn Callbacks + Send),
142142
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
143143
emitter: Option<Box<dyn Write + Send>>,
144+
make_codegen_backend: Option<
145+
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
146+
>,
144147
) -> interface::Result<()> {
145148
let mut args = Vec::new();
146149
for arg in at_args {
@@ -162,6 +165,11 @@ pub fn run_compiler(
162165
let sopts = config::build_session_options(&matches);
163166
let cfg = interface::parse_cfgspecs(matches.opt_strs("cfg"));
164167

168+
// We wrap `make_codegen_backend` in another `Option` such that `dummy_config` can take
169+
// ownership of it when necessary, while also allowing the non-dummy config to take ownership
170+
// when `dummy_config` is not used.
171+
let mut make_codegen_backend = Some(make_codegen_backend);
172+
165173
let mut dummy_config = |sopts, cfg, diagnostic_output| {
166174
let mut config = interface::Config {
167175
opts: sopts,
@@ -177,6 +185,7 @@ pub fn run_compiler(
177185
lint_caps: Default::default(),
178186
register_lints: None,
179187
override_queries: None,
188+
make_codegen_backend: make_codegen_backend.take().unwrap(),
180189
registry: diagnostics_registry(),
181190
};
182191
callbacks.config(&mut config);
@@ -253,6 +262,7 @@ pub fn run_compiler(
253262
lint_caps: Default::default(),
254263
register_lints: None,
255264
override_queries: None,
265+
make_codegen_backend: make_codegen_backend.unwrap(),
256266
registry: diagnostics_registry(),
257267
};
258268

@@ -1265,7 +1275,7 @@ pub fn main() -> ! {
12651275
})
12661276
})
12671277
.collect::<Vec<_>>();
1268-
run_compiler(&args, &mut callbacks, None, None)
1278+
run_compiler(&args, &mut callbacks, None, None, None)
12691279
});
12701280
// The extra `\t` is necessary to align this label with the others.
12711281
print_time_passes_entry(callbacks.time_passes, "\ttotal", start.elapsed());

compiler/rustc_feature/src/active.rs

+3
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,9 @@ declare_features! (
587587
/// Allows basic arithmetic on floating point types in a `const fn`.
588588
(active, const_fn_floating_point_arithmetic, "1.48.0", Some(57241), None),
589589

590+
/// Allows using and casting function pointers in a `const fn`.
591+
(active, const_fn_fn_ptr_basics, "1.48.0", Some(57563), None),
592+
590593
// -------------------------------------------------------------------------
591594
// feature-group-end: actual feature gates
592595
// -------------------------------------------------------------------------

compiler/rustc_feature/src/builtin_attrs.rs

-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,6 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
464464
// ==========================================================================
465465

466466
rustc_attr!(rustc_promotable, AssumedUsed, template!(Word), IMPL_DETAIL),
467-
rustc_attr!(rustc_allow_const_fn_ptr, AssumedUsed, template!(Word), IMPL_DETAIL),
468467
rustc_attr!(rustc_args_required_const, AssumedUsed, template!(List: "N"), INTERNAL_UNSTABLE),
469468

470469
// ==========================================================================

compiler/rustc_interface/src/interface.rs

+5
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ pub struct Config {
154154
pub override_queries:
155155
Option<fn(&Session, &mut ty::query::Providers, &mut ty::query::Providers)>,
156156

157+
/// This is a callback from the driver that is called to create a codegen backend.
158+
pub make_codegen_backend:
159+
Option<Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>>,
160+
157161
/// Registry of diagnostics codes.
158162
pub registry: Registry,
159163
}
@@ -167,6 +171,7 @@ pub fn create_compiler_and_run<R>(config: Config, f: impl FnOnce(&Compiler) -> R
167171
config.file_loader,
168172
config.input_path.clone(),
169173
config.lint_caps,
174+
config.make_codegen_backend,
170175
registry.clone(),
171176
);
172177

compiler/rustc_interface/src/util.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,17 @@ pub fn create_session(
6363
file_loader: Option<Box<dyn FileLoader + Send + Sync + 'static>>,
6464
input_path: Option<PathBuf>,
6565
lint_caps: FxHashMap<lint::LintId, lint::Level>,
66+
make_codegen_backend: Option<
67+
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
68+
>,
6669
descriptions: Registry,
6770
) -> (Lrc<Session>, Lrc<Box<dyn CodegenBackend>>) {
68-
let codegen_backend = get_codegen_backend(&sopts);
71+
let codegen_backend = if let Some(make_codegen_backend) = make_codegen_backend {
72+
make_codegen_backend(&sopts)
73+
} else {
74+
get_codegen_backend(&sopts)
75+
};
76+
6977
// target_override is documented to be called before init(), so this is okay
7078
let target_override = codegen_backend.target_override(&sopts);
7179

compiler/rustc_middle/src/query/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,6 @@ rustc_queries! {
457457
desc { |tcx| "checking if item is promotable: `{}`", tcx.def_path_str(key) }
458458
}
459459

460-
query const_fn_is_allowed_fn_ptr(key: DefId) -> bool {
461-
desc { |tcx| "checking if const fn allows `fn()` types: `{}`", tcx.def_path_str(key) }
462-
}
463-
464460
/// Returns `true` if this is a foreign item (i.e., linked via `extern { ... }`).
465461
query is_foreign_item(key: DefId) -> bool {
466462
desc { |tcx| "checking if `{}` is a foreign item", tcx.def_path_str(key) }

compiler/rustc_middle/src/ty/error.rs

+12
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,18 @@ impl<T> Trait<T> for X {
475475
#traits-as-parameters",
476476
);
477477
}
478+
(ty::Param(p), ty::Closure(..) | ty::Generator(..)) => {
479+
let generics = self.generics_of(body_owner_def_id);
480+
let p_span = self.def_span(generics.type_param(p, self).def_id);
481+
if !sp.contains(p_span) {
482+
db.span_label(p_span, "this type parameter");
483+
}
484+
db.help(&format!(
485+
"every closure has a distinct type and so could not always match the \
486+
caller-chosen type of parameter `{}`",
487+
p
488+
));
489+
}
478490
(ty::Param(p), _) | (_, ty::Param(p)) => {
479491
let generics = self.generics_of(body_owner_def_id);
480492
let p_span = self.def_span(generics.type_param(p, self).def_id);

compiler/rustc_mir/src/const_eval/fn_queries.rs

-6
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,11 @@ fn is_promotable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
151151
}
152152
}
153153

154-
fn const_fn_is_allowed_fn_ptr(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
155-
is_const_fn(tcx, def_id)
156-
&& tcx.lookup_const_stability(def_id).map(|stab| stab.allow_const_fn_ptr).unwrap_or(false)
157-
}
158-
159154
pub fn provide(providers: &mut Providers) {
160155
*providers = Providers {
161156
is_const_fn_raw,
162157
is_const_impl_raw: |tcx, def_id| is_const_impl_raw(tcx, def_id.expect_local()),
163158
is_promotable_const_fn,
164-
const_fn_is_allowed_fn_ptr,
165159
..*providers
166160
};
167161
}

compiler/rustc_mir/src/transform/check_consts/ops.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,21 @@ impl NonConstOp for FnPtrCast {
213213
const STOPS_CONST_CHECKING: bool = true;
214214

215215
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
216-
mcf_status_in_item(ccx)
216+
if ccx.const_kind() != hir::ConstContext::ConstFn {
217+
Status::Allowed
218+
} else {
219+
Status::Unstable(sym::const_fn_fn_ptr_basics)
220+
}
217221
}
218222

219223
fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) {
220-
mcf_emit_error(ccx, span, "function pointer casts are not allowed in const fn");
224+
feature_err(
225+
&ccx.tcx.sess.parse_sess,
226+
sym::const_fn_fn_ptr_basics,
227+
span,
228+
&format!("function pointer casts are not allowed in {}s", ccx.const_kind()),
229+
)
230+
.emit()
221231
}
222232
}
223233

@@ -596,17 +606,21 @@ pub mod ty {
596606
const STOPS_CONST_CHECKING: bool = true;
597607

598608
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
599-
// FIXME: This attribute a hack to allow the specialization of the `futures` API. See
600-
// #59739. We should have a proper feature gate for this.
601-
if ccx.tcx.has_attr(ccx.def_id.to_def_id(), sym::rustc_allow_const_fn_ptr) {
609+
if ccx.const_kind() != hir::ConstContext::ConstFn {
602610
Status::Allowed
603611
} else {
604-
mcf_status_in_item(ccx)
612+
Status::Unstable(sym::const_fn_fn_ptr_basics)
605613
}
606614
}
607615

608616
fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) {
609-
mcf_emit_error(ccx, span, "function pointers in const fn are unstable");
617+
feature_err(
618+
&ccx.tcx.sess.parse_sess,
619+
sym::const_fn_fn_ptr_basics,
620+
span,
621+
&format!("function pointers cannot appear in {}s", ccx.const_kind()),
622+
)
623+
.emit()
610624
}
611625
}
612626

compiler/rustc_span/src/symbol.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ symbols! {
353353
const_extern_fn,
354354
const_fn,
355355
const_fn_floating_point_arithmetic,
356+
const_fn_fn_ptr_basics,
356357
const_fn_transmute,
357358
const_fn_union,
358359
const_generics,
@@ -884,7 +885,6 @@ symbols! {
884885
rustc,
885886
rustc_allocator,
886887
rustc_allocator_nounwind,
887-
rustc_allow_const_fn_ptr,
888888
rustc_args_required_const,
889889
rustc_attrs,
890890
rustc_builtin_macro,

compiler/rustc_typeck/src/check/demand.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
117117
ty
118118
}
119119

120-
// Checks that the type of `expr` can be coerced to `expected`.
121-
//
122-
// N.B., this code relies on `self.diverges` to be accurate. In
123-
// particular, assignments to `!` will be permitted if the
124-
// diverges flag is currently "always".
120+
/// Checks that the type of `expr` can be coerced to `expected`.
121+
///
122+
/// N.B., this code relies on `self.diverges` to be accurate. In particular, assignments to `!`
123+
/// will be permitted if the diverges flag is currently "always".
125124
pub fn demand_coerce_diag(
126125
&self,
127126
expr: &hir::Expr<'_>,

library/alloc/tests/boxed.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cell::Cell;
12
use std::mem::MaybeUninit;
23
use std::ptr::NonNull;
34

@@ -49,3 +50,10 @@ fn box_clone_from_ptr_stability() {
4950
assert_eq!(copy.as_ptr() as usize, copy_raw);
5051
}
5152
}
53+
54+
#[test]
55+
fn box_deref_lval() {
56+
let x = Box::new(Cell::new(5));
57+
x.set(1000);
58+
assert_eq!(x.get(), 1000);
59+
}

0 commit comments

Comments
 (0)