Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,15 @@ impl ty::TyExpression {
span,
}
}
// `self` and `Self` are inserted into the namespace as a generic type parameter
// (`GenericTypeForFunctionScope`). When `self` is used as a value but the enclosing
// function has no `self` parameter, resolution falls through to that type parameter.
// Reporting it as "actually a generic type parameter" is misleading, so emit a
// dedicated error pointing at the missing `self` parameter instead.
Some(ty::TyDecl::GenericTypeForFunctionScope(_)) if name.as_str() == "self" => {
let err = handler.emit_err(CompileError::SelfParameterNotAvailable { span });
ty::TyExpression::error(err, name.span(), engines)
}
Some(a) => {
let err = handler.emit_err(CompileError::NotAVariable {
name: name.clone(),
Expand Down
5 changes: 5 additions & 0 deletions sway-error/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ pub enum CompileError {
what_it_is: &'static str,
span: Span,
},
#[error(
"\"self\" is not available here because the enclosing function does not have a \"self\" parameter."
)]
SelfParameterNotAvailable { span: Span },
#[error("{feature} is currently not implemented.")]
Unimplemented {
/// The description of the unimplemented feature,
Expand Down Expand Up @@ -1261,6 +1265,7 @@ impl Spanned for CompileError {
ModuleDepGraphCyclicReference { .. } => Span::dummy(),
UnknownVariable { span, .. } => span.clone(),
NotAVariable { span, .. } => span.clone(),
SelfParameterNotAvailable { span, .. } => span.clone(),
Unimplemented { span, .. } => span.clone(),
TypeError(err) => err.span(),
ParseError { span, .. } => span.clone(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = 'self_used_without_self_parameter'
source = 'member'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
name = "self_used_without_self_parameter"
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
library;

struct S {}

impl S {
fn use_self_method() {
let _ = self.x();
}

fn use_self_value() {
let _ = self;
}

fn x(self) -> u64 {
0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
category = "fail"

# check: $()let _ = self.x();
# nextln: $()"self" is not available here because the enclosing function does not have a "self" parameter.

# check: $()let _ = self;
# nextln: $()"self" is not available here because the enclosing function does not have a "self" parameter.
Loading