summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2020-07-20 12:30:18 -0700
committerGitHub <noreply@github.com>2020-07-20 12:30:18 -0700
commit991da05c32c98ec58f2f312b6dca96d998dbaf34 (patch)
treeb1fda1c2a39b52180b11aae32065ee47afb90a68
parent0897bc2403946f2d07df842292d5a6e2b5e5f4ec (diff)
parentc60a0356585d7b0378f50b129e7c7bcfa739f640 (diff)
downloadrust-991da05c32c98ec58f2f312b6dca96d998dbaf34.tar.gz
Rollup merge of #74051 - yodaldevoid:issue_60814, r=nikomatsakis
disallow non-static lifetimes in const generics Disallow non-static lifetimes in const generics in order to to patch over an ICE caused when we encounter a non-static lifetime in a const generic during borrow checking. This restriction may be relaxed in the future, but we need more discussion before then, and in the meantime we should still deal with this ICE. Fixes issue #60814
-rw-r--r--src/librustc_error_codes/error_codes.rs1
-rw-r--r--src/librustc_error_codes/error_codes/E0771.md23
-rw-r--r--src/librustc_resolve/late/diagnostics.rs18
-rw-r--r--src/librustc_resolve/late/lifetimes.rs11
-rw-r--r--src/test/ui/const-generics/const-argument-non-static-lifetime.rs17
-rw-r--r--src/test/ui/const-generics/const-argument-non-static-lifetime.stderr11
-rw-r--r--src/test/ui/error-codes/E0771.rs8
-rw-r--r--src/test/ui/error-codes/E0771.stderr20
8 files changed, 109 insertions, 0 deletions
diff --git a/src/librustc_error_codes/error_codes.rs b/src/librustc_error_codes/error_codes.rs
index bbbd8359f01..279c65ce03d 100644
--- a/src/librustc_error_codes/error_codes.rs
+++ b/src/librustc_error_codes/error_codes.rs
@@ -453,6 +453,7 @@ E0767: include_str!("./error_codes/E0767.md"),
E0768: include_str!("./error_codes/E0768.md"),
E0769: include_str!("./error_codes/E0769.md"),
E0770: include_str!("./error_codes/E0770.md"),
+E0771: include_str!("./error_codes/E0771.md"),
;
// E0006, // merged with E0005
// E0008, // cannot bind by-move into a pattern guard
diff --git a/src/librustc_error_codes/error_codes/E0771.md b/src/librustc_error_codes/error_codes/E0771.md
new file mode 100644
index 00000000000..824a955f6b3
--- /dev/null
+++ b/src/librustc_error_codes/error_codes/E0771.md
@@ -0,0 +1,23 @@
+A non-`'static` lifetime was used in a const generic. This is currently not
+allowed.
+
+Erroneous code example:
+
+```compile_fail,E0771
+#![feature(const_generics)]
+
+fn function_with_str<'a, const STRING: &'a str>() {} // error!
+```
+
+To fix this issue, the lifetime in the const generic need to be changed to
+`'static`:
+
+```
+#![feature(const_generics)]
+
+fn function_with_str<const STRING: &'static str>() {} // ok!
+```
+
+For more information, see [GitHub issue #74052].
+
+[GitHub issue #74052]: https://github.com/rust-lang/rust/issues/74052
diff --git a/src/librustc_resolve/late/diagnostics.rs b/src/librustc_resolve/late/diagnostics.rs
index 9323c15a941..c86b4141847 100644
--- a/src/librustc_resolve/late/diagnostics.rs
+++ b/src/librustc_resolve/late/diagnostics.rs
@@ -1141,6 +1141,24 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
err.emit();
}
+ // FIXME(const_generics): This patches over a ICE caused by non-'static lifetimes in const
+ // generics. We are disallowing this until we can decide on how we want to handle non-'static
+ // lifetimes in const generics. See issue #74052 for discussion.
+ crate fn emit_non_static_lt_in_const_generic_error(&self, lifetime_ref: &hir::Lifetime) {
+ let mut err = struct_span_err!(
+ self.tcx.sess,
+ lifetime_ref.span,
+ E0771,
+ "use of non-static lifetime `{}` in const generic",
+ lifetime_ref
+ );
+ err.note(
+ "for more information, see issue #74052 \
+ <https://github.com/rust-lang/rust/issues/74052>",
+ );
+ err.emit();
+ }
+
crate fn is_trait_ref_fn_scope(&mut self, trait_ref: &'tcx hir::PolyTraitRef<'tcx>) -> bool {
if let def::Res::Def(_, did) = trait_ref.trait_ref.path.res {
if [
diff --git a/src/librustc_resolve/late/lifetimes.rs b/src/librustc_resolve/late/lifetimes.rs
index 567db8edec9..6009e48a54f 100644
--- a/src/librustc_resolve/late/lifetimes.rs
+++ b/src/librustc_resolve/late/lifetimes.rs
@@ -173,6 +173,8 @@ crate struct LifetimeContext<'a, 'tcx> {
/// Used to disallow the use of in-band lifetimes in `fn` or `Fn` syntax.
is_in_fn_syntax: bool,
+ is_in_const_generic: bool,
+
/// List of labels in the function/method currently under analysis.
labels_in_fn: Vec<Ident>,
@@ -333,6 +335,7 @@ fn krate(tcx: TyCtxt<'_>) -> NamedRegionMap {
scope: ROOT_SCOPE,
trait_ref_hack: false,
is_in_fn_syntax: false,
+ is_in_const_generic: false,
labels_in_fn: vec![],
xcrate_object_lifetime_defaults: Default::default(),
lifetime_uses: &mut Default::default(),
@@ -828,6 +831,10 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
self.insert_lifetime(lifetime_ref, Region::Static);
return;
}
+ if self.is_in_const_generic && lifetime_ref.name != LifetimeName::Error {
+ self.emit_non_static_lt_in_const_generic_error(lifetime_ref);
+ return;
+ }
self.resolve_lifetime_ref(lifetime_ref);
}
@@ -860,8 +867,11 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
}
}
GenericParamKind::Const { ref ty, .. } => {
+ let was_in_const_generic = self.is_in_const_generic;
+ self.is_in_const_generic = true;
walk_list!(self, visit_param_bound, param.bounds);
self.visit_ty(&ty);
+ self.is_in_const_generic = was_in_const_generic;
}
}
}
@@ -1317,6 +1327,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
scope: &wrap_scope,
trait_ref_hack: self.trait_ref_hack,
is_in_fn_syntax: self.is_in_fn_syntax,
+ is_in_const_generic: self.is_in_const_generic,
labels_in_fn,
xcrate_object_lifetime_defaults,
lifetime_uses,
diff --git a/src/test/ui/const-generics/const-argument-non-static-lifetime.rs b/src/test/ui/const-generics/const-argument-non-static-lifetime.rs
new file mode 100644
index 00000000000..bc09ba2ab55
--- /dev/null
+++ b/src/test/ui/const-generics/const-argument-non-static-lifetime.rs
@@ -0,0 +1,17 @@
+// run-pass
+
+#![feature(const_generics)]
+//~^ WARN the feature `const_generics` is incomplete
+#![allow(dead_code)]
+
+fn test<const N: usize>() {}
+
+fn wow<'a>() -> &'a () {
+ test::<{
+ let _: &'a ();
+ 3
+ }>();
+ &()
+}
+
+fn main() {}
diff --git a/src/test/ui/const-generics/const-argument-non-static-lifetime.stderr b/src/test/ui/const-generics/const-argument-non-static-lifetime.stderr
new file mode 100644
index 00000000000..53a7550090d
--- /dev/null
+++ b/src/test/ui/const-generics/const-argument-non-static-lifetime.stderr
@@ -0,0 +1,11 @@
+warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes
+ --> $DIR/const-argument-non-static-lifetime.rs:3:12
+ |
+LL | #![feature(const_generics)]
+ | ^^^^^^^^^^^^^^
+ |
+ = note: `#[warn(incomplete_features)]` on by default
+ = note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
+
+warning: 1 warning emitted
+
diff --git a/src/test/ui/error-codes/E0771.rs b/src/test/ui/error-codes/E0771.rs
new file mode 100644
index 00000000000..ba359271940
--- /dev/null
+++ b/src/test/ui/error-codes/E0771.rs
@@ -0,0 +1,8 @@
+#![feature(const_generics)]
+//~^ WARN the feature `const_generics` is incomplete
+
+fn function_with_str<'a, const STRING: &'a str>() {} //~ ERROR E0771
+
+fn main() {
+ function_with_str::<"Hello, world!">()
+}
diff --git a/src/test/ui/error-codes/E0771.stderr b/src/test/ui/error-codes/E0771.stderr
new file mode 100644
index 00000000000..60220be6b57
--- /dev/null
+++ b/src/test/ui/error-codes/E0771.stderr
@@ -0,0 +1,20 @@
+warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes
+ --> $DIR/E0771.rs:1:12
+ |
+LL | #![feature(const_generics)]
+ | ^^^^^^^^^^^^^^
+ |
+ = note: `#[warn(incomplete_features)]` on by default
+ = note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
+
+error[E0771]: use of non-static lifetime `'a` in const generic
+ --> $DIR/E0771.rs:4:41
+ |
+LL | fn function_with_str<'a, const STRING: &'a str>() {}
+ | ^^
+ |
+ = note: for more information, see issue #74052 <https://github.com/rust-lang/rust/issues/74052>
+
+error: aborting due to previous error; 1 warning emitted
+
+For more information about this error, try `rustc --explain E0771`.