Rollup merge of #91251 - oli-obk:wf_sync_statics, r=matthewjasper

Perform Sync check on static items in wf-check instead of during const checks

r? `@RalfJung`

This check is solely happening on the signature of the static item and not on its body, therefor it belongs into wf-checking instead of const checking.
This commit is contained in:
Matthias Krüger 2021-11-28 17:11:10 +01:00 committed by GitHub
commit 233c50e79e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 105 additions and 136 deletions

View file

@ -1,8 +1,8 @@
//! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations.
use rustc_errors::{Applicability, Diagnostic, ErrorReported};
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::{self as hir, HirId, LangItem};
use rustc_index::bit_set::BitSet;
use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
@ -14,8 +14,7 @@ use rustc_middle::ty::{self, adjustment::PointerCast, Instance, InstanceDef, Ty,
use rustc_middle::ty::{Binder, TraitPredicate, TraitRef};
use rustc_mir_dataflow::{self, Analysis};
use rustc_span::{sym, Span, Symbol};
use rustc_trait_selection::traits::error_reporting::InferCtxtExt;
use rustc_trait_selection::traits::{self, SelectionContext, TraitEngine};
use rustc_trait_selection::traits::SelectionContext;
use std::mem;
use std::ops::Deref;
@ -255,16 +254,6 @@ impl Checker<'mir, 'tcx> {
self.visit_body(&body);
}
// Ensure that the end result is `Sync` in a non-thread local `static`.
let should_check_for_sync = self.const_kind()
== hir::ConstContext::Static(hir::Mutability::Not)
&& !tcx.is_thread_local_static(def_id.to_def_id());
if should_check_for_sync {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
check_return_ty_is_sync(tcx, &body, hir_id);
}
// If we got through const-checking without emitting any "primary" errors, emit any
// "secondary" errors if they occurred.
let secondary_errors = mem::take(&mut self.secondary_errors);
@ -1054,20 +1043,6 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
}
}
fn check_return_ty_is_sync(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, hir_id: HirId) {
let ty = body.return_ty();
tcx.infer_ctxt().enter(|infcx| {
let cause = traits::ObligationCause::new(body.span, hir_id, traits::SharedStatic);
let mut fulfillment_cx = traits::FulfillmentContext::new();
let sync_def_id = tcx.require_lang_item(LangItem::Sync, Some(body.span));
fulfillment_cx.register_bound(&infcx, ty::ParamEnv::empty(), ty, sync_def_id, cause);
let errors = fulfillment_cx.select_all_or_error(&infcx);
if !errors.is_empty() {
infcx.report_fulfillment_errors(&errors, None, false);
}
});
}
fn place_as_reborrow(
tcx: TyCtxt<'tcx>,
body: &Body<'tcx>,