Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add static_in_const feature gate #36332

Merged
merged 5 commits into from
Sep 10, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
@@ -1567,7 +1567,7 @@ fn type_of_def_id<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
NodeItem(item) => {
match item.node {
ItemStatic(ref t, ..) | ItemConst(ref t, _) => {
ccx.icx(&()).to_ty(&ElidableRscope::new(ty::ReStatic), &t)
ccx.icx(&()).to_ty(&StaticRscope::new(&ccx.tcx), &t)
}
ItemFn(ref decl, unsafety, _, abi, ref generics, _) => {
let tofd = AstConv::ty_of_bare_fn(&ccx.icx(generics), unsafety, abi, &decl,
39 changes: 39 additions & 0 deletions src/librustc_typeck/rscope.rs
Original file line number Diff line number Diff line change
@@ -213,6 +213,45 @@ impl RegionScope for ElidableRscope {
}
}

/// A scope that behaves as an ElidabeRscope with a `'static` default region
/// that should also warn if the `static_in_const` feature is unset.
#[derive(Copy, Clone)]
pub struct StaticRscope<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
tcx: &'a ty::TyCtxt<'a, 'gcx, 'tcx>,
}

impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> StaticRscope<'a, 'gcx, 'tcx> {
/// create a new StaticRscope from a reference to the `TyCtxt`
pub fn new(tcx: &'a ty::TyCtxt<'a, 'gcx, 'tcx>) -> Self {
StaticRscope { tcx: tcx }
}
}

impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> RegionScope for StaticRscope<'a, 'gcx, 'tcx> {
fn anon_regions(&self,
_span: Span,
count: usize)
-> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
Ok(vec![ty::ReStatic; count])
}

fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
Some(self.base_object_lifetime_default(span))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see the problem; these routines should be the same as for elidable rscope:

    fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
        // Per RFC #599, object-lifetimes default to 'static unless
        // overridden by context, and this takes precedence over
        // lifetime elision.
        Some(self.base_object_lifetime_default(span))
    }

    fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
        ty::ReStatic
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I report the error in anon_regions(..) then?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I report the error in anon_regions(..) then?

Yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done.

}

fn base_object_lifetime_default(&self, span: Span) -> ty::Region {
if !self.tcx.sess.features.borrow().static_in_const {
self.tcx
.sess
.struct_span_warn(span,
"This needs a `'static` lifetime or the \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: it should be this not This, and this should be an error :)

`static_in_const` feature, see #35897")
.emit();
}
ty::ReStatic
}
}

/// A scope in which we generate anonymous, late-bound regions for
/// omitted regions. This occurs in function signatures.
pub struct BindingRscope {
3 changes: 3 additions & 0 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
@@ -295,6 +295,9 @@ declare_features! (

// Allows untagged unions `union U { ... }`
(active, untagged_unions, "1.13.0", Some(32836)),

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// If you change this list without updating src/doc/reference.md, @cmr will be sad

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the heads-up, added.

// elide `'static` lifetimes in `static`s and `const`s
(active, static_in_const, "1.13.0", Some(35897)),
);

declare_features! (
2 changes: 2 additions & 0 deletions src/test/compile-fail/const-unsized.rs
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ const CONST_0: Debug+Sync = *(&0 as &(Debug+Sync));
//~| NOTE `std::fmt::Debug + Sync + 'static: std::marker::Sized` not satisfied
//~| NOTE does not have a constant size known at compile-time
//~| NOTE constant expressions must have a statically known size
//~| WARNING This needs a `'static` lifetime or the `static_in_const` feature

const CONST_FOO: str = *"foo";
//~^ ERROR `str: std::marker::Sized` is not satisfied
@@ -27,6 +28,7 @@ static STATIC_1: Debug+Sync = *(&1 as &(Debug+Sync));
//~| NOTE `std::fmt::Debug + Sync + 'static: std::marker::Sized` not satisfied
//~| NOTE does not have a constant size known at compile-time
//~| NOTE constant expressions must have a statically known size
//~| WARNING This needs a `'static` lifetime or the `static_in_const` feature

static STATIC_BAR: str = *"bar";
//~^ ERROR `str: std::marker::Sized` is not satisfied
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-24446.rs
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ fn main() {
static foo: Fn() -> u32 = || -> u32 {
//~^ ERROR: mismatched types
//~| ERROR: `std::ops::Fn() -> u32 + 'static: std::marker::Sized` is not satisfied

//~| WARNING: This needs a `'static` lifetime or the `static_in_const` feature
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These cases seem like bugs -- see my comment above.

0
};
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/rfc1623.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(static_in_const)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can you make another test that specifically uses the feature w/o the feature gate and check that it reports an error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing.

#![allow(dead_code)]

fn non_elidable<'a, 'b>(a: &'a u8, b: &'b u8) -> &'a u8 {
98 changes: 0 additions & 98 deletions src/test/compile-fail/rfc1623.rs.bk

This file was deleted.

1 change: 1 addition & 0 deletions src/test/run-pass/rfc1623.rs
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(static_in_const)]
#![allow(dead_code)]

// very simple test for a 'static static with default lifetime
81 changes: 0 additions & 81 deletions src/test/run-pass/rfc1623.rs.bk

This file was deleted.