Skip to content
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

Rollup of 5 pull requests #98566

Merged
merged 13 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
148 changes: 66 additions & 82 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use rustc_attr as attr;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::{is_range_literal, Expr, ExprKind, Node};
use rustc_middle::ty::layout::{IntegerExt, LayoutOf, SizeSkeleton};
use rustc_middle::ty::subst::SubstsRef;
Expand Down Expand Up @@ -1483,39 +1482,32 @@ impl InvalidAtomicOrdering {
None
}

fn matches_ordering(cx: &LateContext<'_>, did: DefId, orderings: &[Symbol]) -> bool {
fn match_ordering(cx: &LateContext<'_>, ord_arg: &Expr<'_>) -> Option<Symbol> {
let ExprKind::Path(ref ord_qpath) = ord_arg.kind else { return None };
let did = cx.qpath_res(ord_qpath, ord_arg.hir_id).opt_def_id()?;
let tcx = cx.tcx;
let atomic_ordering = tcx.get_diagnostic_item(sym::Ordering);
orderings.iter().any(|ordering| {
tcx.item_name(did) == *ordering && {
let parent = tcx.parent(did);
Some(parent) == atomic_ordering
// needed in case this is a ctor, not a variant
|| tcx.opt_parent(parent) == atomic_ordering
}
})
}

fn opt_ordering_defid(cx: &LateContext<'_>, ord_arg: &Expr<'_>) -> Option<DefId> {
if let ExprKind::Path(ref ord_qpath) = ord_arg.kind {
cx.qpath_res(ord_qpath, ord_arg.hir_id).opt_def_id()
} else {
None
}
let name = tcx.item_name(did);
let parent = tcx.parent(did);
[sym::Relaxed, sym::Release, sym::Acquire, sym::AcqRel, sym::SeqCst].into_iter().find(
|&ordering| {
name == ordering
&& (Some(parent) == atomic_ordering
// needed in case this is a ctor, not a variant
|| tcx.opt_parent(parent) == atomic_ordering)
},
)
}

fn check_atomic_load_store(cx: &LateContext<'_>, expr: &Expr<'_>) {
use rustc_hir::def::{DefKind, Res};
use rustc_hir::QPath;
if let Some((method, args)) = Self::inherent_atomic_method_call(cx, expr, &[sym::load, sym::store])
&& let Some((ordering_arg, invalid_ordering)) = match method {
sym::load => Some((&args[1], sym::Release)),
sym::store => Some((&args[2], sym::Acquire)),
_ => None,
}
&& let ExprKind::Path(QPath::Resolved(_, path)) = ordering_arg.kind
&& let Res::Def(DefKind::Ctor(..), ctor_id) = path.res
&& Self::matches_ordering(cx, ctor_id, &[invalid_ordering, sym::AcqRel])
&& let Some(ordering) = Self::match_ordering(cx, ordering_arg)
&& (ordering == invalid_ordering || ordering == sym::AcqRel)
{
cx.struct_span_lint(INVALID_ATOMIC_ORDERING, ordering_arg.span, |diag| {
if method == sym::load {
Expand All @@ -1537,9 +1529,7 @@ impl InvalidAtomicOrdering {
&& let ExprKind::Path(ref func_qpath) = func.kind
&& let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id()
&& matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::fence | sym::compiler_fence))
&& let ExprKind::Path(ref ordering_qpath) = &args[0].kind
&& let Some(ordering_def_id) = cx.qpath_res(ordering_qpath, args[0].hir_id).opt_def_id()
&& Self::matches_ordering(cx, ordering_def_id, &[sym::Relaxed])
&& Self::match_ordering(cx, &args[0]) == Some(sym::Relaxed)
{
cx.struct_span_lint(INVALID_ATOMIC_ORDERING, args[0].span, |diag| {
diag.build("memory fences cannot have `Relaxed` ordering")
Expand All @@ -1550,62 +1540,56 @@ impl InvalidAtomicOrdering {
}

fn check_atomic_compare_exchange(cx: &LateContext<'_>, expr: &Expr<'_>) {
if let Some((method, args)) = Self::inherent_atomic_method_call(cx, expr, &[sym::fetch_update, sym::compare_exchange, sym::compare_exchange_weak])
&& let Some((success_order_arg, failure_order_arg)) = match method {
sym::fetch_update => Some((&args[1], &args[2])),
sym::compare_exchange | sym::compare_exchange_weak => Some((&args[3], &args[4])),
_ => None,
}
&& let Some(fail_ordering_def_id) = Self::opt_ordering_defid(cx, failure_order_arg)
{
// Helper type holding on to some checking and error reporting data. Has
// - (success ordering,
// - list of failure orderings forbidden by the success order,
// - suggestion message)
type OrdLintInfo = (Symbol, &'static [Symbol], &'static str);
const RELAXED: OrdLintInfo = (sym::Relaxed, &[sym::SeqCst, sym::Acquire], "ordering mode `Relaxed`");
const ACQUIRE: OrdLintInfo = (sym::Acquire, &[sym::SeqCst], "ordering modes `Acquire` or `Relaxed`");
const SEQ_CST: OrdLintInfo = (sym::SeqCst, &[], "ordering modes `Acquire`, `SeqCst` or `Relaxed`");
const RELEASE: OrdLintInfo = (sym::Release, RELAXED.1, RELAXED.2);
const ACQREL: OrdLintInfo = (sym::AcqRel, ACQUIRE.1, ACQUIRE.2);
const SEARCH: [OrdLintInfo; 5] = [RELAXED, ACQUIRE, SEQ_CST, RELEASE, ACQREL];

let success_lint_info = Self::opt_ordering_defid(cx, success_order_arg)
.and_then(|success_ord_def_id| -> Option<OrdLintInfo> {
SEARCH
.iter()
.copied()
.find(|(ordering, ..)| {
Self::matches_ordering(cx, success_ord_def_id, &[*ordering])
})
});
if Self::matches_ordering(cx, fail_ordering_def_id, &[sym::Release, sym::AcqRel]) {
// If we don't know the success order is, use what we'd suggest
// if it were maximally permissive.
let suggested = success_lint_info.unwrap_or(SEQ_CST).2;
cx.struct_span_lint(INVALID_ATOMIC_ORDERING, failure_order_arg.span, |diag| {
let msg = format!(
"{}'s failure ordering may not be `Release` or `AcqRel`",
method,
);
diag.build(&msg)
.help(&format!("consider using {} instead", suggested))
.emit();
});
} else if let Some((success_ord, bad_ords_given_success, suggested)) = success_lint_info {
if Self::matches_ordering(cx, fail_ordering_def_id, bad_ords_given_success) {
cx.struct_span_lint(INVALID_ATOMIC_ORDERING, failure_order_arg.span, |diag| {
let msg = format!(
"{}'s failure ordering may not be stronger than the success ordering of `{}`",
method,
success_ord,
);
diag.build(&msg)
.help(&format!("consider using {} instead", suggested))
.emit();
});
}
}
let Some((method, args)) = Self::inherent_atomic_method_call(cx, expr, &[sym::fetch_update, sym::compare_exchange, sym::compare_exchange_weak])
else {return };

let (success_order_arg, fail_order_arg) = match method {
sym::fetch_update => (&args[1], &args[2]),
sym::compare_exchange | sym::compare_exchange_weak => (&args[3], &args[4]),
_ => return,
};

let Some(fail_ordering) = Self::match_ordering(cx, fail_order_arg) else { return };

if matches!(fail_ordering, sym::Release | sym::AcqRel) {
cx.struct_span_lint(INVALID_ATOMIC_ORDERING, fail_order_arg.span, |diag| {
diag.build(&format!(
"`{method}`'s failure ordering may not be `Release` or `AcqRel`, \
since a failed `{method}` does not result in a write",
))
.span_label(fail_order_arg.span, "invalid failure ordering")
.help("consider using `Acquire` or `Relaxed` failure ordering instead")
.emit();
});
}

let Some(success_ordering) = Self::match_ordering(cx, success_order_arg) else { return };

if matches!(
(success_ordering, fail_ordering),
(sym::Relaxed | sym::Release, sym::Acquire)
| (sym::Relaxed | sym::Release | sym::Acquire | sym::AcqRel, sym::SeqCst)
) {
let success_suggestion =
if success_ordering == sym::Release && fail_ordering == sym::Acquire {
sym::AcqRel
} else {
fail_ordering
};
cx.struct_span_lint(INVALID_ATOMIC_ORDERING, success_order_arg.span, |diag| {
diag.build(&format!(
"`{method}`'s success ordering must be at least as strong as its failure ordering"
))
.span_label(fail_order_arg.span, format!("`{fail_ordering}` failure ordering"))
.span_label(success_order_arg.span, format!("`{success_ordering}` success ordering"))
.span_suggestion_short(
success_order_arg.span,
format!("consider using `{success_suggestion}` success ordering instead"),
format!("std::sync::atomic::Ordering::{success_suggestion}"),
Applicability::MaybeIncorrect,
)
.emit();
});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ pub enum ObligationCauseCode<'tcx> {
QuestionMark,

/// Well-formed checking. If a `WellFormedLoc` is provided,
/// then it will be used to eprform HIR-based wf checking
/// then it will be used to perform HIR-based wf checking
/// after an error occurs, in order to generate a more precise error span.
/// This is purely for diagnostic purposes - it is always
/// correct to use `MiscObligation` instead, or to specify
Expand Down
19 changes: 10 additions & 9 deletions compiler/rustc_typeck/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,15 @@ fn check_type_defn<'tcx, F>(
let packed = tcx.adt_def(item.def_id).repr().packed();

for variant in &variants {
// All field types must be well-formed.
for field in &variant.fields {
fcx.register_wf_obligation(
field.ty.into(),
field.span,
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(field.def_id))),
)
}

// For DST, or when drop needs to copy things around, all
// intermediate types must be sized.
let needs_drop_copy = || {
Expand All @@ -1006,6 +1015,7 @@ fn check_type_defn<'tcx, F>(
}
}
};
// All fields (except for possibly the last) should be sized.
let all_sized = all_sized || variant.fields.is_empty() || needs_drop_copy();
let unsized_len = if all_sized { 0 } else { 1 };
for (idx, field) in
Expand All @@ -1030,15 +1040,6 @@ fn check_type_defn<'tcx, F>(
);
}

// All field types must be well-formed.
for field in &variant.fields {
fcx.register_wf_obligation(
field.ty.into(),
field.span,
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(field.def_id))),
)
}

// Explicit `enum` discriminant values must const-evaluate successfully.
if let Some(discr_def_id) = variant.explicit_discr {
let discr_substs = InternalSubsts::identity_for_item(tcx, discr_def_id.to_def_id());
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ macro_rules! nonzero_leading_trailing_zeros {
without modifying the original"]
#[inline]
pub const fn leading_zeros(self) -> u32 {
// SAFETY: since `self` can not be zero it is safe to call ctlz_nonzero
// SAFETY: since `self` cannot be zero, it is safe to call `ctlz_nonzero`.
unsafe { intrinsics::ctlz_nonzero(self.0 as $Uint) as u32 }
}

Expand All @@ -237,7 +237,7 @@ macro_rules! nonzero_leading_trailing_zeros {
without modifying the original"]
#[inline]
pub const fn trailing_zeros(self) -> u32 {
// SAFETY: since `self` can not be zero it is safe to call cttz_nonzero
// SAFETY: since `self` cannot be zero, it is safe to call `cttz_nonzero`.
unsafe { intrinsics::cttz_nonzero(self.0 as $Uint) as u32 }
}

Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub(crate) fn maybe_download_ci_llvm(builder: &Builder<'_>) {
// files in the tarball are in the past, so it doesn't trigger a
// rebuild.
let now = filetime::FileTime::from_system_time(std::time::SystemTime::now());
let llvm_config = llvm_root.join("bin/llvm-config");
let llvm_config = llvm_root.join("bin").join(exe("llvm-config", builder.config.build));
t!(filetime::set_file_times(&llvm_config, now, now));

let llvm_lib = llvm_root.join("lib");
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/chalkify/bugs/async.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// check-fail
// known-bug
// known-bug: unknown
// compile-flags: -Z chalk --edition=2021

fn main() -> () {}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/generic-associated-types/bugs/issue-80626.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// check-fail
// known-bug
// known-bug: #80626

// This should pass, but it requires `Sized` to be coinductive.

Expand Down
13 changes: 4 additions & 9 deletions src/test/ui/generic-associated-types/bugs/issue-80626.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ error[E0275]: overflow evaluating the requirement `LinkedList<A>: Sized`
LL | Next(A::Allocated<Self>)
| ^^^^^^^^^^^^^^^^^^
|
= note: no field of an enum variant may have a dynamically sized type
= help: change the field's type to have a statically known size
help: borrowed types always have a statically known size
note: required by a bound in `Allocator::Allocated`
--> $DIR/issue-80626.rs:9:20
|
LL | Next(&A::Allocated<Self>)
| +
help: the `Box` type always has a statically known size and allocates its contents in the heap
|
LL | Next(Box<A::Allocated<Self>>)
| ++++ +
LL | type Allocated<T>;
| ^ required by this bound in `Allocator::Allocated`

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/generic-associated-types/bugs/issue-86218.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// check-fail
// known-bug
// known-bug: #86218

// This should pass, but seems to run into a TAIT issue.

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/generic-associated-types/bugs/issue-87735.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// check-fail
// known-bug
// known-bug: #87735, #88526

// This should pass, but we need an extension of implied bounds (probably).

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/generic-associated-types/bugs/issue-87748.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// check-fail
// known-bug
// known-bug: #87748

// This should pass, but unnormalized input args aren't treated as implied.

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/generic-associated-types/bugs/issue-87755.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// check-fail
// known-bug
// known-bug: #87755

// This should pass.

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/generic-associated-types/bugs/issue-87803.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// check-fail
// known-bug
// known-bug: #87803

// This should pass, but using a type alias vs a reference directly
// changes late-bound -> early-bound.
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/generic-associated-types/bugs/issue-88382.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// check-fail
// known-bug
// known-bug: #88382

// This should pass, but has a missed normalization due to HRTB.

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/generic-associated-types/bugs/issue-88460.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// check-fail
// known-bug
// known-bug: #88460

// This should pass, but has a missed normalization due to HRTB.

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/generic-associated-types/bugs/issue-88526.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// check-fail
// known-bug
// known-bug: #88526

// This should pass, but requires more logic.

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/generic-associated-types/bugs/issue-89008.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// check-fail
// edition:2021
// known-bug
// known-bug: #88908

// This should pass, but seems to run into a TAIT bug.

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/hrtb/issue-95034.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// known-bug
// known-bug: #95034
// failure-status: 101
// compile-flags: --edition=2021 --crate-type=lib
// rustc-env:RUST_BACKTRACE=0
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-47511.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// check-fail
// known-bug
// known-bug: #47511

// Regression test for #47511: anonymous lifetimes can appear
// unconstrained in a return type, but only if they appear just once
Expand Down
Loading