Skip to content

Commit 13170cd

Browse files
committed
Auto merge of rust-lang#134590 - matthiaskrgr:rollup-8lz2s62, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - rust-lang#123604 (Abstract `ProcThreadAttributeList` into its own struct) - rust-lang#128780 (Add `--doctest-compilation-args` option to add compilation flags to doctest compilation) - rust-lang#133782 (Precedence improvements: closures and jumps) - rust-lang#134509 (Arbitrary self types v2: niche deshadowing test) - rust-lang#134524 (Arbitrary self types v2: no deshadow pre feature.) - rust-lang#134539 (Restrict `#[non_exaustive]` on structs with default field values) - rust-lang#134586 (Also lint on option of function pointer comparisons) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 5f23ef7 + b7ac8d7 commit 13170cd

29 files changed

+779
-186
lines changed

compiler/rustc_ast/src/ast.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1322,11 +1322,15 @@ impl Expr {
13221322
}
13231323

13241324
pub fn precedence(&self) -> ExprPrecedence {
1325-
match self.kind {
1326-
ExprKind::Closure(..) => ExprPrecedence::Closure,
1325+
match &self.kind {
1326+
ExprKind::Closure(closure) => {
1327+
match closure.fn_decl.output {
1328+
FnRetTy::Default(_) => ExprPrecedence::Jump,
1329+
FnRetTy::Ty(_) => ExprPrecedence::Unambiguous,
1330+
}
1331+
}
13271332

13281333
ExprKind::Break(..)
1329-
| ExprKind::Continue(..)
13301334
| ExprKind::Ret(..)
13311335
| ExprKind::Yield(..)
13321336
| ExprKind::Yeet(..)
@@ -1360,6 +1364,7 @@ impl Expr {
13601364
| ExprKind::Block(..)
13611365
| ExprKind::Call(..)
13621366
| ExprKind::ConstBlock(_)
1367+
| ExprKind::Continue(..)
13631368
| ExprKind::Field(..)
13641369
| ExprKind::ForLoop { .. }
13651370
| ExprKind::FormatArgs(..)

compiler/rustc_ast/src/util/parser.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ impl AssocOp {
231231

232232
#[derive(Clone, Copy, PartialEq, PartialOrd)]
233233
pub enum ExprPrecedence {
234-
Closure,
235-
// return, break, yield
234+
// return, break, yield, closures
236235
Jump,
237236
// = += -= *= /= %= &= |= ^= <<= >>=
238237
Assign,

compiler/rustc_hir/src/hir.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1943,11 +1943,15 @@ pub struct Expr<'hir> {
19431943

19441944
impl Expr<'_> {
19451945
pub fn precedence(&self) -> ExprPrecedence {
1946-
match self.kind {
1947-
ExprKind::Closure { .. } => ExprPrecedence::Closure,
1946+
match &self.kind {
1947+
ExprKind::Closure(closure) => {
1948+
match closure.fn_decl.output {
1949+
FnRetTy::DefaultReturn(_) => ExprPrecedence::Jump,
1950+
FnRetTy::Return(_) => ExprPrecedence::Unambiguous,
1951+
}
1952+
}
19481953

19491954
ExprKind::Break(..)
1950-
| ExprKind::Continue(..)
19511955
| ExprKind::Ret(..)
19521956
| ExprKind::Yield(..)
19531957
| ExprKind::Become(..) => ExprPrecedence::Jump,
@@ -1973,6 +1977,7 @@ impl Expr<'_> {
19731977
| ExprKind::Block(..)
19741978
| ExprKind::Call(..)
19751979
| ExprKind::ConstBlock(_)
1980+
| ExprKind::Continue(..)
19761981
| ExprKind::Field(..)
19771982
| ExprKind::If(..)
19781983
| ExprKind::Index(..)
@@ -1990,7 +1995,7 @@ impl Expr<'_> {
19901995
| ExprKind::UnsafeBinderCast(..)
19911996
| ExprKind::Err(_) => ExprPrecedence::Unambiguous,
19921997

1993-
ExprKind::DropTemps(ref expr, ..) => expr.precedence(),
1998+
ExprKind::DropTemps(expr, ..) => expr.precedence(),
19941999
}
19952000
}
19962001

compiler/rustc_hir_typeck/src/method/probe.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,15 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
13291329
mutbl: hir::Mutability,
13301330
track_unstable_candidates: bool,
13311331
) -> Result<(), MethodError<'tcx>> {
1332+
// The errors emitted by this function are part of
1333+
// the arbitrary self types work, and should not impact
1334+
// other users.
1335+
if !self.tcx.features().arbitrary_self_types()
1336+
&& !self.tcx.features().arbitrary_self_types_pointers()
1337+
{
1338+
return Ok(());
1339+
}
1340+
13321341
// We don't want to remember any of the diagnostic hints from this
13331342
// shadow search, but we do need to provide Some/None for the
13341343
// unstable_candidates in order to reflect the behavior of the

compiler/rustc_lint/src/types.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ops::ControlFlow;
44
use rustc_abi::{BackendRepr, ExternAbi, TagEncoding, Variants, WrappingRange};
55
use rustc_data_structures::fx::FxHashSet;
66
use rustc_errors::DiagMessage;
7-
use rustc_hir::{Expr, ExprKind};
7+
use rustc_hir::{Expr, ExprKind, LangItem};
88
use rustc_middle::bug;
99
use rustc_middle::ty::layout::{LayoutOf, SizeSkeleton};
1010
use rustc_middle::ty::{
@@ -444,7 +444,25 @@ fn lint_fn_pointer<'tcx>(
444444
let (l_ty, l_ty_refs) = peel_refs(l_ty);
445445
let (r_ty, r_ty_refs) = peel_refs(r_ty);
446446

447-
if !l_ty.is_fn() || !r_ty.is_fn() {
447+
if l_ty.is_fn() && r_ty.is_fn() {
448+
// both operands are function pointers, fallthrough
449+
} else if let ty::Adt(l_def, l_args) = l_ty.kind()
450+
&& let ty::Adt(r_def, r_args) = r_ty.kind()
451+
&& cx.tcx.is_lang_item(l_def.did(), LangItem::Option)
452+
&& cx.tcx.is_lang_item(r_def.did(), LangItem::Option)
453+
&& let Some(l_some_arg) = l_args.get(0)
454+
&& let Some(r_some_arg) = r_args.get(0)
455+
&& l_some_arg.expect_ty().is_fn()
456+
&& r_some_arg.expect_ty().is_fn()
457+
{
458+
// both operands are `Option<{function ptr}>`
459+
return cx.emit_span_lint(
460+
UNPREDICTABLE_FUNCTION_POINTER_COMPARISONS,
461+
e.span,
462+
UnpredictableFunctionPointerComparisons::Warn,
463+
);
464+
} else {
465+
// types are not function pointers, nothing to do
448466
return;
449467
}
450468

compiler/rustc_passes/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,10 @@ passes_no_sanitize =
566566
`#[no_sanitize({$attr_str})]` should be applied to {$accepted_kind}
567567
.label = not {$accepted_kind}
568568
569+
passes_non_exaustive_with_default_field_values =
570+
`#[non_exhaustive]` can't be used to annotate items with default field values
571+
.label = this struct has default field values
572+
569573
passes_non_exported_macro_invalid_attrs =
570574
attribute should be applied to function or closure
571575
.label = not a function or closure

compiler/rustc_passes/src/check_attr.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
124124
[sym::coverage, ..] => self.check_coverage(attr, span, target),
125125
[sym::optimize, ..] => self.check_optimize(hir_id, attr, span, target),
126126
[sym::no_sanitize, ..] => self.check_no_sanitize(attr, span, target),
127-
[sym::non_exhaustive, ..] => self.check_non_exhaustive(hir_id, attr, span, target),
127+
[sym::non_exhaustive, ..] => self.check_non_exhaustive(hir_id, attr, span, target, item),
128128
[sym::marker, ..] => self.check_marker(hir_id, attr, span, target),
129129
[sym::target_feature, ..] => {
130130
self.check_target_feature(hir_id, attr, span, target, attrs)
@@ -685,9 +685,30 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
685685
}
686686

687687
/// Checks if the `#[non_exhaustive]` attribute on an `item` is valid.
688-
fn check_non_exhaustive(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
688+
fn check_non_exhaustive(
689+
&self,
690+
hir_id: HirId,
691+
attr: &Attribute,
692+
span: Span,
693+
target: Target,
694+
item: Option<ItemLike<'_>>,
695+
) {
689696
match target {
690-
Target::Struct | Target::Enum | Target::Variant => {}
697+
Target::Struct => {
698+
if let Some(ItemLike::Item(hir::Item {
699+
kind: hir::ItemKind::Struct(hir::VariantData::Struct { fields, .. }, _),
700+
..
701+
})) = item
702+
&& !fields.is_empty()
703+
&& fields.iter().any(|f| f.default.is_some())
704+
{
705+
self.dcx().emit_err(errors::NonExhaustiveWithDefaultFieldValues {
706+
attr_span: attr.span,
707+
defn_span: span,
708+
});
709+
}
710+
}
711+
Target::Enum | Target::Variant => {}
691712
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
692713
// `#[non_exhaustive]` attribute with just a lint, because we previously
693714
// erroneously allowed it and some crates used it accidentally, to be compatible

compiler/rustc_passes/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ pub(crate) struct NonExhaustiveWrongLocation {
119119
pub defn_span: Span,
120120
}
121121

122+
#[derive(Diagnostic)]
123+
#[diag(passes_non_exaustive_with_default_field_values)]
124+
pub(crate) struct NonExhaustiveWithDefaultFieldValues {
125+
#[primary_span]
126+
pub attr_span: Span,
127+
#[label]
128+
pub defn_span: Span,
129+
}
130+
122131
#[derive(Diagnostic)]
123132
#[diag(passes_should_be_applied_to_trait)]
124133
pub(crate) struct AttrShouldBeAppliedToTrait {

0 commit comments

Comments
 (0)