Skip to content

Mistakenly using a Dynamicparam block in single-body functions fails obscurely #6431

@mklement0

Description

@mklement0

This is a minor problem and the behavior is as designed, but perhaps the error message can be improved.

A dynamicparam {} block requires an advanced function's other code to be in begin {}, process{} and end {} blocks.

Instead trying to use dynamicparam in the function's main block alongside top-level code fails obscurely:

Steps to reproduce

Function foo {
    [CmdletBinding()] param()
    DynamicParam {
    }

    # Placing code here, outside of begin {}, process {} or end {} fails obscurely.
    'hi'
}

Expected behavior

Actual behavior

At /Users/mklement/Desktop/pg/pg.ps1:33 char:14
+ Function foo {
+              ~
Missing closing '}' in statement block or type definition.

It is not obvious from this error message what the problem is.

Enclosing 'hi' in end { ... } fixes the problem.

Aside from a more helpful error message, perhaps allowing this code is an option (where the top-level code would run in an implied end {} block),
though perhaps that requires special-cased parsing behavior that is not worth the trouble.

Environment data

PowerShell Core v6.0.2 on macOS 10.13.3
PowerShell Core v6.0.2 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.2 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.674 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)

Metadata

Metadata

Assignees

Labels

Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-FixedThe issue is fixed.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions