Skip to content

Commit 0a199e4

Browse files
committed
Auto merge of rust-lang#115758 - matthiaskrgr:rollup-khwbjj7, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#115335 (fix overflow in array length computation) - rust-lang#115440 (bootstrap/format: remove unnecessary paths.push) - rust-lang#115702 (Update mailmap) - rust-lang#115727 (Implement fallback for effect param) - rust-lang#115739 (Call `LateLintPass::check_attribute` from `with_lint_attrs`) - rust-lang#115743 (Point out if a local trait has no implementations) - rust-lang#115744 (Improve diagnostic for generic params from outer items (E0401)) - rust-lang#115752 (rustdoc: Add missing "Aliased type" title in the sidebar) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 3ebb562 + 9ed6eea commit 0a199e4

File tree

140 files changed

+1413
-299
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+1413
-299
lines changed

.mailmap

+4
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ Gareth Daniel Smith <[email protected]> gareth <gareth@gareth-N56VM.(n
205205
Gareth Daniel Smith <[email protected]> Gareth Smith <[email protected]>
206206
Gauri Kholkar <[email protected]>
207207
208+
Ghost <ghost> <[email protected]>
209+
Ghost <ghost> <[email protected]>
210+
Ghost <ghost> <[email protected]>
211+
Ghost <ghost> <[email protected]>
208212
Giles Cope <[email protected]>
209213
Glen De Cauwsemaecker <[email protected]>
210214
Graham Fawcett <[email protected]> Graham Fawcett <[email protected]>

compiler/rustc_error_codes/src/error_codes/E0401.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Inner items do not inherit type or const parameters from the functions
1+
Inner items do not inherit the generic parameters from the items
22
they are embedded in.
33

44
Erroneous code example:
@@ -32,8 +32,8 @@ fn foo<T>(x: T) {
3232
}
3333
```
3434

35-
Items inside functions are basically just like top-level items, except
36-
that they can only be used from the function they are in.
35+
Items nested inside other items are basically just like top-level items, except
36+
that they can only be used from the item they are in.
3737

3838
There are a couple of solutions for this.
3939

compiler/rustc_hir_analysis/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
523523
Ty::new_misc_error(tcx).into()
524524
}
525525
}
526-
GenericParamDefKind::Const { has_default } => {
526+
GenericParamDefKind::Const { has_default, .. } => {
527527
let ty = tcx
528528
.at(self.span)
529529
.type_of(param.def_id)

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
12551255

12561256
let is_our_default = |def: &ty::GenericParamDef| match def.kind {
12571257
GenericParamDefKind::Type { has_default, .. }
1258-
| GenericParamDefKind::Const { has_default } => {
1258+
| GenericParamDefKind::Const { has_default, .. } => {
12591259
has_default && def.index >= generics.parent_count as u32
12601260
}
12611261
GenericParamDefKind::Lifetime => unreachable!(),

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,10 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
328328
name: param.name.ident().name,
329329
def_id: param.def_id.to_def_id(),
330330
pure_wrt_drop: param.pure_wrt_drop,
331-
kind: ty::GenericParamDefKind::Const { has_default: default.is_some() },
331+
kind: ty::GenericParamDefKind::Const {
332+
has_default: default.is_some(),
333+
is_host_effect: is_host_param,
334+
},
332335
})
333336
}
334337
}));

compiler/rustc_hir_typeck/src/fallback.rs

+51-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_data_structures::{
44
graph::{iterate::DepthFirstSearch, vec_graph::VecGraph},
55
unord::{UnordBag, UnordMap, UnordSet},
66
};
7+
use rustc_infer::infer::{DefineOpaqueTypes, InferOk};
78
use rustc_middle::ty::{self, Ty};
89

910
impl<'tcx> FnCtxt<'_, 'tcx> {
@@ -23,20 +24,10 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
2324
self.fulfillment_cx.borrow_mut().pending_obligations()
2425
);
2526

26-
// Check if we have any unsolved variables. If not, no need for fallback.
27-
let unsolved_variables = self.unsolved_variables();
28-
if unsolved_variables.is_empty() {
29-
return;
30-
}
27+
let fallback_occured = self.fallback_types() || self.fallback_effects();
3128

32-
let diverging_fallback = self.calculate_diverging_fallback(&unsolved_variables);
33-
34-
// We do fallback in two passes, to try to generate
35-
// better error messages.
36-
// The first time, we do *not* replace opaque types.
37-
for ty in unsolved_variables {
38-
debug!("unsolved_variable = {:?}", ty);
39-
self.fallback_if_possible(ty, &diverging_fallback);
29+
if !fallback_occured {
30+
return;
4031
}
4132

4233
// We now see if we can make progress. This might cause us to
@@ -65,6 +56,53 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
6556
self.select_obligations_where_possible(|_| {});
6657
}
6758

59+
fn fallback_types(&self) -> bool {
60+
// Check if we have any unsolved variables. If not, no need for fallback.
61+
let unsolved_variables = self.unsolved_variables();
62+
63+
if unsolved_variables.is_empty() {
64+
return false;
65+
}
66+
67+
let diverging_fallback = self.calculate_diverging_fallback(&unsolved_variables);
68+
69+
// We do fallback in two passes, to try to generate
70+
// better error messages.
71+
// The first time, we do *not* replace opaque types.
72+
for ty in unsolved_variables {
73+
debug!("unsolved_variable = {:?}", ty);
74+
self.fallback_if_possible(ty, &diverging_fallback);
75+
}
76+
77+
true
78+
}
79+
80+
fn fallback_effects(&self) -> bool {
81+
let unsolved_effects = self.unsolved_effects();
82+
83+
if unsolved_effects.is_empty() {
84+
return false;
85+
}
86+
87+
// not setting `fallback_has_occured` here because that field is only used for type fallback
88+
// diagnostics.
89+
90+
for effect in unsolved_effects {
91+
let expected = self.tcx.consts.true_;
92+
let cause = self.misc(rustc_span::DUMMY_SP);
93+
match self.at(&cause, self.param_env).eq(DefineOpaqueTypes::Yes, expected, effect) {
94+
Ok(InferOk { obligations, value: () }) => {
95+
self.register_predicates(obligations);
96+
}
97+
Err(e) => {
98+
bug!("cannot eq unsolved effect: {e:?}")
99+
}
100+
}
101+
}
102+
103+
true
104+
}
105+
68106
// Tries to apply a fallback to `ty` if it is an unsolved variable.
69107
//
70108
// - Unconstrained ints are replaced with `i32`.

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+40-21
Original file line numberDiff line numberDiff line change
@@ -1295,17 +1295,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12951295
(GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
12961296
self.fcx.ty_infer(Some(param), inf.span).into()
12971297
}
1298-
(GenericParamDefKind::Const { .. }, GenericArg::Infer(inf)) => {
1298+
(
1299+
&GenericParamDefKind::Const { has_default, is_host_effect },
1300+
GenericArg::Infer(inf),
1301+
) => {
12991302
let tcx = self.fcx.tcx();
1300-
self.fcx
1301-
.ct_infer(
1302-
tcx.type_of(param.def_id)
1303-
.no_bound_vars()
1304-
.expect("const parameter types cannot be generic"),
1305-
Some(param),
1306-
inf.span,
1307-
)
1308-
.into()
1303+
1304+
if has_default && is_host_effect {
1305+
self.fcx.var_for_effect(param)
1306+
} else {
1307+
self.fcx
1308+
.ct_infer(
1309+
tcx.type_of(param.def_id)
1310+
.no_bound_vars()
1311+
.expect("const parameter types cannot be generic"),
1312+
Some(param),
1313+
inf.span,
1314+
)
1315+
.into()
1316+
}
13091317
}
13101318
_ => unreachable!(),
13111319
}
@@ -1324,7 +1332,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13241332
}
13251333
GenericParamDefKind::Type { has_default, .. } => {
13261334
if !infer_args && has_default {
1327-
// If we have a default, then we it doesn't matter that we're not
1335+
// If we have a default, then it doesn't matter that we're not
13281336
// inferring the type arguments: we provide the default where any
13291337
// is missing.
13301338
tcx.type_of(param.def_id).instantiate(tcx, args.unwrap()).into()
@@ -1336,17 +1344,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13361344
self.fcx.var_for_def(self.span, param)
13371345
}
13381346
}
1339-
GenericParamDefKind::Const { has_default } => {
1340-
if !infer_args
1341-
&& has_default
1342-
&& !tcx.has_attr(param.def_id, sym::rustc_host)
1343-
{
1344-
tcx.const_param_default(param.def_id)
1345-
.instantiate(tcx, args.unwrap())
1346-
.into()
1347-
} else {
1348-
self.fcx.var_for_def(self.span, param)
1347+
GenericParamDefKind::Const { has_default, is_host_effect } => {
1348+
if has_default {
1349+
// N.B. this is a bit of a hack. `infer_args` is passed depending on
1350+
// whether the user has provided generic args. E.g. for `Vec::new`
1351+
// we would have to infer the generic types. However, for `Vec::<T>::new`
1352+
// where the allocator param `A` has a default we will *not* infer. But
1353+
// for effect params this is a different story: if the user has not written
1354+
// anything explicit for the effect param, we always need to try to infer
1355+
// it before falling back to default, such that a `const fn` such as
1356+
// `needs_drop::<()>` can still be called in const contexts. (if we defaulted
1357+
// instead of inferred, typeck would error)
1358+
if is_host_effect {
1359+
return self.fcx.var_for_effect(param);
1360+
} else if !infer_args {
1361+
return tcx
1362+
.const_param_default(param.def_id)
1363+
.instantiate(tcx, args.unwrap())
1364+
.into();
1365+
}
13491366
}
1367+
1368+
self.fcx.var_for_def(self.span, param)
13501369
}
13511370
}
13521371
}

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,14 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
266266
param: Option<&ty::GenericParamDef>,
267267
span: Span,
268268
) -> Const<'tcx> {
269+
// FIXME ideally this shouldn't use unwrap
269270
match param {
271+
Some(
272+
param @ ty::GenericParamDef {
273+
kind: ty::GenericParamDefKind::Const { is_host_effect: true, .. },
274+
..
275+
},
276+
) => self.var_for_effect(param).as_const().unwrap(),
270277
Some(param) => self.var_for_def(span, param).as_const().unwrap(),
271278
None => self.next_const_var(
272279
ty,

compiler/rustc_hir_typeck/src/generator_interior/mod.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -643,17 +643,14 @@ fn check_must_not_suspend_ty<'tcx>(
643643
}
644644
ty::Array(ty, len) => {
645645
let descr_pre = &format!("{}array{} of ", data.descr_pre, plural_suffix);
646+
let target_usize =
647+
len.try_eval_target_usize(fcx.tcx, fcx.param_env).unwrap_or(0) as usize;
648+
let plural_len = target_usize.saturating_add(1);
646649
check_must_not_suspend_ty(
647650
fcx,
648651
ty,
649652
hir_id,
650-
SuspendCheckData {
651-
descr_pre,
652-
plural_len: len.try_eval_target_usize(fcx.tcx, fcx.param_env).unwrap_or(0)
653-
as usize
654-
+ 1,
655-
..data
656-
},
653+
SuspendCheckData { descr_pre, plural_len, ..data },
657654
)
658655
}
659656
// If drop tracking is enabled, we want to look through references, since the referent

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,17 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'cx, 'tcx> {
522522
}
523523
}
524524
}
525+
ty::ConstKind::Infer(InferConst::EffectVar(vid)) => {
526+
match self.infcx.probe_effect_var(vid) {
527+
Some(value) => return self.fold_const(value.as_const(self.infcx.tcx)),
528+
None => {
529+
return self.canonicalize_const_var(
530+
CanonicalVarInfo { kind: CanonicalVarKind::Effect },
531+
ct,
532+
);
533+
}
534+
}
535+
}
525536
ty::ConstKind::Infer(InferConst::Fresh(_)) => {
526537
bug!("encountered a fresh const during canonicalization")
527538
}
@@ -690,7 +701,8 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
690701
.iter()
691702
.map(|v| CanonicalVarInfo {
692703
kind: match v.kind {
693-
CanonicalVarKind::Ty(CanonicalTyVarKind::Int | CanonicalTyVarKind::Float) => {
704+
CanonicalVarKind::Ty(CanonicalTyVarKind::Int | CanonicalTyVarKind::Float)
705+
| CanonicalVarKind::Effect => {
694706
return *v;
695707
}
696708
CanonicalVarKind::Ty(CanonicalTyVarKind::General(u)) => {

compiler/rustc_infer/src/infer/canonical/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,11 @@ impl<'tcx> InferCtxt<'tcx> {
151151
universe_map(ui),
152152
)
153153
.into(),
154-
154+
CanonicalVarKind::Effect => {
155+
let vid = self.inner.borrow_mut().effect_unification_table().new_key(None);
156+
ty::Const::new_infer(self.tcx, ty::InferConst::EffectVar(vid), self.tcx.types.bool)
157+
.into()
158+
}
155159
CanonicalVarKind::PlaceholderConst(ty::PlaceholderConst { universe, bound }, ty) => {
156160
let universe_mapped = universe_map(universe);
157161
let placeholder_mapped = ty::PlaceholderConst { universe: universe_mapped, bound };

0 commit comments

Comments
 (0)