Skip to content

Can't fulfill 'static bound with &'static pointer. #7268

Closed
@bblum

Description

@bblum
Contributor

This should be legal but isn't:

fn foo<T: 'static>(_: T) {}

fn bar<T>(x: &'static T) {
    foo(x);
}
fn main() {}

@nikomatsakis ?

nominating feature-complete

Activity

nikomatsakis

nikomatsakis commented on Jun 21, 2013

@nikomatsakis
Contributor

no bug. The type parameter T could contain borrowed pointers, as the error message correctly pointed out. You want:

fn foo<T: 'static>(_: T) {}

fn bar<T: 'static>(x: &'static T) {
    foo(x);
}
fn main() {}
bblum

bblum commented on Jun 22, 2013

@bblum
ContributorAuthor

Not to mean this should change, but it should also be impossible to (safely) construct a &'static &'r T, right?

nikomatsakis

nikomatsakis commented on Jun 23, 2013

@nikomatsakis
Contributor

That is true.

nikomatsakis

nikomatsakis commented on Jun 28, 2013

@nikomatsakis
Contributor

I decided you're right, we ought to try &'static T as possessing the static bound for all T, since there are already other parts of the type system that rely on the fact that for all &'foo T, T cannot have region pointers that have shorter lifetime than 'foo

bluss

bluss commented on Jul 2, 2013

@bluss
Member

Here is a testcase for putting &'static fn() closures into trait objects. Of course sendable closures are also interesting (do we have to use extern fn for that?).

/*
error: cannot pack type `~Test`, which does not fulfill `'static`, as a trait bounded by 'static
let trait_obj = (~t) as ~EvalOnce:'static;
                ^~~~~~~~~~~~~~~~~~~~~~~~~
*/

struct Test {
    f: &'static fn:'static(),
}

trait EvalOnce {
    fn eval(~self);
}

impl EvalOnce for Test {
    fn eval(~self) {
        (self.f)();
    }
}

fn test_me() {
    let t: Test  = Test { f: || { } };
    let trait_obj = (~t) as ~EvalOnce:'static;
}
msullivan

msullivan commented on Aug 23, 2013

@msullivan
Contributor

Seconding the nomination for feature complete, I suppose?

catamorphism

catamorphism commented on Sep 5, 2013

@catamorphism
Contributor

accepted for production-ready

pnkfelix

pnkfelix commented on Mar 20, 2014

@pnkfelix
Member

Could be added backwards-compatibly in the future. Assigning P-low, not 1.0 blocker.

pnkfelix

pnkfelix commented on Mar 24, 2014

@pnkfelix
Member

@nikomatsakis the natural generalization here, assuming we finish #5121 by allowing lifetime kind bounds other than 'static, is that fn foo<T:'b>(x: &'a T) { ... } would imply that actually T: 'a + 'b, right?

ghost added
E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
on Oct 30, 2014
added a commit that references this issue on Jun 3, 2021

Auto merge of rust-lang#7268 - mbartlett21:update_semi, r=Manishearth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsA-type-systemArea: Type systemE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.P-lowLow priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @nikomatsakis@pnkfelix@msullivan@catamorphism@bblum

      Issue actions

        Can't fulfill 'static bound with &'static pointer. · Issue #7268 · rust-lang/rust