Skip to content

Commit

Permalink
Auto merge of #122854 - matthiaskrgr:rollup-9nnuo0z, r=matthiaskrgr
Browse files Browse the repository at this point in the history
Rollup of 11 pull requests

Successful merges:

 - #121881 (std::net: adding acceptfilter feature for netbsd/freebsd.)
 - #122817 (Doc Guarantee: BTree(Set|Map):  `IntoIter` Iterate in Sorted by key Order)
 - #122826 (Add tests for shortcomings of associated type bounds)
 - #122829 (Implement `FusedIterator` for `gen` block)
 - #122831 (make failure logs less verbose)
 - #122837 (add test for #122549)
 - #122838 (Avoid noop rewrite of issues.txt)
 - #122841 (add 2 more tests for issues fixed by #122749)
 - #122843 (Add a never type option to make diverging blocks `()`)
 - #122844 (add test for ice "cannot relate region: LUB(ReErased, ReError)")
 - #122845 (Clippy subtree update)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Mar 22, 2024
2 parents 0ad927c + 99e5618 commit b57a10c
Show file tree
Hide file tree
Showing 221 changed files with 4,882 additions and 1,115 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce"

[[package]]
name = "clippy"
version = "0.1.78"
version = "0.1.79"
dependencies = [
"anstream",
"clippy_config",
Expand Down Expand Up @@ -602,7 +602,7 @@ dependencies = [

[[package]]
name = "clippy_config"
version = "0.1.78"
version = "0.1.79"
dependencies = [
"rustc-semver",
"serde",
Expand All @@ -625,7 +625,7 @@ dependencies = [

[[package]]
name = "clippy_lints"
version = "0.1.78"
version = "0.1.79"
dependencies = [
"arrayvec",
"cargo_metadata 0.18.1",
Expand All @@ -650,7 +650,7 @@ dependencies = [

[[package]]
name = "clippy_utils"
version = "0.1.78"
version = "0.1.79"
dependencies = [
"arrayvec",
"clippy_config",
Expand Down Expand Up @@ -971,7 +971,7 @@ checksum = "a0afaad2b26fa326569eb264b1363e8ae3357618c43982b3f285f0774ce76b69"

[[package]]
name = "declare_clippy_lint"
version = "0.1.78"
version = "0.1.79"
dependencies = [
"itertools 0.12.1",
"quote",
Expand Down Expand Up @@ -2205,7 +2205,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19"
dependencies = [
"cfg-if",
"windows-targets 0.52.4",
"windows-targets 0.48.5",
]

[[package]]
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,12 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
),

rustc_attr!(
rustc_never_type_mode, Normal, template!(NameValueStr: "fallback_to_unit|fallback_to_niko|fallback_to_never|no_fallback"), ErrorFollowing,
rustc_never_type_options,
Normal,
template!(List: r#"/*opt*/ fallback = "unit|niko|never|no""#),
ErrorFollowing,
EncodeCrossCrate::No,
"`rustc_never_type_fallback` is used to experiment with never type fallback and work on \
"`rustc_never_type_options` is used to experiment with never type fallback and work on \
never type stabilization, and will never be stable"
),

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ language_item_table! {
FnOnceOutput, sym::fn_once_output, fn_once_output, Target::AssocTy, GenericRequirement::None;

Iterator, sym::iterator, iterator_trait, Target::Trait, GenericRequirement::Exact(0);
FusedIterator, sym::fused_iterator, fused_iterator_trait, Target::Trait, GenericRequirement::Exact(0);
Future, sym::future_trait, future_trait, Target::Trait, GenericRequirement::Exact(0);
AsyncIterator, sym::async_iterator, async_iterator_trait, Target::Trait, GenericRequirement::Exact(0);

Expand Down
36 changes: 4 additions & 32 deletions compiler/rustc_hir_typeck/src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ use rustc_data_structures::{
graph::{iterate::DepthFirstSearch, vec_graph::VecGraph},
unord::{UnordBag, UnordMap, UnordSet},
};
use rustc_hir::def_id::CRATE_DEF_ID;
use rustc_infer::infer::{DefineOpaqueTypes, InferOk};
use rustc_middle::ty::{self, Ty};
use rustc_span::sym;

enum DivergingFallbackBehavior {
#[derive(Copy, Clone)]
pub enum DivergingFallbackBehavior {
/// Always fallback to `()` (aka "always spontaneous decay")
FallbackToUnit,
/// Sometimes fallback to `!`, but mainly fallback to `()` so that most of the crates are not broken.
Expand Down Expand Up @@ -78,9 +77,8 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
return false;
}

let diverging_behavior = self.diverging_fallback_behavior();
let diverging_fallback =
self.calculate_diverging_fallback(&unresolved_variables, diverging_behavior);
let diverging_fallback = self
.calculate_diverging_fallback(&unresolved_variables, self.diverging_fallback_behavior);

// We do fallback in two passes, to try to generate
// better error messages.
Expand All @@ -94,32 +92,6 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
fallback_occurred
}

fn diverging_fallback_behavior(&self) -> DivergingFallbackBehavior {
let Some((mode, span)) = self
.tcx
.get_attr(CRATE_DEF_ID, sym::rustc_never_type_mode)
.map(|attr| (attr.value_str().unwrap(), attr.span))
else {
if self.tcx.features().never_type_fallback {
return DivergingFallbackBehavior::FallbackToNiko;
}

return DivergingFallbackBehavior::FallbackToUnit;
};

match mode {
sym::fallback_to_unit => DivergingFallbackBehavior::FallbackToUnit,
sym::fallback_to_niko => DivergingFallbackBehavior::FallbackToNiko,
sym::fallback_to_never => DivergingFallbackBehavior::FallbackToNever,
sym::no_fallback => DivergingFallbackBehavior::NoFallback,
_ => {
self.tcx.dcx().span_err(span, format!("unknown never type mode: `{mode}` (supported: `fallback_to_unit`, `fallback_to_niko`, `fallback_to_never` and `no_fallback`)"));

DivergingFallbackBehavior::FallbackToUnit
}
}
}

fn fallback_effects(&self) -> bool {
let unsolved_effects = self.unsolved_effects();

Expand Down
27 changes: 26 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,29 @@ use rustc_trait_selection::traits::{self, ObligationCauseCode, SelectionContext}
use std::iter;
use std::mem;

#[derive(Clone, Copy, Default)]
pub enum DivergingBlockBehavior {
/// This is the current stable behavior:
///
/// ```rust
/// {
/// return;
/// } // block has type = !, even though we are supposedly dropping it with `;`
/// ```
#[default]
Never,

/// Alternative behavior:
///
/// ```ignore (very-unstable-new-attribute)
/// #![rustc_never_type_options(diverging_block_default = "unit")]
/// {
/// return;
/// } // block has type = (), since we are dropping `!` from `return` with `;`
/// ```
Unit,
}

impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub(in super::super) fn check_casts(&mut self) {
// don't hold the borrow to deferred_cast_checks while checking to avoid borrow checker errors
Expand Down Expand Up @@ -1710,7 +1733,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
//
// #41425 -- label the implicit `()` as being the
// "found type" here, rather than the "expected type".
if !self.diverges.get().is_always() {
if !self.diverges.get().is_always()
|| matches!(self.diverging_block_behavior, DivergingBlockBehavior::Unit)
{
// #50009 -- Do not point at the entire fn block span, point at the return type
// span, as it is the cause of the requirement, and
// `consider_hint_about_removing_semicolon` will point at the last expression
Expand Down
73 changes: 72 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ mod checks;
mod suggestions;

use crate::coercion::DynamicCoerceMany;
use crate::fallback::DivergingFallbackBehavior;
use crate::fn_ctxt::checks::DivergingBlockBehavior;
use crate::{CoroutineTypes, Diverges, EnclosingBreakables, Inherited};
use hir::def_id::CRATE_DEF_ID;
use rustc_errors::{DiagCtxt, ErrorGuaranteed};
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
Expand All @@ -18,7 +21,7 @@ use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKin
use rustc_middle::ty::{self, Const, Ty, TyCtxt, TypeVisitableExt};
use rustc_session::Session;
use rustc_span::symbol::Ident;
use rustc_span::{self, Span, DUMMY_SP};
use rustc_span::{self, sym, Span, DUMMY_SP};
use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode, ObligationCtxt};

use std::cell::{Cell, RefCell};
Expand Down Expand Up @@ -108,6 +111,9 @@ pub struct FnCtxt<'a, 'tcx> {
pub(super) inh: &'a Inherited<'tcx>,

pub(super) fallback_has_occurred: Cell<bool>,

pub(super) diverging_fallback_behavior: DivergingFallbackBehavior,
pub(super) diverging_block_behavior: DivergingBlockBehavior,
}

impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Expand All @@ -116,6 +122,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
param_env: ty::ParamEnv<'tcx>,
body_id: LocalDefId,
) -> FnCtxt<'a, 'tcx> {
let (diverging_fallback_behavior, diverging_block_behavior) =
parse_never_type_options_attr(inh.tcx);
FnCtxt {
body_id,
param_env,
Expand All @@ -131,6 +139,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}),
inh,
fallback_has_occurred: Cell::new(false),
diverging_fallback_behavior,
diverging_block_behavior,
}
}

Expand Down Expand Up @@ -374,3 +384,64 @@ impl<'tcx> LoweredTy<'tcx> {
LoweredTy { raw, normalized }
}
}

fn parse_never_type_options_attr(
tcx: TyCtxt<'_>,
) -> (DivergingFallbackBehavior, DivergingBlockBehavior) {
use DivergingFallbackBehavior::*;

// Error handling is dubious here (unwraps), but that's probably fine for an internal attribute.
// Just don't write incorrect attributes <3

let mut fallback = None;
let mut block = None;

let items = tcx
.get_attr(CRATE_DEF_ID, sym::rustc_never_type_options)
.map(|attr| attr.meta_item_list().unwrap())
.unwrap_or_default();

for item in items {
if item.has_name(sym::fallback) && fallback.is_none() {
let mode = item.value_str().unwrap();
match mode {
sym::unit => fallback = Some(FallbackToUnit),
sym::niko => fallback = Some(FallbackToNiko),
sym::never => fallback = Some(FallbackToNever),
sym::no => fallback = Some(NoFallback),
_ => {
tcx.dcx().span_err(item.span(), format!("unknown never type fallback mode: `{mode}` (supported: `unit`, `niko`, `never` and `no`)"));
}
};
continue;
}

if item.has_name(sym::diverging_block_default) && fallback.is_none() {
let mode = item.value_str().unwrap();
match mode {
sym::unit => block = Some(DivergingBlockBehavior::Unit),
sym::never => block = Some(DivergingBlockBehavior::Never),
_ => {
tcx.dcx().span_err(item.span(), format!("unknown diverging block default: `{mode}` (supported: `unit` and `never`)"));
}
};
continue;
}

tcx.dcx().span_err(
item.span(),
format!(
"unknown never type option: `{}` (supported: `fallback`)",
item.name_or_empty()
),
);
}

let fallback = fallback.unwrap_or_else(|| {
if tcx.features().never_type_fallback { FallbackToNiko } else { FallbackToUnit }
});

let block = block.unwrap_or_default();

(fallback, block)
}
11 changes: 6 additions & 5 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ symbols! {
FromResidual,
FsOpenOptions,
FsPermissions,
FusedIterator,
Future,
FutureOutput,
GlobalAlloc,
Expand Down Expand Up @@ -690,6 +691,7 @@ symbols! {
dispatch_from_dyn,
div,
div_assign,
diverging_block_default,
do_not_recommend,
doc,
doc_alias,
Expand Down Expand Up @@ -815,9 +817,7 @@ symbols! {
fadd_algebraic,
fadd_fast,
fake_variadic,
fallback_to_never,
fallback_to_niko,
fallback_to_unit,
fallback,
fdiv_algebraic,
fdiv_fast,
feature,
Expand Down Expand Up @@ -886,6 +886,7 @@ symbols! {
fsub_algebraic,
fsub_fast,
fundamental,
fused_iterator,
future,
future_trait,
gdb_script_file,
Expand Down Expand Up @@ -1228,6 +1229,7 @@ symbols! {
new_v1,
new_v1_formatted,
next,
niko,
nll,
no,
no_builtins,
Expand All @@ -1236,7 +1238,6 @@ symbols! {
no_crate_inject,
no_debug,
no_default_passes,
no_fallback,
no_implicit_prelude,
no_inline,
no_link,
Expand Down Expand Up @@ -1554,7 +1555,7 @@ symbols! {
rustc_mir,
rustc_must_implement_one_of,
rustc_never_returns_null_ptr,
rustc_never_type_mode,
rustc_never_type_options,
rustc_no_mir_inline,
rustc_nonnull_optimization_guaranteed,
rustc_nounwind,
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_trait_selection/src/solve/assembly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ pub(super) trait GoalKind<'tcx>:
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx>;

/// A coroutine (that comes from a `gen` desugaring) is known to implement
/// `FusedIterator`
fn consider_builtin_fused_iterator_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx>;

fn consider_builtin_async_iterator_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
Expand Down Expand Up @@ -497,6 +504,8 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
G::consider_builtin_future_candidate(self, goal)
} else if lang_items.iterator_trait() == Some(trait_def_id) {
G::consider_builtin_iterator_candidate(self, goal)
} else if lang_items.fused_iterator_trait() == Some(trait_def_id) {
G::consider_builtin_fused_iterator_candidate(self, goal)
} else if lang_items.async_iterator_trait() == Some(trait_def_id) {
G::consider_builtin_async_iterator_candidate(self, goal)
} else if lang_items.coroutine_trait() == Some(trait_def_id) {
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_trait_selection/src/solve/normalizes_to/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,13 @@ impl<'tcx> assembly::GoalKind<'tcx> for NormalizesTo<'tcx> {
)
}

fn consider_builtin_fused_iterator_candidate(
_ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
bug!("`FusedIterator` does not have an associated type: {:?}", goal);
}

fn consider_builtin_async_iterator_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
Expand Down
Loading

0 comments on commit b57a10c

Please sign in to comment.