Skip to content

Commit e94bda3

Browse files
committed
Auto merge of rust-lang#111047 - compiler-errors:rtn-no-ty-ct-params, r=spastorino
Emit an error when return-type-notation is used with type/const params These are not intended to be supported initially, even though the compiler supports them internally...
2 parents 10b7e46 + b0eaaca commit e94bda3

File tree

8 files changed

+100
-44
lines changed

8 files changed

+100
-44
lines changed

compiler/rustc_hir_analysis/messages.ftl

+7
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ hir_analysis_return_type_notation_conflicting_bound =
195195
hir_analysis_return_type_notation_equality_bound =
196196
return type notation is not allowed to use type equality
197197
198+
hir_analysis_return_type_notation_illegal_param_const =
199+
return type notation is not allowed for functions that have const parameters
200+
.label = const parameter declared here
201+
hir_analysis_return_type_notation_illegal_param_type =
202+
return type notation is not allowed for functions that have type parameters
203+
.label = type parameter declared here
204+
198205
hir_analysis_return_type_notation_missing_method =
199206
cannot find associated function `{$assoc_name}` for `{$ty_name}`
200207

compiler/rustc_hir_analysis/src/astconv/mod.rs

+24-39
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
2626
use rustc_hir::def_id::{DefId, LocalDefId};
2727
use rustc_hir::intravisit::{walk_generics, Visitor as _};
2828
use rustc_hir::{GenericArg, GenericArgs, OpaqueTyOrigin};
29-
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
3029
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
3130
use rustc_infer::traits::ObligationCause;
32-
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
3331
use rustc_middle::middle::stability::AllowUnstable;
3432
use rustc_middle::ty::fold::FnMutDelegate;
3533
use rustc_middle::ty::subst::{self, GenericArgKind, InternalSubsts, SubstsRef};
@@ -1215,6 +1213,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12151213
}
12161214

12171215
let projection_ty = if return_type_notation {
1216+
let mut emitted_bad_param_err = false;
12181217
// If we have an method return type bound, then we need to substitute
12191218
// the method's early bound params with suitable late-bound params.
12201219
let mut num_bound_vars = candidate.bound_vars().len();
@@ -1230,16 +1229,35 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12301229
},
12311230
)
12321231
.into(),
1233-
GenericParamDefKind::Type { .. } => tcx
1234-
.mk_bound(
1232+
GenericParamDefKind::Type { .. } => {
1233+
if !emitted_bad_param_err {
1234+
tcx.sess.emit_err(
1235+
crate::errors::ReturnTypeNotationIllegalParam::Type {
1236+
span: path_span,
1237+
param_span: tcx.def_span(param.def_id),
1238+
},
1239+
);
1240+
emitted_bad_param_err = true;
1241+
}
1242+
tcx.mk_bound(
12351243
ty::INNERMOST,
12361244
ty::BoundTy {
12371245
var: ty::BoundVar::from_usize(num_bound_vars),
12381246
kind: ty::BoundTyKind::Param(param.def_id, param.name),
12391247
},
12401248
)
1241-
.into(),
1249+
.into()
1250+
}
12421251
GenericParamDefKind::Const { .. } => {
1252+
if !emitted_bad_param_err {
1253+
tcx.sess.emit_err(
1254+
crate::errors::ReturnTypeNotationIllegalParam::Const {
1255+
span: path_span,
1256+
param_span: tcx.def_span(param.def_id),
1257+
},
1258+
);
1259+
emitted_bad_param_err = true;
1260+
}
12431261
let ty = tcx
12441262
.type_of(param.def_id)
12451263
.no_bound_vars()
@@ -2472,7 +2490,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24722490
infcx.probe(|_| {
24732491
let ocx = ObligationCtxt::new_in_snapshot(&infcx);
24742492

2475-
let impl_substs = infcx.fresh_item_substs(impl_);
2493+
let impl_substs = infcx.fresh_substs_for_item(span, impl_);
24762494
let impl_ty = tcx.type_of(impl_).subst(tcx, impl_substs);
24772495
let impl_ty = ocx.normalize(&cause, param_env, impl_ty);
24782496

@@ -3759,36 +3777,3 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
37593777
}
37603778
}
37613779
}
3762-
3763-
pub trait InferCtxtExt<'tcx> {
3764-
fn fresh_item_substs(&self, def_id: DefId) -> SubstsRef<'tcx>;
3765-
}
3766-
3767-
impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
3768-
fn fresh_item_substs(&self, def_id: DefId) -> SubstsRef<'tcx> {
3769-
InternalSubsts::for_item(self.tcx, def_id, |param, _| match param.kind {
3770-
GenericParamDefKind::Lifetime => self.tcx.lifetimes.re_erased.into(),
3771-
GenericParamDefKind::Type { .. } => self
3772-
.next_ty_var(TypeVariableOrigin {
3773-
kind: TypeVariableOriginKind::SubstitutionPlaceholder,
3774-
span: self.tcx.def_span(def_id),
3775-
})
3776-
.into(),
3777-
GenericParamDefKind::Const { .. } => {
3778-
let span = self.tcx.def_span(def_id);
3779-
let origin = ConstVariableOrigin {
3780-
kind: ConstVariableOriginKind::SubstitutionPlaceholder,
3781-
span,
3782-
};
3783-
self.next_const_var(
3784-
self.tcx
3785-
.type_of(param.def_id)
3786-
.no_bound_vars()
3787-
.expect("const parameter types cannot be generic"),
3788-
origin,
3789-
)
3790-
.into()
3791-
}
3792-
})
3793-
}
3794-
}

compiler/rustc_hir_analysis/src/errors.rs

+18
Original file line numberDiff line numberDiff line change
@@ -857,3 +857,21 @@ pub(crate) enum DropImplPolarity {
857857
span: Span,
858858
},
859859
}
860+
861+
#[derive(Diagnostic)]
862+
pub(crate) enum ReturnTypeNotationIllegalParam {
863+
#[diag(hir_analysis_return_type_notation_illegal_param_type)]
864+
Type {
865+
#[primary_span]
866+
span: Span,
867+
#[label]
868+
param_span: Span,
869+
},
870+
#[diag(hir_analysis_return_type_notation_illegal_param_const)]
871+
Const {
872+
#[primary_span]
873+
span: Span,
874+
#[label]
875+
param_span: Span,
876+
},
877+
}

compiler/rustc_hir_typeck/src/method/probe.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_data_structures::fx::FxHashSet;
99
use rustc_errors::Applicability;
1010
use rustc_hir as hir;
1111
use rustc_hir::def::DefKind;
12-
use rustc_hir_analysis::astconv::InferCtxtExt as _;
1312
use rustc_hir_analysis::autoderef::{self, Autoderef};
1413
use rustc_infer::infer::canonical::OriginalQueryValues;
1514
use rustc_infer::infer::canonical::{Canonical, QueryResponse};
@@ -954,7 +953,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
954953
trait_def_id: DefId,
955954
) {
956955
debug!("assemble_extension_candidates_for_trait(trait_def_id={:?})", trait_def_id);
957-
let trait_substs = self.fresh_item_substs(trait_def_id);
956+
let trait_substs = self.fresh_substs_for_item(self.span, trait_def_id);
958957
let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, trait_substs);
959958

960959
if self.tcx.is_trait_alias(trait_def_id) {
@@ -1899,7 +1898,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
18991898
&self,
19001899
impl_def_id: DefId,
19011900
) -> (ty::EarlyBinder<Ty<'tcx>>, SubstsRef<'tcx>) {
1902-
(self.tcx.type_of(impl_def_id), self.fresh_item_substs(impl_def_id))
1901+
(self.tcx.type_of(impl_def_id), self.fresh_substs_for_item(self.span, impl_def_id))
19031902
}
19041903

19051904
/// Replaces late-bound-regions bound by `value` with `'static` using

compiler/rustc_infer/src/infer/type_variable.rs

-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ pub enum TypeVariableOriginKind {
129129
/// (before it has been determined).
130130
// FIXME(eddyb) distinguish upvar inference variables from the rest.
131131
ClosureSynthetic,
132-
SubstitutionPlaceholder,
133132
AutoDeref,
134133
AdjustmentType,
135134

compiler/rustc_middle/src/infer/unify_key.rs

-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ pub enum ConstVariableOriginKind {
116116
MiscVariable,
117117
ConstInference,
118118
ConstParameterDefinition(Symbol, DefId),
119-
SubstitutionPlaceholder,
120119
}
121120

122121
#[derive(Copy, Clone, Debug)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// edition: 2021
2+
3+
#![feature(async_fn_in_trait, return_type_notation)]
4+
//~^ WARN the feature `return_type_notation` is incomplete
5+
6+
trait Foo {
7+
async fn bar<T>() {}
8+
9+
async fn baz<const N: usize>() {}
10+
}
11+
12+
fn test<T>()
13+
where
14+
T: Foo<bar(): Send, baz(): Send>,
15+
//~^ ERROR return type notation is not allowed for functions that have const parameters
16+
//~| ERROR return type notation is not allowed for functions that have type parameters
17+
{
18+
}
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
warning: the feature `return_type_notation` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/ty-or-ct-params.rs:3:31
3+
|
4+
LL | #![feature(async_fn_in_trait, return_type_notation)]
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #109417 <https://github.com/rust-lang/rust/issues/109417> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
error: return type notation is not allowed for functions that have type parameters
11+
--> $DIR/ty-or-ct-params.rs:14:12
12+
|
13+
LL | async fn bar<T>() {}
14+
| - type parameter declared here
15+
...
16+
LL | T: Foo<bar(): Send, baz(): Send>,
17+
| ^^^^^^^^^^^
18+
19+
error: return type notation is not allowed for functions that have const parameters
20+
--> $DIR/ty-or-ct-params.rs:14:25
21+
|
22+
LL | async fn baz<const N: usize>() {}
23+
| -------------- const parameter declared here
24+
...
25+
LL | T: Foo<bar(): Send, baz(): Send>,
26+
| ^^^^^^^^^^^
27+
28+
error: aborting due to 2 previous errors; 1 warning emitted
29+

0 commit comments

Comments
 (0)