{"action":"created","comment":{"url":"https://api.github.com/repos/rust-lang/rfcs/pulls/comments/25404506","id":25404506,"diff_hunk":"@@ -0,0 +1,150 @@\n+- Feature Name: const_fn\n+- Start Date: 2015-02-25\n+- RFC PR: (leave this empty)\n+- Rust Issue: (leave this empty)\n+\n+# Summary\n+\n+Allow marking free functions and inherent methods as `const`, enabling them to be\n+called in constants contexts, with constant arguments.\n+\n+# Motivation\n+\n+As it is right now, `UnsafeCell` is a stabilization and safety hazard: the field\n+it is supposed to be wrapping is public. This is only done out of the necessity\n+to initialize static items containing atomics, mutexes, etc. - for example:\n+```rust\n+#[lang=\"unsafe_cell\"]\n+struct UnsafeCell<T> { pub value: T }\n+struct AtomicUsize { v: UnsafeCell<usize> }\n+const ATOMIC_USIZE_INIT: AtomicUsize = AtomicUsize {\n+    v: UnsafeCell { value: 0 }\n+};\n+```\n+This approach is fragile and doesn't compose well - consider having to initialize\n+an `AtomicUsize` static with `usize::MAX` - you would need a `const` for each\n+possible value.\n+Also, types like `AtomicPtr<T>` or `Cell<T>` have no way *at all* to initialize\n+them in constant contexts, leading to overuse of `UnsafeCell` or `static mut`,\n+disregarding type safety and proper abstractions.\n+During implementation, the worst offender I've found was `std::thread_local`:\n+all the fields of `std::thread_local::imp::Key` are public, so they can be\n+filled in by a macro - and they're marked \"stable\".\n+\n+A pre-RFC for the removal of the dangerous (and oftenly misued) `static mut`\n+received positive feedback, but only under the condition that abstractions\n+could be created and used in `const` and `static` items.\n+\n+Another concern is the ability to use certain intrinsics, like `size_of`, inside\n+constant expressions, including fixed-length array types. Unlike keyword-based\n+alternatives, `const fn` provides an extensible and composable building block\n+for such features.\n+\n+# Detailed design\n+\n+Functions and inherent methods can be marked as `const`:\n+```rust\n+const fn foo(x: T, y: U) -> Foo {\n+    stmts;\n+    expr\n+}\n+impl Foo {\n+    const fn new(x: T) -> Foo {\n+        stmts;\n+        expr\n+    }\n+\n+    const fn transform(self, y: U) -> Foo {\n+        stmts;\n+        expr\n+    }\n+}\n+```\n+Traits, trait implementations and their methods cannot be `const` - this\n+allows us to properly design a constness/CTFE system that interacts well\n+with traits - for more details, see *Alternatives*.\n+Only simple by-value immutable bindings are allowed as arguments' patterns.\n+The body of the function is checked as if it were a block inside a `const`:\n+```rust\n+const FOO: Foo = {\n+    // Currently, only item \"statements\" are allowed here.\n+    stmts;\n+    // The function's arguments and constant expressions can be freely combined.\n+    expr\n+}\n+```\n+For the purpose of rvalue promotion (to static memory), arguments are considered\n+potentially varying, because the function can still be called with non-constant\n+values at runtime.\n+\n+`const` functions and methods can be called from any constant expression:\n+```rust\n+// Standalone example.\n+struct Point { x: i32, y: i32 }\n+\n+impl Point {\n+    const fn new(x: i32, y: i32) -> Point {\n+        Point { x: x, y: y }\n+    }\n+\n+    const fn add(self, other: Point) -> Point {\n+        Point::new(self.x + other.x, self.y + other.y)\n+    }\n+}\n+\n+const ORIGIN: Point = Point::new(0, 0);\n+\n+const fn sum_test(xs: [Point; 3]) -> Point {\n+    xs[0].add(xs[1]).add(xs[2])\n+}\n+\n+const A: Point = Point::new(1, 0);\n+const B: Point = Point::new(0, 1);\n+const C: Point = A.add(B);\n+const D: Point = sum_test([A, B, C]);\n+\n+// Assuming the Foo::new methods used here are const.\n+static FLAG: AtomicBool = AtomicBool::new(true);\n+static COUNTDOWN: AtomicUsize = AtomicUsize::new(10);\n+#[thread_local]\n+static TLS_COUNTER: Cell<u32> = Cell::new(1);\n+```\n+\n+# Drawbacks\n+\n+None that I know of.\n+\n+# Alternatives\n+\n+* Not do anything for 1.0. This would result in some APIs being crippled and\n+serious backwards compatibility issues - `UnsafeCell`'s `value` field cannot","path":"text/0000-const-fn.md","position":120,"original_position":120,"commit_id":"3c02ef5cc680b9e0472da57bfb69e5196a0fd312","original_commit_id":"3c02ef5cc680b9e0472da57bfb69e5196a0fd312","user":{"login":"huonw","id":1203825,"avatar_url":"https://avatars.githubusercontent.com/u/1203825?v=3","gravatar_id":"","url":"https://api.github.com/users/huonw","html_url":"https://github.com/huonw","followers_url":"https://api.github.com/users/huonw/followers","following_url":"https://api.github.com/users/huonw/following{/other_user}","gists_url":"https://api.github.com/users/huonw/gists{/gist_id}","starred_url":"https://api.github.com/users/huonw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/huonw/subscriptions","organizations_url":"https://api.github.com/users/huonw/orgs","repos_url":"https://api.github.com/users/huonw/repos","events_url":"https://api.github.com/users/huonw/events{/privacy}","received_events_url":"https://api.github.com/users/huonw/received_events","type":"User","site_admin":false},"body":"There are ways around this, for example: stability checking will not trigger on code that's directly generated by a `std` macro, so we could have the field be marked `unstable` and a macro like\r\n\r\n```rust\r\nmacro_rules! unsafe_cell {\r\n    ($e: expr) => { UnsafeCell { value: $e } }\r\n}\r\n```\r\n\r\nThis would ensure that `UnsafeCell` values can be constructed via `unsafe_cell!(foo)`, but not allow direct access to the `value` field in the stable channel.\r\n\r\nDownside: it would introduce an `unsafe_cell` macro to global namespace. If/when we get a more structured way to construct such values, the body of the macro can be replaced with that, and the macro itself can become deprecated.","created_at":"2015-02-26T04:43:36Z","updated_at":"2015-02-26T04:43:36Z","html_url":"https://github.com/rust-lang/rfcs/pull/911#discussion_r25404506","pull_request_url":"https://api.github.com/repos/rust-lang/rfcs/pulls/911","_links":{"self":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/comments/25404506"},"html":{"href":"https://github.com/rust-lang/rfcs/pull/911#discussion_r25404506"},"pull_request":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/911"}}},"pull_request":{"url":"https://api.github.com/repos/rust-lang/rfcs/pulls/911","id":30030260,"html_url":"https://github.com/rust-lang/rfcs/pull/911","diff_url":"https://github.com/rust-lang/rfcs/pull/911.diff","patch_url":"https://github.com/rust-lang/rfcs/pull/911.patch","issue_url":"https://api.github.com/repos/rust-lang/rfcs/issues/911","number":911,"state":"open","locked":false,"title":"Const functions and inherent methods.","user":{"login":"eddyb","id":77424,"avatar_url":"https://avatars.githubusercontent.com/u/77424?v=3","gravatar_id":"","url":"https://api.github.com/users/eddyb","html_url":"https://github.com/eddyb","followers_url":"https://api.github.com/users/eddyb/followers","following_url":"https://api.github.com/users/eddyb/following{/other_user}","gists_url":"https://api.github.com/users/eddyb/gists{/gist_id}","starred_url":"https://api.github.com/users/eddyb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eddyb/subscriptions","organizations_url":"https://api.github.com/users/eddyb/orgs","repos_url":"https://api.github.com/users/eddyb/repos","events_url":"https://api.github.com/users/eddyb/events{/privacy}","received_events_url":"https://api.github.com/users/eddyb/received_events","type":"User","site_admin":false},"body":"[Rendered](https://github.com/eddyb/rfcs/blob/const-fn/text/0000-const-fn.md)\r\n\r\nReference implementation is at rust-lang/rust#22816.","created_at":"2015-02-25T22:07:36Z","updated_at":"2015-02-26T04:43:36Z","closed_at":null,"merged_at":null,"merge_commit_sha":"9d8c672ec40ce456b30a1615733dbb5cfde34d38","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/rust-lang/rfcs/pulls/911/commits","review_comments_url":"https://api.github.com/repos/rust-lang/rfcs/pulls/911/comments","review_comment_url":"https://api.github.com/repos/rust-lang/rfcs/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rust-lang/rfcs/issues/911/comments","statuses_url":"https://api.github.com/repos/rust-lang/rfcs/statuses/3c02ef5cc680b9e0472da57bfb69e5196a0fd312","head":{"label":"eddyb:const-fn","ref":"const-fn","sha":"3c02ef5cc680b9e0472da57bfb69e5196a0fd312","user":{"login":"eddyb","id":77424,"avatar_url":"https://avatars.githubusercontent.com/u/77424?v=3","gravatar_id":"","url":"https://api.github.com/users/eddyb","html_url":"https://github.com/eddyb","followers_url":"https://api.github.com/users/eddyb/followers","following_url":"https://api.github.com/users/eddyb/following{/other_user}","gists_url":"https://api.github.com/users/eddyb/gists{/gist_id}","starred_url":"https://api.github.com/users/eddyb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eddyb/subscriptions","organizations_url":"https://api.github.com/users/eddyb/orgs","repos_url":"https://api.github.com/users/eddyb/repos","events_url":"https://api.github.com/users/eddyb/events{/privacy}","received_events_url":"https://api.github.com/users/eddyb/received_events","type":"User","site_admin":false},"repo":{"id":25957759,"name":"rfcs","full_name":"eddyb/rfcs","owner":{"login":"eddyb","id":77424,"avatar_url":"https://avatars.githubusercontent.com/u/77424?v=3","gravatar_id":"","url":"https://api.github.com/users/eddyb","html_url":"https://github.com/eddyb","followers_url":"https://api.github.com/users/eddyb/followers","following_url":"https://api.github.com/users/eddyb/following{/other_user}","gists_url":"https://api.github.com/users/eddyb/gists{/gist_id}","starred_url":"https://api.github.com/users/eddyb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eddyb/subscriptions","organizations_url":"https://api.github.com/users/eddyb/orgs","repos_url":"https://api.github.com/users/eddyb/repos","events_url":"https://api.github.com/users/eddyb/events{/privacy}","received_events_url":"https://api.github.com/users/eddyb/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/eddyb/rfcs","description":"RFCs for changes to Rust","fork":true,"url":"https://api.github.com/repos/eddyb/rfcs","forks_url":"https://api.github.com/repos/eddyb/rfcs/forks","keys_url":"https://api.github.com/repos/eddyb/rfcs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/eddyb/rfcs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/eddyb/rfcs/teams","hooks_url":"https://api.github.com/repos/eddyb/rfcs/hooks","issue_events_url":"https://api.github.com/repos/eddyb/rfcs/issues/events{/number}","events_url":"https://api.github.com/repos/eddyb/rfcs/events","assignees_url":"https://api.github.com/repos/eddyb/rfcs/assignees{/user}","branches_url":"https://api.github.com/repos/eddyb/rfcs/branches{/branch}","tags_url":"https://api.github.com/repos/eddyb/rfcs/tags","blobs_url":"https://api.github.com/repos/eddyb/rfcs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/eddyb/rfcs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/eddyb/rfcs/git/refs{/sha}","trees_url":"https://api.github.com/repos/eddyb/rfcs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/eddyb/rfcs/statuses/{sha}","languages_url":"https://api.github.com/repos/eddyb/rfcs/languages","stargazers_url":"https://api.github.com/repos/eddyb/rfcs/stargazers","contributors_url":"https://api.github.com/repos/eddyb/rfcs/contributors","subscribers_url":"https://api.github.com/repos/eddyb/rfcs/subscribers","subscription_url":"https://api.github.com/repos/eddyb/rfcs/subscription","commits_url":"https://api.github.com/repos/eddyb/rfcs/commits{/sha}","git_commits_url":"https://api.github.com/repos/eddyb/rfcs/git/commits{/sha}","comments_url":"https://api.github.com/repos/eddyb/rfcs/comments{/number}","issue_comment_url":"https://api.github.com/repos/eddyb/rfcs/issues/comments{/number}","contents_url":"https://api.github.com/repos/eddyb/rfcs/contents/{+path}","compare_url":"https://api.github.com/repos/eddyb/rfcs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/eddyb/rfcs/merges","archive_url":"https://api.github.com/repos/eddyb/rfcs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/eddyb/rfcs/downloads","issues_url":"https://api.github.com/repos/eddyb/rfcs/issues{/number}","pulls_url":"https://api.github.com/repos/eddyb/rfcs/pulls{/number}","milestones_url":"https://api.github.com/repos/eddyb/rfcs/milestones{/number}","notifications_url":"https://api.github.com/repos/eddyb/rfcs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/eddyb/rfcs/labels{/name}","releases_url":"https://api.github.com/repos/eddyb/rfcs/releases{/id}","created_at":"2014-10-30T06:24:46Z","updated_at":"2014-10-29T16:53:17Z","pushed_at":"2015-02-25T22:07:17Z","git_url":"git://github.com/eddyb/rfcs.git","ssh_url":"git@github.com:eddyb/rfcs.git","clone_url":"https://github.com/eddyb/rfcs.git","svn_url":"https://github.com/eddyb/rfcs","homepage":"","size":1140,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"rust-lang:master","ref":"master","sha":"0806be4f282144cfcd55b1d20284b43f87cbe1c6","user":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"repo":{"id":17527989,"name":"rfcs","full_name":"rust-lang/rfcs","owner":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rust-lang/rfcs","description":"RFCs for changes to Rust","fork":false,"url":"https://api.github.com/repos/rust-lang/rfcs","forks_url":"https://api.github.com/repos/rust-lang/rfcs/forks","keys_url":"https://api.github.com/repos/rust-lang/rfcs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rust-lang/rfcs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rust-lang/rfcs/teams","hooks_url":"https://api.github.com/repos/rust-lang/rfcs/hooks","issue_events_url":"https://api.github.com/repos/rust-lang/rfcs/issues/events{/number}","events_url":"https://api.github.com/repos/rust-lang/rfcs/events","assignees_url":"https://api.github.com/repos/rust-lang/rfcs/assignees{/user}","branches_url":"https://api.github.com/repos/rust-lang/rfcs/branches{/branch}","tags_url":"https://api.github.com/repos/rust-lang/rfcs/tags","blobs_url":"https://api.github.com/repos/rust-lang/rfcs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rust-lang/rfcs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rust-lang/rfcs/git/refs{/sha}","trees_url":"https://api.github.com/repos/rust-lang/rfcs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rust-lang/rfcs/statuses/{sha}","languages_url":"https://api.github.com/repos/rust-lang/rfcs/languages","stargazers_url":"https://api.github.com/repos/rust-lang/rfcs/stargazers","contributors_url":"https://api.github.com/repos/rust-lang/rfcs/contributors","subscribers_url":"https://api.github.com/repos/rust-lang/rfcs/subscribers","subscription_url":"https://api.github.com/repos/rust-lang/rfcs/subscription","commits_url":"https://api.github.com/repos/rust-lang/rfcs/commits{/sha}","git_commits_url":"https://api.github.com/repos/rust-lang/rfcs/git/commits{/sha}","comments_url":"https://api.github.com/repos/rust-lang/rfcs/comments{/number}","issue_comment_url":"https://api.github.com/repos/rust-lang/rfcs/issues/comments{/number}","contents_url":"https://api.github.com/repos/rust-lang/rfcs/contents/{+path}","compare_url":"https://api.github.com/repos/rust-lang/rfcs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rust-lang/rfcs/merges","archive_url":"https://api.github.com/repos/rust-lang/rfcs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rust-lang/rfcs/downloads","issues_url":"https://api.github.com/repos/rust-lang/rfcs/issues{/number}","pulls_url":"https://api.github.com/repos/rust-lang/rfcs/pulls{/number}","milestones_url":"https://api.github.com/repos/rust-lang/rfcs/milestones{/number}","notifications_url":"https://api.github.com/repos/rust-lang/rfcs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rust-lang/rfcs/labels{/name}","releases_url":"https://api.github.com/repos/rust-lang/rfcs/releases{/id}","created_at":"2014-03-07T21:29:00Z","updated_at":"2015-02-25T08:26:48Z","pushed_at":"2015-02-23T17:29:13Z","git_url":"git://github.com/rust-lang/rfcs.git","ssh_url":"git@github.com:rust-lang/rfcs.git","clone_url":"https://github.com/rust-lang/rfcs.git","svn_url":"https://github.com/rust-lang/rfcs","homepage":"","size":8732,"stargazers_count":267,"watchers_count":267,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":169,"mirror_url":null,"open_issues_count":447,"forks":169,"open_issues":447,"watchers":267,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/911"},"html":{"href":"https://github.com/rust-lang/rfcs/pull/911"},"issue":{"href":"https://api.github.com/repos/rust-lang/rfcs/issues/911"},"comments":{"href":"https://api.github.com/repos/rust-lang/rfcs/issues/911/comments"},"review_comments":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/911/comments"},"review_comment":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/911/commits"},"statuses":{"href":"https://api.github.com/repos/rust-lang/rfcs/statuses/3c02ef5cc680b9e0472da57bfb69e5196a0fd312"}}}}