Skip to content

Rollup of 10 pull requests #81171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
14eb94f
don't suggest erroneous trailing comma after `..`
zackmdavis Jan 16, 2021
ad5aa23
Remove an unnecessary field from a `NonConstOp`
oli-obk Jan 4, 2021
949bdd8
Add regression test
oli-obk Jan 4, 2021
2136a5c
Fix `unused_unsafe` label with `unsafe_block_in_unsafe_fn
LeSeulArtichaut Jan 17, 2021
70a43e0
Fix structured suggestion for explicit `drop` call
estebank Jan 18, 2021
dc04cea
use raw-ptr-addr-of for slice::swap
RalfJung Jan 18, 2021
1d1ab21
Remove inline script tags
GuillaumeGomez Jan 18, 2021
5bac1c9
Only inherit const stability for methods of `impl const Trait` blocks
oli-obk Jan 16, 2021
222e0e4
Fix typo in simplify.rs
eltociear Jan 18, 2021
712d065
remove some outdated comments regarding debug assertions
RalfJung Jan 18, 2021
47c2476
Fixes #81109 - Typo in pointer::wrapping_sub
soniasingla Jan 18, 2021
4775334
BTreeMap: prefer bulk_steal functions over specialized ones
ssomers Jan 17, 2021
6f0cfce
Rollup merge of #80707 - oli-obk:stability_hole_const_intrinsics, r=R…
m-ou-se Jan 18, 2021
a246cdb
Rollup merge of #81103 - zackmdavis:comma_trail, r=davidtwco
m-ou-se Jan 18, 2021
d68ed11
Rollup merge of #81110 - LeSeulArtichaut:fix-unused-unsafe-label, r=R…
m-ou-se Jan 18, 2021
eee7d3c
Rollup merge of #81115 - ssomers:btree_drainy_refactor_4, r=Mark-Simu…
m-ou-se Jan 18, 2021
3a8aa7b
Rollup merge of #81147 - estebank:drop-suggestion, r=varkor
m-ou-se Jan 18, 2021
0f1dff9
Rollup merge of #81160 - RalfJung:swap, r=oli-obk
m-ou-se Jan 18, 2021
eb65d22
Rollup merge of #81161 - GuillaumeGomez:remove-inline-script, r=Nemo157
m-ou-se Jan 18, 2021
fd52951
Rollup merge of #81164 - eltociear:patch-5, r=jonas-schievink
m-ou-se Jan 18, 2021
c3fcaf8
Rollup merge of #81166 - RalfJung:debug-assert-comments, r=Mark-Simul…
m-ou-se Jan 18, 2021
1ae90bd
Rollup merge of #81168 - soniasingla:doc/sonia, r=jonas-schievink
m-ou-se Jan 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_mir/src/transform/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl NonConstOp for FnCallIndirect {

/// A function call where the callee is not marked as `const`.
#[derive(Debug)]
pub struct FnCallNonConst(pub DefId);
pub struct FnCallNonConst;
impl NonConstOp for FnCallNonConst {
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
struct_span_err!(
Expand Down
24 changes: 17 additions & 7 deletions compiler/rustc_mir/src/transform/check_consts/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,10 +789,10 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
}
}

#[instrument(skip(self))]
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
use rustc_target::spec::abi::Abi::RustIntrinsic;

trace!("visit_terminator: terminator={:?} location={:?}", terminator, location);
self.super_terminator(terminator, location);

match &terminator.kind {
Expand All @@ -816,8 +816,9 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {

// Attempting to call a trait method?
if let Some(trait_id) = tcx.trait_of_item(callee) {
trace!("attempting to call a trait method");
if !self.tcx.features().const_trait_impl {
self.check_op(ops::FnCallNonConst(callee));
self.check_op(ops::FnCallNonConst);
return;
}

Expand Down Expand Up @@ -871,25 +872,26 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
return;
}

let is_intrinsic = tcx.fn_sig(callee).abi() == RustIntrinsic;

// HACK: This is to "unstabilize" the `transmute` intrinsic
// within const fns. `transmute` is allowed in all other const contexts.
// This won't really scale to more intrinsics or functions. Let's allow const
// transmutes in const fn before we add more hacks to this.
if tcx.fn_sig(callee).abi() == RustIntrinsic
&& tcx.item_name(callee) == sym::transmute
{
if is_intrinsic && tcx.item_name(callee) == sym::transmute {
self.check_op(ops::Transmute);
return;
}

if !tcx.is_const_fn_raw(callee) {
self.check_op(ops::FnCallNonConst(callee));
self.check_op(ops::FnCallNonConst);
return;
}

// If the `const fn` we are trying to call is not const-stable, ensure that we have
// the proper feature gate enabled.
if let Some(gate) = is_unstable_const_fn(tcx, callee) {
trace!(?gate, "calling unstable const fn");
if self.span.allows_unstable(gate) {
return;
}
Expand All @@ -904,12 +906,14 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
// If this crate is not using stability attributes, or the caller is not claiming to be a
// stable `const fn`, that is all that is required.
if !self.ccx.is_const_stable_const_fn() {
trace!("crate not using stability attributes or caller not stably const");
return;
}

// Otherwise, we are something const-stable calling a const-unstable fn.

if super::rustc_allow_const_fn_unstable(tcx, caller, gate) {
trace!("rustc_allow_const_fn_unstable gate active");
return;
}

Expand All @@ -923,10 +927,16 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
let callee_is_unstable_unmarked = tcx.lookup_const_stability(callee).is_none()
&& tcx.lookup_stability(callee).map_or(false, |s| s.level.is_unstable());
if callee_is_unstable_unmarked {
if self.ccx.is_const_stable_const_fn() {
trace!("callee_is_unstable_unmarked");
// We do not use `const` modifiers for intrinsic "functions", as intrinsics are
// `extern` funtions, and these have no way to get marked `const`. So instead we
// use `rustc_const_(un)stable` attributes to mean that the intrinsic is `const`
if self.ccx.is_const_stable_const_fn() || is_intrinsic {
self.check_op(ops::FnCallUnstable(callee, None));
return;
}
}
trace!("permitting call");
}

// Forbid all `Drop` terminators unless the place being dropped is a local with no
Expand Down
17 changes: 9 additions & 8 deletions compiler/rustc_mir/src/transform/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,24 +568,23 @@ fn is_enclosed(
tcx: TyCtxt<'_>,
used_unsafe: &FxHashSet<hir::HirId>,
id: hir::HirId,
) -> Option<(String, hir::HirId)> {
unsafe_op_in_unsafe_fn_allowed: bool,
) -> Option<(&'static str, hir::HirId)> {
let parent_id = tcx.hir().get_parent_node(id);
if parent_id != id {
if used_unsafe.contains(&parent_id) {
Some(("block".to_string(), parent_id))
Some(("block", parent_id))
} else if let Some(Node::Item(&hir::Item {
kind: hir::ItemKind::Fn(ref sig, _, _), ..
})) = tcx.hir().find(parent_id)
{
if sig.header.unsafety == hir::Unsafety::Unsafe
&& !tcx.features().unsafe_block_in_unsafe_fn
{
Some(("fn".to_string(), parent_id))
if sig.header.unsafety == hir::Unsafety::Unsafe && unsafe_op_in_unsafe_fn_allowed {
Some(("fn", parent_id))
} else {
None
}
} else {
is_enclosed(tcx, used_unsafe, parent_id)
is_enclosed(tcx, used_unsafe, parent_id, unsafe_op_in_unsafe_fn_allowed)
}
} else {
None
Expand All @@ -598,7 +597,9 @@ fn report_unused_unsafe(tcx: TyCtxt<'_>, used_unsafe: &FxHashSet<hir::HirId>, id
let msg = "unnecessary `unsafe` block";
let mut db = lint.build(msg);
db.span_label(span, msg);
if let Some((kind, id)) = is_enclosed(tcx, used_unsafe, id) {
if let Some((kind, id)) =
is_enclosed(tcx, used_unsafe, id, unsafe_op_in_unsafe_fn_allowed(tcx, id))
{
db.span_label(
tcx.sess.source_map().guess_head_span(tcx.hir().span(id)),
format!("because it's nested under this `unsafe` {}", kind),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/build/matches/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// * the bindings from the previous iteration of the loop is prepended to the bindings from
// the current iteration (in the implementation this is done by mem::swap and extend)
// * after all iterations, these new bindings are then appended to the bindings that were
// prexisting (i.e. `candidate.binding` when the function was called).
// preexisting (i.e. `candidate.binding` when the function was called).
//
// example:
// candidate.bindings = [1, 2, 3]
Expand Down
70 changes: 60 additions & 10 deletions compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ impl InheritDeprecation {
}
}

/// Whether to inherit const stability flags for nested items. In most cases, we do not want to
/// inherit const stability: just because an enclosing `fn` is const-stable does not mean
/// all `extern` imports declared in it should be const-stable! However, trait methods
/// inherit const stability attributes from their parent and do not have their own.
enum InheritConstStability {
Yes,
No,
}

impl InheritConstStability {
fn yes(&self) -> bool {
matches!(self, InheritConstStability::Yes)
}
}

// A private tree-walker for producing an Index.
struct Annotator<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
Expand All @@ -75,6 +90,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
item_sp: Span,
kind: AnnotationKind,
inherit_deprecation: InheritDeprecation,
inherit_const_stability: InheritConstStability,
visit_children: F,
) where
F: FnOnce(&mut Self),
Expand Down Expand Up @@ -140,6 +156,8 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
const_stab
});

// `impl const Trait for Type` items forward their const stability to their
// immediate children.
if const_stab.is_none() {
debug!("annotate: const_stab not found, parent = {:?}", self.parent_const_stab);
if let Some(parent) = self.parent_const_stab {
Expand Down Expand Up @@ -228,7 +246,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
self.recurse_with_stability_attrs(
depr.map(|(d, _)| DeprecationEntry::local(d, hir_id)),
stab,
const_stab,
if inherit_const_stability.yes() { const_stab } else { None },
visit_children,
);
}
Expand Down Expand Up @@ -325,6 +343,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
fn visit_item(&mut self, i: &'tcx Item<'tcx>) {
let orig_in_trait_impl = self.in_trait_impl;
let mut kind = AnnotationKind::Required;
let mut const_stab_inherit = InheritConstStability::No;
match i.kind {
// Inherent impls and foreign modules serve only as containers for other items,
// they don't have their own stability. They still can be annotated as unstable
Expand All @@ -338,6 +357,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) => {
self.in_trait_impl = true;
kind = AnnotationKind::DeprecationProhibited;
const_stab_inherit = InheritConstStability::Yes;
}
hir::ItemKind::Struct(ref sd, _) => {
if let Some(ctor_hir_id) = sd.ctor_hir_id() {
Expand All @@ -347,16 +367,23 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
i.span,
AnnotationKind::Required,
InheritDeprecation::Yes,
InheritConstStability::No,
|_| {},
)
}
}
_ => {}
}

self.annotate(i.hir_id, &i.attrs, i.span, kind, InheritDeprecation::Yes, |v| {
intravisit::walk_item(v, i)
});
self.annotate(
i.hir_id,
&i.attrs,
i.span,
kind,
InheritDeprecation::Yes,
const_stab_inherit,
|v| intravisit::walk_item(v, i),
);
self.in_trait_impl = orig_in_trait_impl;
}

Expand All @@ -367,6 +394,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
ti.span,
AnnotationKind::Required,
InheritDeprecation::Yes,
InheritConstStability::No,
|v| {
intravisit::walk_trait_item(v, ti);
},
Expand All @@ -376,9 +404,17 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem<'tcx>) {
let kind =
if self.in_trait_impl { AnnotationKind::Prohibited } else { AnnotationKind::Required };
self.annotate(ii.hir_id, &ii.attrs, ii.span, kind, InheritDeprecation::Yes, |v| {
intravisit::walk_impl_item(v, ii);
});
self.annotate(
ii.hir_id,
&ii.attrs,
ii.span,
kind,
InheritDeprecation::Yes,
InheritConstStability::No,
|v| {
intravisit::walk_impl_item(v, ii);
},
);
}

fn visit_variant(&mut self, var: &'tcx Variant<'tcx>, g: &'tcx Generics<'tcx>, item_id: HirId) {
Expand All @@ -388,6 +424,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
var.span,
AnnotationKind::Required,
InheritDeprecation::Yes,
InheritConstStability::No,
|v| {
if let Some(ctor_hir_id) = var.data.ctor_hir_id() {
v.annotate(
Expand All @@ -396,6 +433,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
var.span,
AnnotationKind::Required,
InheritDeprecation::Yes,
InheritConstStability::No,
|_| {},
);
}
Expand All @@ -412,6 +450,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
s.span,
AnnotationKind::Required,
InheritDeprecation::Yes,
InheritConstStability::No,
|v| {
intravisit::walk_struct_field(v, s);
},
Expand All @@ -425,6 +464,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
i.span,
AnnotationKind::Required,
InheritDeprecation::Yes,
InheritConstStability::No,
|v| {
intravisit::walk_foreign_item(v, i);
},
Expand All @@ -438,6 +478,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
md.span,
AnnotationKind::Required,
InheritDeprecation::Yes,
InheritConstStability::No,
|_| {},
);
}
Expand All @@ -451,9 +492,17 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
_ => AnnotationKind::Prohibited,
};

self.annotate(p.hir_id, &p.attrs, p.span, kind, InheritDeprecation::No, |v| {
intravisit::walk_generic_param(v, p);
});
self.annotate(
p.hir_id,
&p.attrs,
p.span,
kind,
InheritDeprecation::No,
InheritConstStability::No,
|v| {
intravisit::walk_generic_param(v, p);
},
);
}
}

Expand Down Expand Up @@ -618,6 +667,7 @@ fn new_index(tcx: TyCtxt<'tcx>) -> Index<'tcx> {
krate.item.span,
AnnotationKind::Required,
InheritDeprecation::Yes,
InheritConstStability::No,
|v| intravisit::walk_crate(v, krate),
);
}
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_typeck/src/check/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ pub fn check_legal_trait_for_method_call(
tcx: TyCtxt<'_>,
span: Span,
receiver: Option<Span>,
expr_span: Span,
trait_id: DefId,
) {
if tcx.lang_items().drop_trait() == Some(trait_id) {
let mut err = struct_span_err!(tcx.sess, span, E0040, "explicit use of destructor method");
err.span_label(span, "explicit destructor calls not allowed");

let snippet = receiver
let (sp, suggestion) = receiver
.and_then(|s| tcx.sess.source_map().span_to_snippet(s).ok())
.unwrap_or_default();

let suggestion =
if snippet.is_empty() { "drop".to_string() } else { format!("drop({})", snippet) };
.filter(|snippet| !snippet.is_empty())
.map(|snippet| (expr_span, format!("drop({})", snippet)))
.unwrap_or_else(|| (span, "drop".to_string()));

err.span_suggestion(
span,
&format!("consider using `drop` function: `{}`", suggestion),
String::new(),
Applicability::Unspecified,
sp,
"consider using `drop` function",
suggestion,
Applicability::MaybeIncorrect,
);

err.emit();
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
debug!("instantiate_value_path: def_id={:?} container={:?}", def_id, container);
match container {
ty::TraitContainer(trait_did) => {
callee::check_legal_trait_for_method_call(tcx, span, None, trait_did)
callee::check_legal_trait_for_method_call(tcx, span, None, span, trait_did)
}
ty::ImplContainer(impl_def_id) => {
if segments.len() == 1 {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_typeck/src/check/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
self.tcx,
self.span,
Some(self.self_expr.span),
self.call_expr.span,
trait_def_id,
),
ty::ImplContainer(..) => {}
Expand Down
Loading