Skip to content

Commit a75fed7

Browse files
committed
Auto merge of #119042 - bvanjoi:fix-118697-2, r=compiler-errors
fallback `default` to `None` during ast-lowering for lifetime binder Fixes #118697 This is another attempt. It has a fallback, setting `default` to `None` and emit an error for non-lifetime binders during ast lowering. r? `@compiler-errors`
2 parents e1fadb2 + e16efbd commit a75fed7

File tree

6 files changed

+69
-2
lines changed

6 files changed

+69
-2
lines changed

compiler/rustc_ast_lowering/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ ast_lowering_closure_cannot_be_static = closures cannot be static
4545
ast_lowering_coroutine_too_many_parameters =
4646
too many parameters for a coroutine (expected 0 or 1 parameters)
4747
48+
ast_lowering_default_parameter_in_binder = default parameter is not allowed in this binder
49+
4850
ast_lowering_does_not_support_modifiers =
4951
the `{$class_name}` register class does not support template modifiers
5052

compiler/rustc_ast_lowering/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,10 @@ pub enum BadReturnTypeNotation {
395395
span: Span,
396396
},
397397
}
398+
399+
#[derive(Diagnostic)]
400+
#[diag(ast_lowering_default_parameter_in_binder)]
401+
pub(crate) struct UnexpectedDefaultParameterInBinder {
402+
#[primary_span]
403+
pub span: Span,
404+
}

compiler/rustc_ast_lowering/src/lib.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#![allow(internal_features)]
3434
#![feature(rustdoc_internals)]
3535
#![doc(rust_logo)]
36+
#![feature(if_let_guard)]
3637
#![feature(box_patterns)]
3738
#![feature(let_chains)]
3839
#![recursion_limit = "256"]
@@ -65,6 +66,7 @@ use rustc_session::parse::{add_feature_diagnostics, feature_err};
6566
use rustc_span::symbol::{kw, sym, Ident, Symbol};
6667
use rustc_span::{DesugaringKind, Span, DUMMY_SP};
6768
use smallvec::SmallVec;
69+
use std::borrow::Cow;
6870
use std::collections::hash_map::Entry;
6971
use thin_vec::ThinVec;
7072

@@ -878,8 +880,27 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
878880
binder: NodeId,
879881
generic_params: &[GenericParam],
880882
) -> &'hir [hir::GenericParam<'hir>] {
881-
let mut generic_params: Vec<_> = self
882-
.lower_generic_params_mut(generic_params, hir::GenericParamSource::Binder)
883+
let mut generic_params: Vec<_> = generic_params
884+
.iter()
885+
.map(|param| {
886+
let param = match param.kind {
887+
GenericParamKind::Type { ref default } if let Some(ty) = default => {
888+
// Default type is not permitted in non-lifetime binders.
889+
// So we emit an error and default to `None` to prevent
890+
// potential ice.
891+
self.dcx().emit_err(errors::UnexpectedDefaultParameterInBinder {
892+
span: ty.span(),
893+
});
894+
let param = GenericParam {
895+
kind: GenericParamKind::Type { default: None },
896+
..param.clone()
897+
};
898+
Cow::Owned(param)
899+
}
900+
_ => Cow::Borrowed(param),
901+
};
902+
self.lower_generic_param(param.as_ref(), hir::GenericParamSource::Binder)
903+
})
883904
.collect();
884905
let extra_lifetimes = self.resolver.take_extra_lifetime_params(binder);
885906
debug!(?extra_lifetimes);

tests/ui/parser/issue-119042.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// check-pass
2+
3+
macro_rules! a { ($ty:ty) => {} }
4+
5+
a! { for<T = &i32> fn() }
6+
7+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![allow(incomplete_features)]
2+
#![feature(non_lifetime_binders)]
3+
4+
type T = dyn for<V = A(&())> Fn(());
5+
//~^ ERROR default parameter is not allowed in this binder
6+
//~| ERROR cannot find type `A` in this scope
7+
//~| ERROR late-bound type parameter not allowed on trait object types
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0412]: cannot find type `A` in this scope
2+
--> $DIR/issue-118697.rs:4:22
3+
|
4+
LL | type T = dyn for<V = A(&())> Fn(());
5+
| ^ not found in this scope
6+
7+
error: default parameter is not allowed in this binder
8+
--> $DIR/issue-118697.rs:4:22
9+
|
10+
LL | type T = dyn for<V = A(&())> Fn(());
11+
| ^^^^^^
12+
13+
error: late-bound type parameter not allowed on trait object types
14+
--> $DIR/issue-118697.rs:4:18
15+
|
16+
LL | type T = dyn for<V = A(&())> Fn(());
17+
| ^
18+
19+
error: aborting due to 3 previous errors
20+
21+
For more information about this error, try `rustc --explain E0412`.

0 commit comments

Comments
 (0)