Skip to content

Commit

Permalink
Error on use of self intead of Self in return type (#6763)
Browse files Browse the repository at this point in the history
## Description

With this PR we no longer support using `self` instead of `Self` when
referring to the Self Type.

Fixes #6396

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
esdrubal authored Nov 28, 2024
1 parent bcd15c3 commit a02e8ea
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 3 deletions.
7 changes: 5 additions & 2 deletions sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,7 @@ pub(crate) fn type_name_to_type_info_opt(name: &Ident) -> Option<TypeInfo> {
"str" => Some(TypeInfo::StringSlice),
"raw_ptr" => Some(TypeInfo::RawUntypedPtr),
"raw_slice" => Some(TypeInfo::RawUntypedSlice),
"Self" | "self" => Some(TypeInfo::new_self_type(name.span())),
"Self" => Some(TypeInfo::new_self_type(name.span())),
"Contract" => Some(TypeInfo::Contract),
_other => None,
}
Expand Down Expand Up @@ -4598,7 +4598,10 @@ fn path_type_to_type_info(
}
}
None => {
if name.as_str() == "ContractCaller" {
if name.as_str() == "self" {
let error = ConvertParseTreeError::UnknownTypeNameSelf { span };
return Err(handler.emit_err(error.into()));
} else if name.as_str() == "ContractCaller" {
if root_opt.is_some() || !suffix.is_empty() {
let error = ConvertParseTreeError::FullySpecifiedTypesNotSupported { span };
return Err(handler.emit_err(error.into()));
Expand Down
3 changes: 3 additions & 0 deletions sway-error/src/convert_parse_tree_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ pub enum ConvertParseTreeError {
UnexpectedValueForCfgExperimental { span: Span },
#[error("Unexpected attribute value: \"{value}\" for attribute: \"cfg\"")]
InvalidCfgArg { span: Span, value: String },
#[error("Unknown type name \"self\". A self type with a similar name exists (notice the capitalization): `Self`")]
UnknownTypeNameSelf { span: Span },
}

impl Spanned for ConvertParseTreeError {
Expand Down Expand Up @@ -185,6 +187,7 @@ impl Spanned for ConvertParseTreeError {
ConvertParseTreeError::ExpectedCfgProgramTypeArgValue { span } => span.clone(),
ConvertParseTreeError::UnexpectedValueForCfgExperimental { span } => span.clone(),
ConvertParseTreeError::InvalidCfgArg { span, .. } => span.clone(),
ConvertParseTreeError::UnknownTypeNameSelf { span } => span.clone(),
}
}
}
2 changes: 1 addition & 1 deletion sway-lib-std/src/bytes.sw
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ impl Bytes {
/// assert(bytes.capacity() == first_cap + second_cap);
/// }
/// ```
pub fn append(ref mut self, ref mut other: self) {
pub fn append(ref mut self, ref mut other: Self) {
let other_len = other.len();
if other_len == 0 {
return
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[package]]
name = "core"
source = "path+from-root-722DFCAB6A209427"

[[package]]
name = "self_return_type"
source = "member"
dependencies = ["core"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
name = "self_return_type"
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
implicit-std = false

[dependencies]
core = { path = "../../../../../../sway-lib-core" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
script;
trait MyTrait {
fn foo(self, other: Self) -> Self;
}
impl MyTrait for u8 {
fn foo(self, other: Self) -> self {
self
}
}
fn main() -> () {
let a = 1u8;
let _ = a.foo(2u8);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
category = "fail"

# check: $()fn foo(self, other: Self) -> self {
# nextln: $()Unknown type name "self". A self type with a similar name exists (notice the capitalization): `Self`

0 comments on commit a02e8ea

Please sign in to comment.